From e03dada1a7847160b0b0ae65ad0a7f94891fbe74 Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 3 Apr 2019 23:53:06 -0300 Subject: [PATCH] migration: some fixes here and there - 0_base.sql: make it rerunnable by dropping hardcoded constraints --- manage/cmd/migration/command.py | 8 +++++--- manage/cmd/migration/scripts/0_base.sql | 17 +++++------------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/manage/cmd/migration/command.py b/manage/cmd/migration/command.py index b33aa16..e769b6c 100644 --- a/manage/cmd/migration/command.py +++ b/manage/cmd/migration/command.py @@ -101,11 +101,13 @@ async def _ensure_changelog(app, ctx): # NOTE: this is a migration breakage, # only applying to databases that had their first migration # before 4 april 2019 (more on BREAK) + + # if migration_log is empty, just assume this is new first = await app.db.fetchval(""" SELECT apply_ts FROM migration_log ORDER BY apply_ts ASC LIMIT 1 - """) + """) or BREAK if first < BREAK: log.info('deleting migration_log due to migration structure change') await app.db.execute("DROP TABLE migration_log") @@ -170,7 +172,7 @@ async def _check_base(app) -> bool: await app.db.execute(f""" SELECT * FROM {table} LIMIT 0 """) - except asyncpg.DuplicateTableError: + except asyncpg.UndefinedTableError: return False return True @@ -204,7 +206,7 @@ async def migrate_cmd(app, _args): if has_base: await _insert_log(app, 0, 'migration setup (from existing)') else: - await apply_migration(app, 0) + await apply_migration(app, ctx.scripts[0]) # after that check the current local_change # and the latest migration to be run diff --git a/manage/cmd/migration/scripts/0_base.sql b/manage/cmd/migration/scripts/0_base.sql index 4c0941f..d98c201 100644 --- a/manage/cmd/migration/scripts/0_base.sql +++ b/manage/cmd/migration/scripts/0_base.sql @@ -27,18 +27,8 @@ CREATE TABLE IF NOT EXISTS user_conn_apps ( name text NOT NULL ); -INSERT INTO user_conn_apps (id, name) VALUES (0, 'Twitch'); -INSERT INTO user_conn_apps (id, name) VALUES (1, 'Youtube'); -INSERT INTO user_conn_apps (id, name) VALUES (2, 'Steam'); -INSERT INTO user_conn_apps (id, name) VALUES (3, 'Reddit'); -INSERT INTO user_conn_apps (id, name) VALUES (4, 'Facebook'); -INSERT INTO user_conn_apps (id, name) VALUES (5, 'Twitter'); -INSERT INTO user_conn_apps (id, name) VALUES (6, 'Spotify'); -INSERT INTO user_conn_apps (id, name) VALUES (7, 'XBOX'); -INSERT INTO user_conn_apps (id, name) VALUES (8, 'Battle.net'); -INSERT INTO user_conn_apps (id, name) VALUES (9, 'Skype'); -INSERT INTO user_conn_apps (id, name) VALUES (10, 'League of Legends'); - +-- there was a chain of INSERTs here with hardcoded names and stuff. +-- removed it because we aren't in the best business of hardcoding. CREATE TABLE IF NOT EXISTS instance_invites ( code text PRIMARY KEY, @@ -607,6 +597,7 @@ CREATE TABLE IF NOT EXISTS channel_overwrites ( -- columns in private keys can't have NULL values, -- so instead we use a custom constraint with UNIQUE +ALTER TABLE channel_overwrites DROP CONSTRAINT IF EXISTS channel_overwrites_uniq; ALTER TABLE channel_overwrites ADD CONSTRAINT channel_overwrites_uniq UNIQUE (channel_id, target_role, target_user); @@ -688,6 +679,8 @@ CREATE TABLE IF NOT EXISTS message_reactions ( emoji_text text ); +-- unique constraint over multiple columns instead of a primary key +ALTER TABLE message_reactions DROP CONSTRAINT IF EXISTS message_reactions_main_uniq; ALTER TABLE message_reactions ADD CONSTRAINT message_reactions_main_uniq UNIQUE (message_id, user_id, emoji_id, emoji_text);