Skip to content

Commit

Permalink
Add migration to add group creators as owners
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Oct 18, 2024
1 parent 05038ec commit 5a0c7c9
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions h/migrations/versions/59f42b6a0cd9_add_group_creators_as_owners.py
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

0 comments on commit 5a0c7c9

Please sign in to comment.