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
+21 -21
View File
@@ -29,7 +29,7 @@ def upgrade() -> None:
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_tenants_slug", "tenants", ["slug"])
op.execute('CREATE INDEX IF NOT EXISTS ix_tenants_slug ON tenants (slug)')
# users
op.create_table(
@@ -47,7 +47,7 @@ def upgrade() -> None:
sa.UniqueConstraint("tenant_id", "email", name="uq_users_tenant_email"),
)
op.execute("CREATE INDEX IF NOT EXISTS ix_users_tenant_id ON users (tenant_id)")
op.create_index("ix_users_email", "users", ["email"])
op.execute('CREATE INDEX IF NOT EXISTS ix_users_email ON users (email)')
# user_tenants
op.create_table(
@@ -68,7 +68,7 @@ def upgrade() -> None:
sa.Column("field_permissions", postgresql.JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_roles_tenant_id", "roles", ["tenant_id"])
op.execute('CREATE INDEX IF NOT EXISTS ix_roles_tenant_id ON roles (tenant_id)')
# sessions
op.create_table(
@@ -80,8 +80,8 @@ def upgrade() -> None:
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_sessions_tenant_id", "sessions", ["tenant_id"])
op.create_index("ix_sessions_user_id", "sessions", ["user_id"])
op.execute('CREATE INDEX IF NOT EXISTS ix_sessions_tenant_id ON sessions (tenant_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_sessions_user_id ON sessions (user_id)')
# audit_log
op.create_table(
@@ -95,10 +95,10 @@ def upgrade() -> None:
sa.Column("changes", postgresql.JSONB, nullable=True),
sa.Column("timestamp", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_audit_log_tenant_id", "audit_log", ["tenant_id"])
op.create_index("ix_audit_log_entity_type", "audit_log", ["entity_type"])
op.create_index("ix_audit_log_user_id", "audit_log", ["user_id"])
op.create_index("ix_audit_log_timestamp", "audit_log", ["timestamp"])
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_tenant_id ON audit_log (tenant_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_entity_type ON audit_log (entity_type)')
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_user_id ON audit_log (user_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_audit_log_timestamp ON audit_log (timestamp)')
# deletion_log
op.create_table(
@@ -124,9 +124,9 @@ def upgrade() -> None:
sa.Column("read_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_notifications_tenant_id", "notifications", ["tenant_id"])
op.create_index("ix_notifications_user_id", "notifications", ["user_id"])
op.create_index("ix_notifications_tenant_user_read", "notifications", ["tenant_id", "user_id", "read_at"])
op.execute('CREATE INDEX IF NOT EXISTS ix_notifications_tenant_id ON notifications (tenant_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_notifications_user_id ON notifications (user_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_notifications_tenant_user_read ON notifications (tenant_id, user_id, read_at)')
# password_reset_tokens
op.create_table(
@@ -138,9 +138,9 @@ def upgrade() -> None:
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("used_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_password_reset_tokens_tenant_id", "password_reset_tokens", ["tenant_id"])
op.create_index("ix_password_reset_tokens_user_id", "password_reset_tokens", ["user_id"])
op.create_index("ix_password_reset_tokens_token_hash", "password_reset_tokens", ["token_hash"])
op.execute('CREATE INDEX IF NOT EXISTS ix_password_reset_tokens_tenant_id ON password_reset_tokens (tenant_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_password_reset_tokens_user_id ON password_reset_tokens (user_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_password_reset_tokens_token_hash ON password_reset_tokens (token_hash)')
# api_tokens
op.create_table(
@@ -156,9 +156,9 @@ def upgrade() -> None:
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("revoked_at", sa.DateTime(timezone=True), nullable=True),
)
op.create_index("ix_api_tokens_tenant_id", "api_tokens", ["tenant_id"])
op.create_index("ix_api_tokens_token_hash", "api_tokens", ["token_hash"])
op.create_index("ix_api_tokens_tenant_user", "api_tokens", ["tenant_id", "user_id"])
op.execute('CREATE INDEX IF NOT EXISTS ix_api_tokens_tenant_id ON api_tokens (tenant_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_api_tokens_token_hash ON api_tokens (token_hash)')
op.execute('CREATE INDEX IF NOT EXISTS ix_api_tokens_tenant_user ON api_tokens (tenant_id, user_id)')
# companies
op.create_table(
@@ -178,9 +178,9 @@ def upgrade() -> None:
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
)
op.create_index("ix_companies_tenant_id", "companies", ["tenant_id"])
op.create_index("ix_companies_tenant_deleted", "companies", ["tenant_id", "deleted_at"])
op.create_index("ix_companies_tenant_name", "companies", ["tenant_id", "name"])
op.execute('CREATE INDEX IF NOT EXISTS ix_companies_tenant_id ON companies (tenant_id)')
op.execute('CREATE INDEX IF NOT EXISTS ix_companies_tenant_deleted ON companies (tenant_id, deleted_at)')
op.execute('CREATE INDEX IF NOT EXISTS ix_companies_tenant_name ON companies (tenant_id, name)')
# Enable RLS on tenant-scoped tables
for table in ["companies", "users", "roles", "sessions", "audit_log", "notifications", "api_tokens"]: