|
5 | 5 | Create Date: 2026-01-29 14:54:10.669000 |
6 | 6 |
|
7 | 7 | """ |
| 8 | + |
8 | 9 | from typing import Sequence, Union |
9 | 10 |
|
| 11 | +import sqlalchemy as sa |
10 | 12 | from alembic import op |
11 | 13 | from sqlalchemy import inspect, text |
12 | | -import sqlalchemy as sa |
13 | | - |
14 | 14 |
|
15 | 15 | # revision identifiers, used by Alembic. |
16 | | -revision: str = '9221408912dd' |
| 16 | +revision: str = "9221408912dd" |
17 | 17 | down_revision: Union[str, None] = None |
18 | 18 | branch_labels: Union[str, Sequence[str], None] = None |
19 | 19 | depends_on: Union[str, Sequence[str], None] = None |
20 | 20 |
|
21 | 21 |
|
22 | 22 | def upgrade() -> None: |
23 | 23 | bind = op.get_bind() |
| 24 | + assert bind is not None |
24 | 25 | insp = inspect(bind) |
25 | 26 |
|
26 | 27 | # Add unique constraint on users.auth_uid (if not already present) |
27 | 28 | constraint_exists = bind.execute( |
28 | 29 | text("SELECT 1 FROM pg_constraint WHERE conname = 'auth_uid_unique'") |
29 | 30 | ).scalar() |
30 | 31 | if not constraint_exists: |
31 | | - op.create_unique_constraint('auth_uid_unique', 'users', ['auth_uid']) |
| 32 | + op.create_unique_constraint("auth_uid_unique", "users", ["auth_uid"]) |
32 | 33 |
|
33 | 34 | # Create the workspace_role enum type (if not already present) |
34 | 35 | result = bind.execute( |
35 | 36 | text("SELECT 1 FROM pg_type WHERE typname = 'workspace_role'") |
36 | 37 | ) |
37 | 38 | if not result.scalar(): |
38 | | - workspace_role = sa.Enum('lead', 'validator', 'contributor', name='workspace_role') |
| 39 | + workspace_role = sa.Enum( |
| 40 | + "lead", "validator", "contributor", name="workspace_role" |
| 41 | + ) |
39 | 42 | workspace_role.create(bind) |
40 | 43 |
|
41 | 44 | # Create the user_workspace_roles table (if not already present) |
42 | | - if not insp.has_table('user_workspace_roles'): |
| 45 | + if not insp.has_table("user_workspace_roles"): |
43 | 46 | op.create_table( |
44 | | - 'user_workspace_roles', |
45 | | - sa.Column('user_auth_uid', sa.Uuid(), nullable=False), |
46 | | - sa.Column('workspace_id', sa.BigInteger(), nullable=False), |
47 | | - sa.Column('role', sa.Enum('lead', 'validator', 'contributor', name='workspace_role', create_type=False), nullable=False), |
48 | | - sa.ForeignKeyConstraint(['user_auth_uid'], ['users.auth_uid']), |
49 | | - sa.PrimaryKeyConstraint('user_auth_uid', 'workspace_id') |
| 47 | + "user_workspace_roles", |
| 48 | + sa.Column("user_auth_uid", sa.Uuid(), nullable=False), |
| 49 | + sa.Column("workspace_id", sa.BigInteger(), nullable=False), |
| 50 | + sa.Column( |
| 51 | + "role", |
| 52 | + sa.Enum( |
| 53 | + "lead", |
| 54 | + "validator", |
| 55 | + "contributor", |
| 56 | + name="workspace_role", |
| 57 | + create_type=False, |
| 58 | + ), |
| 59 | + nullable=False, |
| 60 | + ), |
| 61 | + sa.ForeignKeyConstraint(["user_auth_uid"], ["users.auth_uid"]), |
| 62 | + sa.PrimaryKeyConstraint("user_auth_uid", "workspace_id"), |
50 | 63 | ) |
51 | 64 |
|
52 | 65 |
|
53 | 66 | def downgrade() -> None: |
54 | 67 | bind = op.get_bind() |
| 68 | + assert bind is not None |
55 | 69 | insp = inspect(bind) |
56 | 70 |
|
57 | | - if insp.has_table('user_workspace_roles'): |
58 | | - op.drop_table('user_workspace_roles') |
| 71 | + if insp.has_table("user_workspace_roles"): |
| 72 | + op.drop_table("user_workspace_roles") |
59 | 73 |
|
60 | 74 | # Drop the enum type |
61 | 75 | result = bind.execute( |
62 | 76 | text("SELECT 1 FROM pg_type WHERE typname = 'workspace_role'") |
63 | 77 | ) |
64 | 78 | if result.scalar(): |
65 | | - workspace_role = sa.Enum('lead', 'validator', 'contributor', name='workspace_role') |
| 79 | + workspace_role = sa.Enum( |
| 80 | + "lead", "validator", "contributor", name="workspace_role" |
| 81 | + ) |
66 | 82 | workspace_role.drop(bind) |
67 | 83 |
|
68 | 84 | constraint_exists = bind.execute( |
69 | 85 | text("SELECT 1 FROM pg_constraint WHERE conname = 'auth_uid_unique'") |
70 | 86 | ).scalar() |
71 | 87 | if constraint_exists: |
72 | | - op.drop_constraint('auth_uid_unique', 'users', type_='unique') |
| 88 | + op.drop_constraint("auth_uid_unique", "users", type_="unique") |
0 commit comments