Files
leocrm/alembic/versions/0080_disable_rls_audit_sessions.py

34 lines
1018 B
Python

"""Disable RLS on audit_log and sessions (written during login before tenant context).
Revision ID: 0080
Revises: 0079
"""
from alembic import op
from sqlalchemy import text
revision = "0080"
down_revision = "0079"
branch_labels = None
depends_on = None
TABLES = ["audit_log", "sessions", "password_reset_tokens", "api_tokens"]
def upgrade() -> None:
conn = op.get_bind()
for table in TABLES:
exists = conn.execute(
text(f"SELECT 1 FROM information_schema.tables WHERE table_name = '{table}'")
).fetchone() is not None
if not exists:
continue
policies = conn.execute(text(
f"SELECT policyname FROM pg_policies WHERE tablename = '{table}'"
)).fetchall()
for (policyname,) in policies:
conn.execute(text(f"DROP POLICY IF EXISTS {policyname} ON {table}"))
conn.execute(text(f"ALTER TABLE {table} DISABLE ROW LEVEL SECURITY"))
print(f" Disabled RLS on {table}")
def downgrade() -> None:
pass