gate2: fix duplicate column/index in migrations for fresh DB installation

This commit is contained in:
Agent Zero
2026-07-31 18:20:09 +02:00
parent 224a5ea9af
commit dd7ad461d8
17 changed files with 53 additions and 78 deletions
+8 -5
View File
@@ -161,11 +161,14 @@ def upgrade() -> None:
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_contacts_tenant_deleted", "contacts", ["tenant_id", "deleted_at"])
op.execute("DROP INDEX IF EXISTS ix_contacts_tenant_deleted")
op.execute("CREATE INDEX IF NOT EXISTS ix_contacts_tenant_deleted ON contacts (tenant_id, deleted_at)")
op.create_index("ix_contacts_tenant_type", "contacts", ["tenant_id", "type"])
op.create_index("ix_contacts_tenant_name", "contacts", ["tenant_id", "name"])
op.execute("DROP INDEX IF EXISTS ix_contacts_tenant_name")
op.execute("CREATE INDEX IF NOT EXISTS ix_contacts_tenant_name ON contacts (tenant_id, name)")
op.create_index("ix_contacts_tenant_displayname", "contacts", ["tenant_id", "displayname"])
op.create_index("ix_contacts_email", "contacts", ["email_1"])
op.execute("DROP INDEX IF EXISTS ix_contacts_email")
op.execute("CREATE INDEX IF NOT EXISTS ix_contacts_email ON contacts (email_1)")
op.create_index("ix_contacts_code", "contacts", ["code"])
op.create_index("ix_contacts_search_vec", "contacts", ["search_tsv"], postgresql_using="gin")
@@ -202,8 +205,8 @@ def upgrade() -> None:
op.create_index("ix_contactpersons_email", "contactpersons", ["email"])
# ── 4. Add FK columns to contacts that reference contactpersons ───
op.add_column("contacts", sa.Column("default_person_id", UUID(as_uuid=True), sa.ForeignKey("contactpersons.id", ondelete="SET NULL"), nullable=True))
op.add_column("contacts", sa.Column("admin_contactperson_id", UUID(as_uuid=True), sa.ForeignKey("contactpersons.id", ondelete="SET NULL"), nullable=True))
op.execute("ALTER TABLE contacts ADD COLUMN IF NOT EXISTS default_person_id UUID REFERENCES contactpersons(id) ON DELETE SET NULL")
op.execute("ALTER TABLE contacts ADD COLUMN IF NOT EXISTS admin_contactperson_id UUID REFERENCES contactpersons(id) ON DELETE SET NULL")
# ── 5. Migrate data from old tables ────────────────────────────────