-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to add group creators as owners
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
h/migrations/versions/59f42b6a0cd9_add_group_creators_as_owners.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Add existing group creators as owners.""" | ||
|
||
from alembic import op | ||
from sqlalchemy import Column, ForeignKey, Integer, MetaData, Table, select, update | ||
from sqlalchemy.dialects.postgresql import JSONB | ||
|
||
metadata = MetaData() | ||
|
||
|
||
user_table = Table("user", metadata, Column("id", Integer, primary_key=True)) | ||
|
||
|
||
group_table = Table( | ||
"group", | ||
metadata, | ||
Column("id", Integer, primary_key=True), | ||
Column("creator_id", Integer, ForeignKey("user.id")), | ||
) | ||
|
||
|
||
user_group_table = Table( | ||
"user_group", | ||
metadata, | ||
Column("id", Integer, primary_key=True), | ||
Column("user_id", Integer, ForeignKey("user.id")), | ||
Column("group_id", Integer, ForeignKey("group.id")), | ||
Column("roles", JSONB), | ||
) | ||
|
||
|
||
revision = "59f42b6a0cd9" | ||
down_revision = "1975edef158d" | ||
|
||
|
||
def upgrade(): | ||
op.execute( | ||
update(user_group_table) | ||
.where( | ||
user_group_table.c.id.in_( | ||
select(user_group_table.c.id) | ||
.where(user_group_table.c.group_id == group_table.c.id) | ||
.where(user_group_table.c.user_id == user_table.c.id) | ||
.where(user_table.c.id == group_table.c.creator_id) | ||
) | ||
) | ||
.values(roles=["owner"]) | ||
) | ||
|
||
|
||
def downgrade(): | ||
pass |