Files
leocrm/alembic/versions/0050_owner_id_all_tables.py
T

50 lines
1.1 KiB
Python

"""Add owner_id to all entity tables for row-level ownership.
Revision ID: 0050
Revises: 0049
Create Date: 2026-07-29
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID as PGUUID
revision = "0050"
down_revision = "0049"
branch_labels = None
depends_on = None
# Tables that get owner_id (all entity tables except system tables)
TABLES = [
"contacts",
"contactpersons",
"addresses",
"bank_accounts",
"attachments",
"workflows",
"workflow_instances",
"sequences",
"saved_filters",
"saved_views",
"webhooks",
"custom_field_definitions",
"notifications",
"entity_history",
"ai_conversations",
]
def upgrade() -> None:
for table in TABLES:
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"])
def downgrade() -> None:
for table in TABLES:
op.drop_index(f"ix_{table}_owner", table_name=table)
op.drop_column(table, "owner_id")