gate2: fix all migrations for fresh DB installation

This commit is contained in:
Agent Zero
2026-07-31 19:16:11 +02:00
parent d37388423d
commit 010ef448e7
40 changed files with 230 additions and 207 deletions
+11 -10
View File
@@ -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: