Initialisation script for database

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2020-03-12 06:29:04 +00:00
parent f6b92b31c0
commit d23f982021

123
init-db.py Executable file
View file

@ -0,0 +1,123 @@
#! /usr/bin/python3
import pymysql
from common import norm, get_tile_width, get_tile, get_area_name
# TODO set default to false
force = True
states = [
"pending",
"started",
"done",
"packaged",
"skip",
"rebuild"
]
#try:
db = pymysql.connect("localhost", dbuser, dbpw, "worldbuild")
cursor = db.cursor()
if force:
cursor.execute("DROP TABLE tile")
cursor.execute("DROP TABLE secondLevel")
cursor.execute("DROP TABLE topLevel")
cursor.execute("DROP TABLE status")
cursor.execute("CREATE TABLE IF NOT EXISTS status (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(15) UNIQUE NOT NULL);")
cursor.execute("CREATE TABLE IF NOT EXISTS topLevel (id INT PRIMARY KEY AUTO_INCREMENT, name CHAR(7) UNIQUE NOT NULL, status_id INT NOT NULL, FOREIGN KEY (status_id) REFERENCES status(id));")
cursor.execute("CREATE TABLE IF NOT EXISTS secondLevel (id INT PRIMARY KEY AUTO_INCREMENT, name CHAR(7) UNIQUE NOT NULL, status_id INT NOT NULL, parent_id INT NOT NULL, FOREIGN KEY (status_id) REFERENCES status(id), FOREIGN KEY (parent_id) REFERENCES topLevel(id));")
cursor.execute("CREATE TABLE IF NOT EXISTS tile (id INT PRIMARY KEY AUTO_INCREMENT, status_id INT NOT NULL, parent_id INT NOT NULL, FOREIGN KEY (status_id) REFERENCES status(id), FOREIGN KEY (parent_id) REFERENCES secondLevel(id));")
for state in states:
sql = "INSERT INTO status(name) VALUES ('" + state + "')"
cursor.execute(sql)
sql = "SELECT id FROM status WHERE name='pending'"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
sid = str(row[0])
print("Creating tile groups")
ii = -9
while ii < 9:
i = ii * 10
jj = -18
while jj < 18:
j = jj * 10
major = get_area_name(i, j)
sql = "INSERT INTO topLevel (name, status_id) VALUES ('" + major + "', " + sid + ");"
cursor.execute(sql)
sql = "SELECT id FROM topLevel WHERE name='" + major + "'"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
major_id = str(row[0])
for k in range(0, 10):
iii = i
for l in range(0, 10):
minor = get_area_name(iii, j)
sql = "INSERT INTO secondLevel (parent_id, name, status_id) VALUES (" + major_id + ", '" + minor + "', " + sid + ")"
cursor.execute(sql)
iii += 1
j += 1
jj += 1
ii += 1
print("Creating tiles")
lat = -90
while lat < 90:
lon = -180
while lon < 180:
tile = get_tile(lat, lon)
parent = get_area_name(int(lat), int(lon))
sql="SELECT id FROM secondLevel WHERE name='" + parent + "'"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
pid = row[0]
sql = "INSERT INTO tile (id, parent_id, status_id) VALUES (" + str(tile) + ", " + str(pid) + ", " + sid + ")"
cursor.execute(sql)
lon += get_tile_width(lat)
lat += 0.125
db.commit()
# TODO create trigger
# Get states
#SELECT status.name AS state, COUNT(status.name) AS occ FROM tile INNER JOIN status ON tile.status_id = status.id WHERE tile.parent_id = 400 GROUP BY status.name;
'''
DELIMITER //
CREATE OR REPLACE TRIGGER update_second_level
AFTER UPDATE ON tile
FOR EACH ROW
BEGIN
IF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id) = (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "packaged")) THEN
UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "packaged") WHERE id = NEW.parent_id;
ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id) = (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "done")) THEN
UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "done") WHERE id = NEW.parent_id;
ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "started")) > 0 THEN
UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "started") WHERE id = NEW.parent_id;
ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "rebuild")) > 0 THEN
UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "rebuild") WHERE id = NEW.parent_id;
ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "skip")) > 0 THEN
UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "skip") WHERE id = NEW.parent_id;
ELSE
UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "pending") WHERE id = NEW.parent_id;
END IF;
END; //
DELIMITER ;
'''
#except:
# db.rollback()
# print("Failed to setup database...")
#finally:
db.close()