-- 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);