From db00724379cb195d5a79ff14ae43d983758e6b7d Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 23 Jul 2019 15:40:23 -0300 Subject: [PATCH] fix channel overwrite creation and constraints --- litecord/blueprints/channels.py | 11 +++++++---- litecord/storage.py | 4 ++-- .../scripts/2_fix_chan_overwrites_constraint.sql | 10 ++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 manage/cmd/migration/scripts/2_fix_chan_overwrites_constraint.sql diff --git a/litecord/blueprints/channels.py b/litecord/blueprints/channels.py index bfb08dc..1dfcee9 100644 --- a/litecord/blueprints/channels.py +++ b/litecord/blueprints/channels.py @@ -278,19 +278,22 @@ async def _mass_chan_update(guild_id, channel_ids: List[Optional[int]]): async def _process_overwrites(channel_id: int, overwrites: list): for overwrite in overwrites: - # 0 for user overwrite, 1 for role overwrite - target_type = 0 if overwrite['type'] == 'user' else 1 + # 0 for member overwrite, 1 for role overwrite + target_type = 0 if overwrite['type'] == 'member' else 1 target_role = None if target_type == 0 else overwrite['id'] target_user = overwrite['id'] if target_type == 0 else None + col_name = 'target_user' if target_type == 0 else 'target_role' + constraint_name = f'channel_overwrites_target_{col_name}' + await app.db.execute( - """ + f""" INSERT INTO channel_overwrites (channel_id, target_type, target_role, target_user, allow, deny) VALUES ($1, $2, $3, $4, $5, $6) - ON CONFLICT ON CONSTRAINT channel_overwrites_uniq + ON CONFLICT ON CONSTRAINT {constraint_name} DO UPDATE SET allow = $5, deny = $6 diff --git a/litecord/storage.py b/litecord/storage.py index 79d07dd..58c7bd6 100644 --- a/litecord/storage.py +++ b/litecord/storage.py @@ -403,9 +403,9 @@ class Storage: drow = dict(row) target_type = drow['target_type'] - drow['type'] = 'user' if target_type == 0 else 'role' + drow['type'] = 'member' if target_type == 0 else 'role' - # if type is 0, the overwrite is for a user + # if type is 0, the overwrite is for a member # if type is 1, the overwrite is for a role drow['id'] = { 0: drow['target_user'], diff --git a/manage/cmd/migration/scripts/2_fix_chan_overwrites_constraint.sql b/manage/cmd/migration/scripts/2_fix_chan_overwrites_constraint.sql new file mode 100644 index 0000000..7816d00 --- /dev/null +++ b/manage/cmd/migration/scripts/2_fix_chan_overwrites_constraint.sql @@ -0,0 +1,10 @@ +ALTER TABLE channel_overwrites + DROP CONSTRAINT IF EXISTS channel_overwrites_uniq; + +ALTER TABLE channel_overwrites + ADD CONSTRAINT channel_overwrites_target_role_uniq + UNIQUE (channel_id, target_role); + +ALTER TABLE channel_overwrites + ADD CONSTRAINT channel_overwrites_target_user_uniq + UNIQUE (channel_id, target_user);