From 914d77a24bfa414612684367a6d0a5ac1d612de7 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Wed, 30 Jun 2021 03:06:58 -0400 Subject: [PATCH] Tweak database upgrade code style --- matrix_puppeteer_line/db/upgrade.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/matrix_puppeteer_line/db/upgrade.py b/matrix_puppeteer_line/db/upgrade.py index ac98ee5..5cd7f9a 100644 --- a/matrix_puppeteer_line/db/upgrade.py +++ b/matrix_puppeteer_line/db/upgrade.py @@ -72,20 +72,22 @@ async def upgrade_media(conn: Connection) -> None: @upgrade_table.register(description="Helpful table constraints") async def upgrade_table_constraints(conn: Connection) -> None: - constraint_name = "portal_mxid_key" + table_name = "portal" + constraint_name = f"{table_name}_mxid_key" q = ( "SELECT EXISTS(SELECT FROM information_schema.constraint_table_usage " - f"WHERE table_name='portal' AND constraint_name='{constraint_name}')") - has_unique_mxid = await conn.fetchval(q) - if not has_unique_mxid: - await conn.execute(f"ALTER TABLE portal ADD CONSTRAINT {constraint_name} UNIQUE(mxid)") + f"WHERE table_name='{table_name}' AND constraint_name='{constraint_name}')") + has_constraint = await conn.fetchval(q) + if not has_constraint: + await conn.execute(f"ALTER TABLE {table_name} ADD CONSTRAINT {constraint_name} UNIQUE(mxid)") - constraint_name = "message_chat_id_fkey" + table_name = "message" + constraint_name = f"{table_name}_chat_id_fkey" q = ( "SELECT EXISTS(SELECT FROM information_schema.table_constraints " - f"WHERE table_name='message' AND constraint_name='{constraint_name}')") - has_fkey = await conn.fetchval(q) - if not has_fkey: + f"WHERE table_name='{table_name}' AND constraint_name='{constraint_name}')") + has_constraint = await conn.fetchval(q) + if not has_constraint: await conn.execute( - f"ALTER TABLE message ADD CONSTRAINT {constraint_name} " + f"ALTER TABLE {table_name} ADD CONSTRAINT {constraint_name} " "FOREIGN KEY (chat_id) " "REFERENCES portal (chat_id) " "ON DELETE CASCADE")