-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OIDC: Reference group by FK, set workspace attribute to False (#974)
- Loading branch information
Showing
3 changed files
with
99 additions
and
20 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
mwdb/model/migrations/versions/72a94f88d2b6_oidc_group_referenced_by_id_instead_of_.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,78 @@ | ||
"""OIDC group referenced by id instead of name + convert to non-workspace | ||
Revision ID: 72a94f88d2b6 | ||
Revises: 6fc42e070495 | ||
Create Date: 2024-08-20 13:29:36.839985 | ||
""" | ||
import logging | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "72a94f88d2b6" | ||
down_revision = "6fc42e070495" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
group_helper = sa.Table( | ||
"group", | ||
sa.MetaData(), | ||
sa.Column("id", sa.Integer()), | ||
sa.Column("name", sa.String(32)), | ||
sa.Column("private", sa.Boolean()), | ||
sa.Column("default", sa.Boolean()), | ||
sa.Column("workspace", sa.Boolean()), | ||
) | ||
|
||
provider_helper = sa.Table( | ||
"openid_provider", | ||
sa.MetaData(), | ||
sa.Column("id", sa.Integer()), | ||
sa.Column("name", sa.String(64)), | ||
sa.Column("group_id", sa.Integer()), | ||
) | ||
|
||
logger = logging.getLogger("alembic") | ||
|
||
|
||
def group_name_from_provider_name(provider_name): | ||
return ("OpenID_" + provider_name)[:32] | ||
|
||
|
||
def upgrade(): | ||
connection = op.get_bind() | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("openid_provider", sa.Column("group_id", sa.Integer(), nullable=True)) | ||
op.create_foreign_key(None, "openid_provider", "group", ["group_id"], ["id"]) | ||
|
||
# Migrate existing providers | ||
for provider in connection.execute(provider_helper.select()): | ||
group_name = group_name_from_provider_name(provider.name) | ||
group = connection.execute( | ||
group_helper.select().where(group_helper.c.name == group_name) | ||
).first() | ||
connection.execute( | ||
group_helper.update() | ||
.where(group_helper.c.name == group_name) | ||
.values(workspace=False) | ||
) | ||
connection.execute( | ||
provider_helper.update() | ||
.where(provider_helper.c.id == provider.id) | ||
.values(group_id=group.id) | ||
) | ||
|
||
# Set group_id as non-nullable | ||
op.alter_column( | ||
"openid_provider", "group_id", existing_type=sa.INTEGER(), nullable=False | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(None, "openid_provider", type_="foreignkey") | ||
op.drop_column("openid_provider", "group_id") | ||
# ### end Alembic commands ### |
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
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