-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.sql
84 lines (78 loc) · 2.4 KB
/
database.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
-- Create a PostgreSQL database named `vanpool`
-- Run these queries in order to create the tables and relationships
-- USER is a reserved keyword with Postgres
-- You must use double quotes in every query that user is in:
-- ex. SELECT * FROM "user";
-- Otherwise you will have errors!
CREATE TABLE "user" (
"id" SERIAL PRIMARY KEY,
-- username will be an email address
"username" VARCHAR (80) UNIQUE NOT NULL,
"password" VARCHAR (1000) NOT NULL,
"full_name" VARCHAR (80) NOT NULL,
"display_name" VARCHAR (20) NOT NULL,
-- phone numbers will be formatted and parsed by the app
"cell" VARCHAR (20),
-- admin must manually grant access
"access_level" INT DEFAULT 0
);
CREATE TYPE "e_wkday" AS ENUM (
-- We probably won't use Sat or Sun but they're still days of the week
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
);
CREATE TABLE "days" (
-- the day primary key will be integers of the form YYYYMMDD
-- e.g., 20191017; it will generated in-app, not by Postgres
"id" INT UNIQUE NOT NULL PRIMARY KEY,
"date" DATE UNIQUE NOT NULL,
-- redundant; these will be set/used directly by Moment.js
-- it's a bit belt-and-suspenders but it's much easier to query
"year" INT NOT NULL,
"month" INT NOT NULL,
"week" INT NOT NULL,
"weekday" e_wkday NOT NULL,
"day" INT NOT NULL,
-- there can be only one driver per day
"driver_id" INT REFERENCES "user"
);
CREATE TABLE "user_days" (
-- many-to-many junction table
"id" SERIAL PRIMARY KEY,
-- form the junctions
"user_id" INT REFERENCES "user",
"days_id" INT REFERENCES "days",
-- is the given user riding and/or driving on the given day?
"riding" BOOLEAN DEFAULT false,
"driving" BOOLEAN DEFAULT false,
-- did they pay for gas?
"gas_credit" FLOAT DEFAULT 0
);
CREATE TABLE "messages" (
"id" SERIAL PRIMARY KEY,
"content" TEXT NOT NULL,
-- a message is posted at a specific time
"time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- by a specific user
"user_id" INT REFERENCES "user",
-- but can apply to a different date
"days_id" INT REFERENCES "days"
);
CREATE TABLE "routes" (
-- store global variables related to a vanpool route
-- currently, the app only supports a single route per instance
"id" SERIAL PRIMARY KEY,
"name" VARCHAR (20) NOT NULL,
"description" TEXT,
-- only needed if the app is tracking finances too
"show_charge" BOOLEAN DEFAULT false,
"ride_fee" FLOAT DEFAULT 0,
"drive_credit" FLOAT DEFAULT 0,
"max_fee" FLOAT,
"min_fee" FLOAT
);