-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_structure.sql
177 lines (161 loc) · 4.91 KB
/
database_structure.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
-- Create PostgreSQL DB with name corner_pocket
-- Holds user data for account
CREATE TABLE "user"(
"id" SERIAL PRIMARY KEY,
"username" VARCHAR(255) UNIQUE NOT NULL,
"first_name" VARCHAR(255) NOT NULL,
"last_name" VARCHAR(255) NOT NULL,
"address_1" VARCHAR(255),
"address_2" VARCHAR(255),
"city" VARCHAR(255),
"state" VARCHAR(255),
"country" VARCHAR(255),
"zip" VARCHAR(255),
"phone" VARCHAR(255),
"password" VARCHAR(1000) NOT NULL
);
-- Currently only has 8-ball billards,
-- but an option for other games/activities in future
CREATE TABLE "activities"(
"id" SERIAL PRIMARY KEY,
"activity" VARCHAR(255) NOT NULL
);
-- Have a default one set now, but an option to add different venues in the future
CREATE TABLE "locations"(
"id" SERIAL PRIMARY KEY,
"name" VARCHAR(255) NOT NULL,
"address_1" VARCHAR(255),
"address_2" VARCHAR(255),
"city" VARCHAR(255),
"state" VARCHAR(255),
"country" VARCHAR(255),
"zip" VARCHAR(255)
);
-- Table for storing leagues. Works with two junction tables.
-- "users_leagues" and "teams_leagues" for membership
CREATE TABLE "leagues"(
"id" SERIAL PRIMARY KEY,
"league_name" VARCHAR(255) NOT NULL,
"activity_id" INT DEFAULT 1,
"owner_id" BIGINT NOT NULL,
"authorized_user_id" BIGINT,
"number_of_teams" BIGINT NOT NULL,
"default_location_id" BIGINT DEFAULT 1,
"join_code" VARCHAR(255) UNIQUE
);
-- Table for storing teams. Works with two junction tables.
-- "users_teams" and "teams_leagues" for membership
CREATE TABLE "teams"(
"id" SERIAL PRIMARY KEY,
"team_name" VARCHAR(255) NOT NULL,
"owner_id" BIGINT NOT NULL,
"authorized_user_id" BIGINT,
"join_code" VARCHAR(255)
);
-- Table for storing tournaments. Works with a junction table.
-- "leagues_tournaments" shows which league a tournament belongs
CREATE TABLE "tournaments"(
"id" SERIAL PRIMARY KEY,
"tournament_name" VARCHAR(255) NOT NULL,
"bracket" BOOLEAN NOT NULL,
"num_teams" INT,
"num_matchups" INT,
"playoffs" BOOLEAN DEFAULT FALSE,
"playoff_num" INT DEFAULT NULL,
"league_id" INT NOT NULL,
"complete" BOOLEAN DEFAULT FALSE
);
-- Junction table for showing user membership in a league
CREATE TABLE "users_leagues"(
"id" SERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"league_id" BIGINT NOT NULL
);
-- Junction table for showing user membership on a team
CREATE TABLE "users_teams" (
"id" SERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"team_id" BIGINT NOT NULL
);
-- Junction table for showing team membership in a league
CREATE TABLE "teams_leagues" (
"id" SERIAL PRIMARY KEY,
"team_id" BIGINT NOT NULL,
"league_id" BIGINT NOT NULL
);
-- Table for defining different scoresheet types
CREATE TABLE "scoresheets"(
"id" SERIAL PRIMARY KEY,
"num_teams" INT NOT NULL,
"num_players" INT NOT NULL,
"num_rounds" INT NOT NULL,
"total_num_matches" INT NOT NULL
);
-- Table for the overall matchup between two teams
CREATE TABLE "matchups"(
"id" SERIAL PRIMARY KEY,
"date" DATE NOT NULL,
"location_id" BIGINT DEFAULT 1,
"league_id" BIGINT NOT NULL,
"home_team_id" BIGINT,
"away_team_id" BIGINT,
"home_team_total" NUMERIC DEFAULT 0,
"away_team_total" NUMERIC DEFAULT 0,
"winning_team_id" BIGINT,
"losing_team_id" BIGINT,
"home_lineup_set" BOOLEAN DEFAULT FALSE,
"away_lineup_set" BOOLEAN DEFAULT FALSE,
"confirmed_home" BOOLEAN DEFAULT FALSE,
"confirmed_away" BOOLEAN DEFAULT FALSE,
"confirmed" BOOLEAN DEFAULT FALSE,
"bye" BOOLEAN DEFAULT FALSE,
"tournament_id" BIGINT NOT NULL
);
-- Table for each round in a matchup
CREATE TABLE "rounds"(
"id" SERIAL PRIMARY KEY,
"matchup_id" BIGINT NOT NULL,
"home_score" NUMERIC,
"away_score" NUMERIC,
"home_handicap" NUMERIC,
"away_handicap" NUMERIC,
"home_total" NUMERIC,
"away_total" NUMERIC,
"round_winner" BIGINT,
"round_number" INT NOT NULL
);
-- Table for each game in a round
CREATE TABLE "games"(
"id" SERIAL PRIMARY KEY,
"home_player_id" BIGINT,
"home_player_sub" BOOLEAN,
"home_player_score" VARCHAR(255),
"away_player_id" BIGINT,
"away_player_sub" BOOLEAN,
"away_player_score" VARCHAR(255),
"winner" BIGINT,
"matchup_id" BIGINT NOT NULL,
"round_id" BIGINT NOT NULL,
"game_number" INT NOT NULL
);
-- Junction Table for teams in a matchup
CREATE TABLE "teams_matchups"(
"id" SERIAL PRIMARY KEY,
"team_id" BIGINT NOT NULL,
"matchup_id" BIGINT NOT NULL
);
-- Junction Table for users in a matchup
CREATE TABLE "users_matchups"(
"id" SERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"matchup_id" BIGINT NOT NULL
);
-- Junction Table for scores by user for each game
CREATE TABLE "users_games"(
"id" SERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"game_id" BIGINT NOT NULL,
"score" INT NOT NULL,
"break_run" BOOLEAN DEFAULT FALSE,
"return_run" BOOLEAN DEFAULT FALSE
);