From 77cf2519b2887a0ab22caec0102da2ab78edbf82 Mon Sep 17 00:00:00 2001 From: Trent Palmer Date: Fri, 8 Jan 2021 20:19:20 -0800 Subject: [PATCH] add database example --- examples/database/README.md | 398 +++++++++++++++++++ examples/database/create_database_tables.sql | 84 ++++ 2 files changed, 482 insertions(+) create mode 100644 examples/database/README.md create mode 100644 examples/database/create_database_tables.sql diff --git a/examples/database/README.md b/examples/database/README.md new file mode 100644 index 0000000..a0085e4 --- /dev/null +++ b/examples/database/README.md @@ -0,0 +1,398 @@ +## login to the psql command line + +`sudo -u postgres psql` + +#### format output +if you want to generate html output, the command is `\pset format html` +but you probably want the default format which is *aligned* + +## create database and role + +`CREATE DATABASE ;` +`CREATE ROLE WITH LOGIN;` +`\password ` +`GRANT ALL PRIVILEGES ON DATABASE TO ;` + +## change database + +`\c ` + +## create the database tables +`sudo -u postgres psql < create_database_tables.sql` + +## sanity check +In order to be able to register, login to the psql command line +and insert your email address into the email_white_list table. + +verify the database table schemas against the models in `models.py`, +and hopefully it matches what is below + +## describe database +`=# \d` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
List of relations
SchemaNameTypeOwner
publiccontributortableapplication_unix_user
publiccontributor_id_seqsequenceapplication_unix_user
publicemail_white_listtableapplication_unix_user
publicemail_white_list_id_seqsequenceapplication_unix_user
publicphototableapplication_unix_user
publicphoto_id_seqsequenceapplication_unix_user
+

(6 rows)
+

+ + +## describe contributor table +`=# \d contributor` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table "public.contributor"
ColumnTypeCollationNullableDefault
idinteger  not nullnextval('contributor_id_seq'::regclass)
namecharacter varying(64)     
emailcharacter varying(120)     
password_hashcharacter varying(128)     
num_photosinteger     
totp_keycharacter(16)     
use_totpboolean    false
+

Indexes:
+    "contributor_pkey" PRIMARY KEY, btree (id)
+    "contributor_email_key" UNIQUE CONSTRAINT, btree (email)
+    "contributor_name_key" UNIQUE CONSTRAINT, btree (name)
+

+ +## describe photo table +`=# \d photo` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table "public.photo"
ColumnTypeCollationNullableDefault
idinteger  not nullnextval('photo_id_seq'::regclass)
photo_namecharacter varying(120)  not null 
contributor_idinteger  not null 
timestamptimestamp without time zone     
timestamp_intbigint     
photo_formatcharacter(12)     
photo_widthinteger     
photo_heightinteger     
photo_1280_widthinteger     
photo_1280_heightinteger     
photo_480_widthinteger     
photo_480_heightinteger     
Makecharacter varying     
Modelcharacter varying     
Softwarecharacter varying     
DateTimetimestamp without time zone     
DateTimeOriginaltimestamp without time zone     
DateTimeDigitizedtimestamp without time zone     
fnumbernumeric     
DigitalZoomRationumeric     
AspectRationumeric     
photo_raw_sizebigint     
photo_1280_sizebigint     
photo_480_sizebigint     
TimeZoneOffsetinteger     
GPSAltitudenumeric     
GPSAboveSeaLevelboolean     
GPSLatitudenumeric     
GPSLongitudenumeric     
+

Indexes:
+    "photo_pkey" PRIMARY KEY, btree (id)
+    "photo_photo_name_key" UNIQUE CONSTRAINT, btree (photo_name)
+

+ + +## describe email_white_list +`=# \d email_white_list` + + + + + + + + + + + + + + + + + + + + + + + +
Table "public.email_white_list"
ColumnTypeCollationNullableDefault
idinteger  not nullnextval('email_white_list_id_seq'::regclass)
emailcharacter varying(120)     
+

Indexes:
+    "email_white_list_pkey" PRIMARY KEY, btree (id)
+    "email_white_list_email_key" UNIQUE CONSTRAINT, btree (email)
+

diff --git a/examples/database/create_database_tables.sql b/examples/database/create_database_tables.sql new file mode 100644 index 0000000..080a7db --- /dev/null +++ b/examples/database/create_database_tables.sql @@ -0,0 +1,84 @@ +\c photo_app_database_name + +CREATE TABLE IF NOT EXISTS email_white_list( + id SERIAL PRIMARY KEY, + email VARCHAR(120) NOT NULL UNIQUE +); + +CREATE TABLE IF NOT EXISTS contributor( + id SERIAL PRIMARY KEY, + name VARCHAR(64) NOT NULL UNIQUE, + email VARCHAR(120) NOT NULL UNIQUE, + password_hash VARCHAR(128), + num_photos INT, + totp_key CHAR(16), + use_totp BOOLEAN DEFAULT '0' +); + +/* +ALTER TABLE contributor ADD COLUMN IF NOT EXISTS num_photos INT; +ALTER TABLE contributor ADD COLUMN IF NOT EXISTS totp_key CHAR(16); +ALTER TABLE contributor ADD COLUMN IF NOT EXISTS use_totp BOOLEAN DEFAULT '0'; +*/ + +CREATE TABLE IF NOT EXISTS photo( + id SERIAL PRIMARY KEY, + photo_name VARCHAR(120) NOT NULL UNIQUE, + contributor_id INT NOT NULL, + timestamp TIMESTAMP WITHOUT TIME ZONE, + timestamp_int BIGINT, + photo_format CHAR(12), + photo_width INT, + photo_height INT, + photo_1280_width INT, + photo_1280_height INT, + photo_480_width INT, + photo_480_height INT, + "Make" VARCHAR, + "Model" VARCHAR, + "Software" VARCHAR, + "DateTime" TIMESTAMP WITHOUT TIME ZONE, + "DateTimeOriginal" TIMESTAMP WITHOUT TIME ZONE, + "DateTimeDigitized" TIMESTAMP WITHOUT TIME ZONE, + fnumber DECIMAL, + "DigitalZoomRatio" DECIMAL, + "AspectRatio" DECIMAL, + photo_raw_size BIGINT, + photo_1280_size BIGINT, + photo_480_size BIGINT, + "TimeZoneOffset" INT, + "GPSAltitude" DECIMAL, + "GPSAboveSeaLevel" BOOLEAN, + "GPSLatitude" DECIMAL, + "GPSLongitude" DECIMAL +); + +/* +ALTER TABLE photo ADD COLUMN IF NOT EXISTS contributor_id INT NOT NULL; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS timestamp TIMESTAMP WITHOUT TIME ZONE; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS timestamp_int BIGINT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_format CHAR(12); +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_width INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_height INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_1280_width INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_1280_height INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_480_width INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_480_height INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "Make" VARCHAR; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "Model" VARCHAR; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "Software" VARCHAR; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "DateTime" TIMESTAMP WITHOUT TIME ZONE; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "DateTimeOriginal" TIMESTAMP WITHOUT TIME ZONE; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "DateTimeDigitized" TIMESTAMP WITHOUT TIME ZONE; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS fnumber DECIMAL; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "DigitalZoomRatio" DECIMAL; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "AspectRatio" DECIMAL; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_raw_size BIGINT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_1280_size BIGINT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS photo_480_size BIGINT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "TimeZoneOffset" INT; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "GPSAltitude" DECIMAL; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "GPSAboveSeaLevel" BOOLEAN; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "GPSLatitude" DECIMAL; +ALTER TABLE photo ADD COLUMN IF NOT EXISTS "GPSLongitude" DECIMAL +*/