-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.py
executable file
·39 lines (35 loc) · 2.53 KB
/
create.py
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
#!/usr/bin/env python
from deck_mysql import DeckDB
import sys
print "You didn't want this, it drops the whole database"
sys.exit(1)
def main(args):
tables = [
('tournaments', ['CREATE table `tournaments` (tournamentid INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) UNIQUE, url VARCHAR(100), decklisturl VARCHAR(100), password VARCHAR(100), rounds INT, pairings BOOLEAN, team BOOLEAN)']),
("players", ["CREATE TABLE `players` (playerid INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), country VARCHAR(100), tournamentid INT, decklistid VARCHAR(100), CONSTRAINT fkidtourp FOREIGN KEY (tournamentid) REFERENCES tournaments(tournamentid) ON DELETE CASCADE ON UPDATE CASCADE, UNIQUE KEY player_key (tournamentid, name, country))"]),
('round', ["CREATE TABLE `round` (roundnum INT, tournamentid INT, CONSTRAINT fkidtourr FOREIGN KEY (tournamentid) REFERENCES tournaments(tournamentid) ON DELETE CASCADE ON UPDATE CASCADE)"]),
('seatings', ["CREATE TABLE `seatings` (playerid INT UNIQUE, buildtable INT, tournamentid INT, INDEX ididx(playerid), CONSTRAINT fkidseat FOREIGN KEY (playerid) REFERENCES players(playerid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fkidtours FOREIGN KEY (tournamentid) REFERENCES tournaments(tournamentid) ON DELETE CASCADE ON UPDATE CASCADE)"]),
('pairings', ["CREATE TABLE `pairings` (playerid INT, round INT, score INT, tablenum INT, tournamentid INT, UNIQUE KEY pairings_key (tournamentid, playerid, round), INDEX ididx(playerid), CONSTRAINT fkidpair FOREIGN KEY (playerid) REFERENCES players(playerid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fkidtoura FOREIGN KEY (tournamentid) REFERENCES tournaments(tournamentid) ON DELETE CASCADE ON UPDATE CASCADE)"]),
('deckchecks', ["CREATE TABLE `deckchecks` (playerid INT, teamplayer INT, tournamentid INT, round INT, UNIQUE KEY checks_key (playerid, teamplayer, tournamentid, round), INDEX ididx(playerid), CONSTRAINT fkiddeck FOREIGN KEY (playerid) REFERENCES players(playerid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fkidtourd FOREIGN KEY (tournamentid) REFERENCES tournaments(tournamentid) ON DELETE CASCADE ON UPDATE CASCADE)"]),
]
with DeckDB() as db:
for (t, _) in reversed(tables):
with db.cursor() as cur:
try:
print "DROP TABLE `%s`" % t
cur.execute("DROP TABLE `%s`" % t)
except:
pass
db.commit()
with DeckDB() as db:
for (_, queries) in tables:
for q in queries:
with db.cursor() as cur:
try:
print q
cur.execute(q)
except:
pass
db.commit()
if __name__ == "__main__":
main(sys.argv[1:])