fix channel overwrite creation and constraints

This commit is contained in:
Luna 2019-07-23 15:40:23 -03:00
parent 39fd7577b8
commit db00724379
3 changed files with 19 additions and 6 deletions

View File

@ -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

View File

@ -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'],

View File

@ -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);