Files
HartOMat/backend/alembic/versions/035_tenants.py
T
Hartmut 251dd703ed feat(B2): add tenant model + migrations 035/036 + RLS policies
Migration 035: tenants table with 'Schaeffler' default seed.
Migration 036: tenant_id FK on all tables, RLS policies, backfill.
New domains/tenants/ with CRUD router (admin only).
All domain models extended with tenant_id FK.
core/database.py: get_db_for_tenant with RLS context setter.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 16:30:41 +01:00

37 lines
1.0 KiB
Python

"""Add tenants table.
Revision ID: 035
Revises: 034
Create Date: 2026-03-06
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects.postgresql import UUID
revision = '035'
down_revision = '034'
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
'tenants',
sa.Column('id', UUID(as_uuid=True), primary_key=True,
server_default=sa.text('gen_random_uuid()')),
sa.Column('name', sa.String(200), nullable=False),
sa.Column('slug', sa.String(100), nullable=False, unique=True),
sa.Column('is_active', sa.Boolean(), nullable=False, server_default='true'),
sa.Column('created_at', sa.DateTime(), nullable=False,
server_default=sa.text('NOW()')),
)
# Seed default tenant — all existing data will be assigned to this tenant
op.execute("""
INSERT INTO tenants (name, slug, is_active)
VALUES ('Schaeffler', 'schaeffler', true)
""")
def downgrade():
op.drop_table('tenants')