fix(migration): just use two different commands

This commit is contained in:
Iain Learmonth 2023-05-31 15:52:57 +01:00
parent 1d7a8ce1a0
commit f91f7e5274

View file

@ -16,18 +16,20 @@ depends_on = None
def upgrade(): def upgrade():
# Add SQL here
if op.get_context().dialect.name == 'postgresql': if op.get_context().dialect.name == 'postgresql':
random_string = "md5(to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS.US'))" sql = text("""INSERT INTO pool VALUES (
on_conflict = " ON CONFLICT (id) DO NOTHING" -1, 'Hot spares (reserve)', NOW(), NOW(), NULL, 'hotspare',
md5(to_char(NOW(), 'YYYY-MM-DD HH24:MI:SS.US')), NULL) ON CONFLICT (id) DO NOTHING;""")
elif op.get_context().dialect == 'sqlite':
sql = text(f"""INSERT OR IGNORE INTO pool VALUES (
-1, 'Hot spares (reserve)', datetime('now'), datetime('now'), NULL, 'hotspare',
CAST(RANDOM() AS TEXT), NULL);""")
else: else:
random_string = "CAST(RANDOM() AS TEXT)" raise RuntimeError("Using an unsupported engine, only SQLite or PostgreSQL are supported. "
on_conflict = "" "You could do this migration manually.")
sql = text(f"""INSERT INTO pool VALUES (
-1, 'Hot spares (reserve)', NOW(), NOW(), NULL, 'hotspare',
{random_string}, NULL){on_conflict};""")
op.execute(sql) op.execute(sql)
def downgrade(): def downgrade():
# SQL to undo the changes # SQL to undo the changes
sql = text("""DELETE FROM pool WHERE id = -1;""") sql = text("""DELETE FROM pool WHERE id = -1;""")