fix: migration 0135 use CREATE TABLE IF NOT EXISTS + fix syntax error

This commit is contained in:
Agent Zero
2026-08-21 10:05:47 +02:00
parent 283409513e
commit e92034f1b6
+23 -23
View File
@@ -22,33 +22,33 @@ def upgrade() -> None:
op.execute("ALTER TABLE notifications ALTER COLUMN type TYPE VARCHAR(100);") op.execute("ALTER TABLE notifications ALTER COLUMN type TYPE VARCHAR(100);")
op.execute("ALTER TABLE notification_preferences ALTER COLUMN type_key TYPE VARCHAR(100);") op.execute("ALTER TABLE notification_preferences ALTER COLUMN type_key TYPE VARCHAR(100);")
# 2. Create missing table: forgejo_reported_errors # 2. Create missing table: forgejo_reported_errors (only if not exists)
# Model: ReportedError(Base) — NO TenantMixin, Integer id op.execute("""
op.create_table( CREATE TABLE IF NOT EXISTS forgejo_reported_errors (
"forgejo_reported_errors", id SERIAL PRIMARY KEY,
sa.Column("id", sa.Integer, primary_key=True, autoincrement=True), dedup_key VARCHAR(64) NOT NULL UNIQUE,
sa.Column("dedup_key", sa.String(64), nullable=False, unique=True, index=True), message TEXT NOT NULL,
sa.Column("message", sa.Text, nullable=False), stack TEXT,
sa.Column("stack", sa.Text, nullable=True), forgejo_issue_number INTEGER,
sa.Column("forgejo_issue_number", sa.Integer, nullable=True), reported_at TIMESTAMPTZ DEFAULT now() NOT NULL,
sa.Column("reported_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")), status VARCHAR(20) NOT NULL DEFAULT 'reported'
sa.Column("status", sa.String(20), nullable=False, server_default=sa.text("'reported'")),
) )
# No RLS — model has no tenant_id """)
# 3. Create missing table: pgp_keys # 3. Create missing table: pgp_keys (only if not exists)
# Model: PgpKey(Base, TenantMixin) — UUID id, user_id, key_id, encrypted_private_key, public_key_armored op.execute("""
op.create_table( CREATE TABLE IF NOT EXISTS pgp_keys (
"pgp_keys", id UUID DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
sa.Column("id", UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
sa.Column("tenant_id", UUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False), user_id UUID NOT NULL,
sa.Column("user_id", UUID(as_uuid=True), nullable=False), key_id VARCHAR(255) NOT NULL,
sa.Column("key_id", sa.String(255), nullable=False), encrypted_private_key TEXT NOT NULL,
sa.Column("encrypted_private_key", sa.Text, nullable=False), public_key_armored TEXT NOT NULL
sa.Column("public_key_armored", sa.Text, nullable=False),
) )
op.create_index("ix_pgp_keys_user", "pgp_keys", ["user_id"]) """)
op.execute("CREATE INDEX IF NOT EXISTS ix_pgp_keys_user ON pgp_keys (user_id);")
op.execute("ALTER TABLE pgp_keys ENABLE ROW LEVEL SECURITY;") op.execute("ALTER TABLE pgp_keys ENABLE ROW LEVEL SECURITY;")
op.execute("DROP POLICY IF EXISTS pgp_keys_tenant_isolation ON pgp_keys;")
op.execute("CREATE POLICY pgp_keys_tenant_isolation ON pgp_keys USING (tenant_id::text = current_setting('app.current_tenant_id', true));") op.execute("CREATE POLICY pgp_keys_tenant_isolation ON pgp_keys USING (tenant_id::text = current_setting('app.current_tenant_id', true));")