Mercurial > hg > rlgallery-misc
comparison py/setupdb.py @ 0:5ba2123d2c20
Put this project under version control, finally.
Scripts for rlgallery.org, using a PostgreSQL backend. The recorder
system is in py/, CGI scripts are in web/.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Wed, 25 Jul 2012 21:59:42 -0700 |
parents | |
children | e8f3b7994d88 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:5ba2123d2c20 |
---|---|
1 #!/usr/bin/python | |
2 # setupdb.py: initializes the database tables used by the rlg system. | |
3 | |
4 import rlgalldb as rlgall | |
5 import psycopg2 | |
6 | |
7 webuser = "webserver" | |
8 allowquery = "GRANT SELECT ON {0} TO " + webuser + ";" | |
9 | |
10 dbconn = psycopg2.connect("dbname=rlg") | |
11 dbcur = dbconn.cursor() | |
12 | |
13 dbcur.execute("CREATE TABLE games ( gname varchar(20), offbytes int );") | |
14 dbcur.execute("CREATE TABLE players (pname varchar(20) PRIMARY KEY);") | |
15 dbconn.commit() | |
16 | |
17 for game in rlgall.gamelist: | |
18 dbcur.execute("INSERT INTO games VALUES (%s, %s);", (game.uname, 0)) | |
19 | |
20 createquery = "CREATE TABLE " + game.uname + " ( " | |
21 for i, field in enumerate(game.sqltypes.keys()): | |
22 createquery += "{0} {1}".format(field, game.sqltypes[field]) | |
23 if field == "name": | |
24 createquery += " REFERENCES players(pname)" | |
25 if i == len(game.sqltypes) - 1: | |
26 createquery += " )" | |
27 else: | |
28 createquery += ", " | |
29 createquery += ";" | |
30 #print createquery | |
31 dbcur.execute(createquery) | |
32 dbcur.execute(allowquery.format(game.uname)) | |
33 dbconn.commit() | |
34 | |
35 dbcur.close() | |
36 dbconn.close() | |
37 exit() |