Skip to content

Commit

Permalink
feat: update migrate tenant_id
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhouhaoJiang committed Jul 8, 2024
1 parent 6312e7b commit 8f1ad72
Showing 1 changed file with 1 addition and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,7 @@ def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('dataset_permissions', schema=None) as batch_op:
# Step 1: Add column without NOT NULL constraint
op.add_column('dataset_permissions', sa.Column('tenant_id', sa.UUID(), nullable=True))

# Step 2: Update the column to set a default value for existing rows
op.execute(
"""
UPDATE dataset_permissions
SET tenant_id = (
SELECT tenant_id
FROM datasets
WHERE datasets.id = dataset_permissions.dataset_id
)
"""
)

# Step 3: Update the column to set a default value for existing rows
op.execute(
"""
UPDATE dataset_permissions
SET tenant_id = '00000000-0000-0000-0000-000000000000'
WHERE tenant_id IS NULL
"""
)

# Step 4: Alter column to NOT NULL
op.alter_column('dataset_permissions', 'tenant_id', nullable=False)
op.add_column('dataset_permissions', sa.Column('tenant_id', sa.UUID(), nullable=False))

# ### end Alembic commands ###

Expand Down

0 comments on commit 8f1ad72

Please sign in to comment.