Quicker db init

Signed-off-by: fly <merspieler@airmail.cc>
This commit is contained in:
fly 2021-09-30 09:15:32 +02:00
parent 05892dd003
commit f8e67c3cea
2 changed files with 332 additions and 114 deletions

View file

@ -22,7 +22,9 @@ from common import norm, get_tile_width, get_tile, get_area_name
dbuser= "" dbuser= ""
dbpw = "" dbpw = ""
dbname = "worldbuild" dbname = "worldbuild"
dbhost = "localhost"
force = False force = False
use_sqlfile = True
argc = len(sys.argv) argc = len(sys.argv)
i = 1 i = 1
@ -36,8 +38,13 @@ while i < argc:
elif sys.argv[i] == "-d" or sys.argv[i] == "--database": elif sys.argv[i] == "-d" or sys.argv[i] == "--database":
i += 1 i += 1
dbname = sys.argv[i] dbname = sys.argv[i]
elif sys.argv[i] == "-h" or sys.argv[i] == "--host":
i += 1
dbhost = sys.argv[i]
elif sys.argv[i] == "-f" or sys.argv[i] == "--force": elif sys.argv[i] == "-f" or sys.argv[i] == "--force":
force = True force = True
elif sys.argv[i] == "-N" or sys.argv[i] == "--no-sql":
force = True
elif sys.argv[i] == "-h" or sys.argv[i] == "--help": elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
print("usage: init-db.py [OPTIONS]") print("usage: init-db.py [OPTIONS]")
print("Initializes worldbuild status db") print("Initializes worldbuild status db")
@ -46,7 +53,10 @@ while i < argc:
print(" -d, --database Database to be initialised. Default: worldbuild") print(" -d, --database Database to be initialised. Default: worldbuild")
print(" -p, --password Database password") print(" -p, --password Database password")
print(" -u, --user Database user") print(" -u, --user Database user")
print(" -h, --host Database host")
print(" -f, --force Recreates everything from scratch") print(" -f, --force Recreates everything from scratch")
print(" -N, --no-sql Don't use the sql file and create the structure from scratch")
print(" Only recommended when underlaying tiling structure was changed")
print(" -h, --help Shows this help and exit") print(" -h, --help Shows this help and exit")
sys.exit(0) sys.exit(0)
else: else:
@ -62,6 +72,9 @@ if dbpw == "":
print("ERROR: No database password given") print("ERROR: No database password given")
sys.exit(1) sys.exit(1)
#try:
if use_sqlfile:
else:
states = [ states = [
"pending", "pending",
"started", "started",
@ -70,9 +83,7 @@ states = [
"skip", "skip",
"rebuild" "rebuild"
] ]
db = pymysql.connect(dbhost, dbuser, dbpw, dbname)
#try:
db = pymysql.connect("localhost", dbuser, dbpw, dbname)
cursor = db.cursor() cursor = db.cursor()
@ -147,60 +158,31 @@ while lat < 83:
lon += get_tile_width(lat) lon += get_tile_width(lat)
lat += 0.125 lat += 0.125
sql = "CREATE TABLE `auth` (`id` int(11) NOT NULL AUTO_INCREMENT, `token` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `token` (`token`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4"
cursor.execute(sql)
db.commit() db.commit()
# Get states # Always create triggers via script cause there can be a user name conflict on import.
#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;
print("Creating triggers") print("Creating triggers")
# Trigger for updating secondLevel on tile update # Trigger for updating secondLevel on tile update
sql = ('CREATE OR REPLACE TRIGGER update_second_level ' sql = ('CREATE OR REPLACE TRIGGER update_second_level '
'AFTER UPDATE ON tile ' 'AFTER UPDATE ON tile '
'FOR EACH ROW ' 'FOR EACH ROW '
'BEGIN ' 'BEGIN UPDATE secondLevel SET status_id = (SELECT status_id FROM tile JOIN status ON status_id = status.id WHERE tile.parent_id = NEW.parent_id ORDER BY priority ASC LIMIT 1) WHERE id = NEW.parent_id; END')
'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 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; '
'ELSEIF (SELECT COUNT(status_id) FROM tile WHERE tile.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "done")) > 0 THEN '
'UPDATE secondLevel SET status_id = (SELECT id FROM status WHERE name = "done") 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; ')
cursor.execute(sql) cursor.execute(sql)
# Trigger for updating topLevel on secondLevel update # Trigger for updating topLevel on secondLevel update
sql = ('CREATE OR REPLACE TRIGGER update_top_level ' sql = ('CREATE OR REPLACE TRIGGER update_top_level '
'AFTER UPDATE ON secondLevel ' 'AFTER UPDATE ON secondLevel '
'FOR EACH ROW ' 'FOR EACH ROW '
'BEGIN ' 'BEGIN UPDATE topLevel SET status_id = (SELECT status_id FROM secondLevel JOIN status ON status_id = status.id WHERE secondLevel.parent_id = NEW.parent_id ORDER BY priority ASC LIMIT 1) WHERE id = NEW.parent_id; END')
'IF (SELECT COUNT(status_id) FROM secondLevel WHERE secondLevel.parent_id = NEW.parent_id) = (SELECT COUNT(status_id) FROM secondLevel WHERE secondLevel.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "packaged")) THEN '
'UPDATE topLevel SET status_id = (SELECT id FROM status WHERE name = "packaged") WHERE id = NEW.parent_id; '
'ELSEIF (SELECT COUNT(status_id) FROM secondLevel WHERE secondLevel.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "started")) > 0 THEN '
'UPDATE topLevel SET status_id = (SELECT id FROM status WHERE name = "started") WHERE id = NEW.parent_id; '
'ELSEIF (SELECT COUNT(status_id) FROM secondLevel WHERE secondLevel.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "rebuild")) > 0 THEN '
'UPDATE topLevel SET status_id = (SELECT id FROM status WHERE name = "rebuild") WHERE id = NEW.parent_id; '
'ELSEIF (SELECT COUNT(status_id) FROM secondLevel WHERE secondLevel.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "skip")) > 0 THEN '
'UPDATE topLevel SET status_id = (SELECT id FROM status WHERE name = "skip") WHERE id = NEW.parent_id; '
'ELSEIF (SELECT COUNT(status_id) FROM secondLevel WHERE secondLevel.parent_id = NEW.parent_id AND status_id = (SELECT id FROM status WHERE name = "done")) > 0 THEN '
'UPDATE topLevel SET status_id = (SELECT id FROM status WHERE name = "done") WHERE id = NEW.parent_id; '
'ELSE '
'UPDATE topLevel SET status_id = (SELECT id FROM status WHERE name = "pending") WHERE id = NEW.parent_id; '
'END IF; '
'END; ')
cursor.execute(sql)
sql = "CREATE TABLE `auth` (`id` int(11) NOT NULL AUTO_INCREMENT, `token` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `token` (`token`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4"
cursor.execute(sql) cursor.execute(sql)
db.commit() db.commit()
#except: #except:
# db.rollback()
# print("Failed to setup database...") # print("Failed to setup database...")
# db.rollback()
#finally: #finally:
db.close() db.close()

236
worldbuild-base.sql Normal file

File diff suppressed because one or more lines are too long