migration: some fixes here and there

- 0_base.sql: make it rerunnable by dropping hardcoded constraints
This commit is contained in:
Luna 2019-04-03 23:53:06 -03:00
parent 1617c87f03
commit e03dada1a7
2 changed files with 10 additions and 15 deletions

View File

@ -101,11 +101,13 @@ async def _ensure_changelog(app, ctx):
# NOTE: this is a migration breakage, # NOTE: this is a migration breakage,
# only applying to databases that had their first migration # only applying to databases that had their first migration
# before 4 april 2019 (more on BREAK) # before 4 april 2019 (more on BREAK)
# if migration_log is empty, just assume this is new
first = await app.db.fetchval(""" first = await app.db.fetchval("""
SELECT apply_ts FROM migration_log SELECT apply_ts FROM migration_log
ORDER BY apply_ts ASC ORDER BY apply_ts ASC
LIMIT 1 LIMIT 1
""") """) or BREAK
if first < BREAK: if first < BREAK:
log.info('deleting migration_log due to migration structure change') log.info('deleting migration_log due to migration structure change')
await app.db.execute("DROP TABLE migration_log") await app.db.execute("DROP TABLE migration_log")
@ -170,7 +172,7 @@ async def _check_base(app) -> bool:
await app.db.execute(f""" await app.db.execute(f"""
SELECT * FROM {table} LIMIT 0 SELECT * FROM {table} LIMIT 0
""") """)
except asyncpg.DuplicateTableError: except asyncpg.UndefinedTableError:
return False return False
return True return True
@ -204,7 +206,7 @@ async def migrate_cmd(app, _args):
if has_base: if has_base:
await _insert_log(app, 0, 'migration setup (from existing)') await _insert_log(app, 0, 'migration setup (from existing)')
else: else:
await apply_migration(app, 0) await apply_migration(app, ctx.scripts[0])
# after that check the current local_change # after that check the current local_change
# and the latest migration to be run # and the latest migration to be run

View File

@ -27,18 +27,8 @@ CREATE TABLE IF NOT EXISTS user_conn_apps (
name text NOT NULL name text NOT NULL
); );
INSERT INTO user_conn_apps (id, name) VALUES (0, 'Twitch'); -- there was a chain of INSERTs here with hardcoded names and stuff.
INSERT INTO user_conn_apps (id, name) VALUES (1, 'Youtube'); -- removed it because we aren't in the best business of hardcoding.
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');
CREATE TABLE IF NOT EXISTS instance_invites ( CREATE TABLE IF NOT EXISTS instance_invites (
code text PRIMARY KEY, code text PRIMARY KEY,
@ -607,6 +597,7 @@ CREATE TABLE IF NOT EXISTS channel_overwrites (
-- columns in private keys can't have NULL values, -- columns in private keys can't have NULL values,
-- so instead we use a custom constraint with UNIQUE -- 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 ALTER TABLE channel_overwrites ADD CONSTRAINT channel_overwrites_uniq
UNIQUE (channel_id, target_role, target_user); UNIQUE (channel_id, target_role, target_user);
@ -688,6 +679,8 @@ CREATE TABLE IF NOT EXISTS message_reactions (
emoji_text text 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 ALTER TABLE message_reactions ADD CONSTRAINT message_reactions_main_uniq
UNIQUE (message_id, user_id, emoji_id, emoji_text); UNIQUE (message_id, user_id, emoji_id, emoji_text);