The schema existed only inside the queries, so a clean clone could not run: nothing created the tables that database.py and main.py expect, and there was no declaration of what to install. - schema.sql: the seven tables, their primary and foreign keys, and the administrator the code requires. Reconstructed from the SQL the code issues and typed for it: the code reads rows by position (SELECT *), so the column order is part of the contract, and users.userid has to be a 4-byte integer because message_all() counts rows with pg_column_size(userid)/4. - scripts/db.py, Makefile, docker-compose.yml: a throwaway PostgreSQL and the targets up/schema/seed/run/test/down. Both passwords come from the environment (NETFLOX_DB_PASSWORD, NETFLOX_DEMO_PASSWORD); nothing is stored in the repository, and schema.sql receives the administrator password through a session setting. - requirements.txt and requirements-dev.txt: psycopg2-binary, plus pytest for the suite. - tests/: 30 end-to-end tests that recreate the schema and drive the real functions of database.py. They check the column order and the keys against information_schema and walk every main flow. The defects found while mapping the code are pinned by test_known_bug_* and xfail instead of fixed, since fixing the application was not part of this change. The suite also guards the credential fix: database.py must refuse to import without its variable and no tracked file may contain the value. - README.md: the sections shared with the other repositories, with a real session. - The report moved to docs/relatorio-BD.pdf: it had accents and spaces in the filename.
151 lines
7.4 KiB
SQL
151 lines
7.4 KiB
SQL
-- Netflox - relational schema.
|
|
--
|
|
-- Reconstructed from the SQL the application actually issues (database.py,
|
|
-- main.py). Every table and column below is read or written by that code and
|
|
-- nothing else was added: the application is the specification.
|
|
--
|
|
-- The application reads rows positionally (`SELECT *` followed by row[1],
|
|
-- row[4], ...), so the ORDER of the columns in each CREATE TABLE is part of the
|
|
-- contract and must not be changed:
|
|
--
|
|
-- users userid, nome, email, password, balance
|
|
-- articles itemid, name, director, release_year, imbd_rating, genre,
|
|
-- price, type, time_available
|
|
-- messages msgid, message, bolread, data, users_userid, senderid
|
|
-- actors actorid, name
|
|
-- articles_actors articles_itemid, actors_actorid
|
|
-- rents rentsid, purchased_date, end_date, articles_itemid,
|
|
-- users_userid
|
|
-- pricehistory pricehistoryid, old_price, change_date, articles_itemid
|
|
--
|
|
-- Load-bearing typing details:
|
|
-- * users.userid is a 4-byte integer. database.py:message_all() counts
|
|
-- recipients with `SELECT Sum(pg_column_size(userid))/4 FROM users`; with a
|
|
-- bigint (8 bytes per row) it would try to send twice as many messages as
|
|
-- there are users.
|
|
-- * Date columns are DATE: the code inserts CURRENT_DATE and
|
|
-- CURRENT_DATE + time_available.
|
|
-- * Money is NUMERIC(x,2): the code subtracts in Python and writes the result
|
|
-- back, and statistics() sums prices.
|
|
-- * `imbd_rating` keeps the spelling used by database.py (it is a typo, but it
|
|
-- is the name in every query).
|
|
-- * email and name are deliberately NOT UNIQUE: create_user() performs no
|
|
-- duplicate check and log_in() takes the first match, so the tables have to
|
|
-- accept what the code accepts. The foreign keys below are what the code
|
|
-- really assumes, and they are what makes the delete paths as narrow as they
|
|
-- are (see README, "Known limitations").
|
|
--
|
|
-- Applied by `make schema`, which first recreates the `public` schema
|
|
-- (scripts/db.py). The database itself (NetfloxFinal) is created by
|
|
-- docker-compose.yml, and the connection parameters are the ones hard-coded in
|
|
-- database.py: host=localhost dbname=NetfloxFinal user=postgres.
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- users: accounts. The email decides the role in log_in(): any address under
|
|
-- netflox.com is an administrator, every other one is a client.
|
|
-- --------------------------------------------------------------------------
|
|
CREATE TABLE users (
|
|
userid integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
|
nome text NOT NULL,
|
|
email text NOT NULL,
|
|
password text NOT NULL, -- plain text, as the application stores it
|
|
balance numeric(8,2) NOT NULL
|
|
);
|
|
|
|
CREATE INDEX users_email_idx ON users (email);
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- articles: the catalogue. `type` is 'movie' or 'series' (statistics() queries
|
|
-- exactly those two literals); price is what a rent costs and time_available is
|
|
-- the number of days a rent lasts.
|
|
-- --------------------------------------------------------------------------
|
|
CREATE TABLE articles (
|
|
itemid integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
|
name text NOT NULL,
|
|
director text NOT NULL,
|
|
release_year integer NOT NULL,
|
|
imbd_rating numeric(3,1) NOT NULL,
|
|
genre text NOT NULL,
|
|
price numeric(6,2) NOT NULL,
|
|
type text NOT NULL,
|
|
time_available integer NOT NULL
|
|
);
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- actors + articles_actors: many-to-many between the catalogue and its cast.
|
|
-- add_article() reuses an existing actor row instead of duplicating it.
|
|
-- --------------------------------------------------------------------------
|
|
CREATE TABLE actors (
|
|
actorid integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
|
name text NOT NULL
|
|
);
|
|
|
|
CREATE TABLE articles_actors (
|
|
articles_itemid integer NOT NULL REFERENCES articles (itemid),
|
|
actors_actorid integer NOT NULL REFERENCES actors (actorid),
|
|
PRIMARY KEY (articles_itemid, actors_actorid)
|
|
);
|
|
|
|
CREATE INDEX articles_actors_actor_idx ON articles_actors (actors_actorid);
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- rents: one row per purchase; end_date is purchased_date + time_available.
|
|
-- my_articles() lists the rows with end_date >= CURRENT_DATE and my_history()
|
|
-- the ones before it, so an expired rent is kept, not deleted.
|
|
-- --------------------------------------------------------------------------
|
|
CREATE TABLE rents (
|
|
rentsid integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
|
purchased_date date NOT NULL,
|
|
end_date date NOT NULL,
|
|
articles_itemid integer NOT NULL REFERENCES articles (itemid),
|
|
users_userid integer NOT NULL REFERENCES users (userid)
|
|
);
|
|
|
|
CREATE INDEX rents_user_idx ON rents (users_userid);
|
|
CREATE INDEX rents_article_idx ON rents (articles_itemid);
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- messages: internal mail. users_userid is the recipient, senderid the sender
|
|
-- (both are regular userids) and data is the day it was sent.
|
|
-- --------------------------------------------------------------------------
|
|
CREATE TABLE messages (
|
|
msgid integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
|
message text NOT NULL,
|
|
bolread boolean NOT NULL DEFAULT FALSE,
|
|
data date NOT NULL,
|
|
users_userid integer NOT NULL REFERENCES users (userid),
|
|
senderid integer NOT NULL REFERENCES users (userid)
|
|
);
|
|
|
|
CREATE INDEX messages_recipient_idx ON messages (users_userid);
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- pricehistory: written by change_price() with the price that was replaced.
|
|
-- Nothing in the application reads this table back.
|
|
-- --------------------------------------------------------------------------
|
|
CREATE TABLE pricehistory (
|
|
pricehistoryid integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
|
old_price numeric(6,2) NOT NULL,
|
|
change_date date NOT NULL,
|
|
articles_itemid integer NOT NULL REFERENCES articles (itemid)
|
|
);
|
|
|
|
CREATE INDEX pricehistory_article_idx ON pricehistory (articles_itemid);
|
|
|
|
-- --------------------------------------------------------------------------
|
|
-- Minimal seed required by the code: one administrator.
|
|
--
|
|
-- log_in() classifies every address under netflox.com as an administrator, and
|
|
-- create_user() refuses to create accounts in that domain, so this row can only
|
|
-- be created here. The admin is also what statistics() assumes when it reports
|
|
-- "Total number of users" as (number of rows - 1), and message_all() divides by
|
|
-- rows, so the table must not be empty.
|
|
--
|
|
-- The password is not committed: it is read from the session setting
|
|
-- `netflox.admin_password`, which `make schema` sets from $NETFLOX_DEMO_PASSWORD.
|
|
-- Running this file without that setting fails with
|
|
-- ERROR: unrecognized configuration parameter "netflox.admin_password".
|
|
-- --------------------------------------------------------------------------
|
|
INSERT INTO users (nome, email, password, balance)
|
|
VALUES ('Admin', 'admin@netflox.com', current_setting('netflox.admin_password'), 20.00);
|