Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions alembic_osm/versions/9221408912dd_add_user_role_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@

import sqlalchemy as sa
from alembic import op
from sqlalchemy import text
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "9221408912dd"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

# create_type=False so the type is NOT implicitly created/dropped by the table
# DDL. We manage its lifecycle explicitly with checkfirst=True so the migration
# is idempotent whether or not the type already exists.
workspace_role = postgresql.ENUM(
"lead", "validator", "contributor", name="workspace_role", create_type=False
)


def upgrade() -> None:
workspace_role.create(op.get_bind(), checkfirst=True)
op.create_unique_constraint("auth_uid_unique", "users", ["auth_uid"])
op.create_table(
"user_workspace_roles",
sa.Column("user_auth_uid", sa.String(), nullable=False),
sa.Column("workspace_id", sa.BigInteger(), nullable=False),
sa.Column(
"role",
sa.Enum("lead", "validator", "contributor", name="workspace_role"),
nullable=False,
),
sa.Column("role", workspace_role, nullable=False),
sa.ForeignKeyConstraint(["user_auth_uid"], ["users.auth_uid"]),
sa.PrimaryKeyConstraint("user_auth_uid", "workspace_id"),
)


def downgrade() -> None:
op.drop_table("user_workspace_roles")
op.execute(text("DROP TYPE workspace_role"))
workspace_role.drop(op.get_bind(), checkfirst=True)
op.drop_constraint("auth_uid_unique", "users", type_="unique")
Loading