-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlebase.sql
113 lines (87 loc) · 2.57 KB
/
lebase.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
-- Tabell over løp
CREATE TABLE race (
race SERIAL PRIMARY KEY,
name VARCHAR(64),
"date" DATE,
procyclingstats_slug VARCHAR(128),
UNIQUE (procyclingstats_slug)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON race TO lebase;
GRANT SELECT, UPDATE ON race_race_seq TO lebase;
-- Etappetype
CREATE TYPE stage_type AS ENUM ('NORMAL', 'ITT', 'TTT');
-- Tabell over etapper
CREATE TABLE stage (
stage SERIAL PRIMARY KEY,
race INTEGER REFERENCES race ON DELETE CASCADE
"date" DATE NOT NULL,
"start" TIME,
"finish" TIME,
"type" stage_type,
name VARCHAR(64) NOT NULL,
length REAL,
prologue BOOLEAN,
procyclingstats_slug VARCHAR(128),
UNIQUE (procyclingstats_slug)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON stage TO lebase;
GRANT SELECT, UPDATE ON stage_stage_seq TO lebase;
-- Tabell over ryttere
CREATE TABLE rider (
rider SERIAL PRIMARY KEY,
name VARCHAR(64),
birth_date DATE,
procyclingstats_slug VARCHAR(64),
UNIQUE (procyclingstats_slug)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON rider TO lebase;
GRANT SELECT, UPDATE ON rider_rider_seq TO lebase;
-- Linjetype
CREATE TYPE line_type AS ENUM ('FINISH', 'SPRINT', 'MOUNTAIN');
CREATE TABLE line (
line SERIAL PRIMARY KEY,
stage INTEGER REFERENCES stage NOT NULL ON DELETE CASCADE,
"type" line_type NOT NULL,
name VARCHAR(64),
length REAL,
category INTEGER
);
CREATE UNIQUE INDEX one_finish ON line (stage) WHERE type = 'FINISH';
GRANT SELECT, UPDATE, INSERT, DELETE ON line TO lebase;
GRANT SELECT, UPDATE ON line_line_seq TO lebase;
-- Tabell over linjekryssinger
CREATE TABLE rider_line (
rider INTEGER REFERENCES rider NOT NULL,
line INTEGER REFERENCES line NOT NULL ON DELETE CASCADE,
"number" INTEGER NOT NULL,
time INTERVAL,
UNIQUE (rider, line)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON rider_line TO lebase;
-- Tabell over lag
CREATE TABLE team (
team SERIAL PRIMARY KEY,
name VARCHAR(64),
procyclingstats_slug VARCHAR(64) UNIQUE,
UNIQUE (name)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON team TO lebase;
GRANT SELECT, UPDATE ON team_team_seq TO lebase;
-- Tabell over målganger for TTT
CREATE TABLE team_line (
team INTEGER REFERENCES team,
line INTEGER REFERENCES line,
"number" INTEGER NOT NULL,
time INTERVAL,
UNIQUE (team, line)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON team_line TO lebase;
-- Tabell over deltagelser
CREATE TABLE rider_race (
rider INTEGER REFERENCES rider,
race INTEGER REFERENCES race ON DELETE CASCADE,
team INTEGER REFERENCES team,
-- FIXME: Ensure that this exists when inserting into rider_line
UNIQUE (rider, race)
);
GRANT SELECT, UPDATE, INSERT, DELETE ON rider_race TO lebase;