gate2: fix all migrations for fresh DB installation
This commit is contained in:
@@ -29,6 +29,15 @@ def upgrade() -> None:
|
||||
# Check which columns already exist before adding
|
||||
conn = op.get_bind()
|
||||
for table in TABLES:
|
||||
# Check if table exists
|
||||
table_exists = conn.execute(
|
||||
sa.text(
|
||||
"SELECT 1 FROM information_schema.tables WHERE table_name = :table"
|
||||
),
|
||||
{"table": table},
|
||||
).fetchone()
|
||||
if table_exists is None:
|
||||
continue
|
||||
# Check if column already exists
|
||||
result = conn.execute(
|
||||
sa.text(
|
||||
@@ -38,16 +47,8 @@ def upgrade() -> None:
|
||||
{"table": table},
|
||||
)
|
||||
if result.fetchone() is None:
|
||||
op.add_column(
|
||||
table,
|
||||
sa.Column(
|
||||
"owner_id",
|
||||
PGUUID(as_uuid=True),
|
||||
sa.ForeignKey("users.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
op.create_index(f"ix_{table}_owner", table, ["owner_id"])
|
||||
op.execute(f"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS owner_id UUID REFERENCES users(id) ON DELETE SET NULL")
|
||||
op.execute(f"CREATE INDEX IF NOT EXISTS ix_{table}_owner ON {table} (owner_id)")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
|
||||
Reference in New Issue
Block a user