From 5a0c7c91b7b886ca79db71144bff73bcb982fef8 Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Fri, 18 Oct 2024 16:39:45 +0100 Subject: [PATCH] Add migration to add group creators as owners --- ...f42b6a0cd9_add_group_creators_as_owners.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 h/migrations/versions/59f42b6a0cd9_add_group_creators_as_owners.py diff --git a/h/migrations/versions/59f42b6a0cd9_add_group_creators_as_owners.py b/h/migrations/versions/59f42b6a0cd9_add_group_creators_as_owners.py new file mode 100644 index 00000000000..f9902a45cc5 --- /dev/null +++ b/h/migrations/versions/59f42b6a0cd9_add_group_creators_as_owners.py @@ -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