49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
"""add tenant lifecycle fields
|
|
|
|
Revision ID: 5f2e9c1a7b44
|
|
Revises: 720027c97104
|
|
Create Date: 2026-04-18 14:10:00.000000
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "5f2e9c1a7b44"
|
|
down_revision: Union[str, Sequence[str], None] = "720027c97104"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.execute("ALTER TABLE tenants ADD COLUMN IF NOT EXISTS start_date DATE")
|
|
op.execute("ALTER TABLE tenants ADD COLUMN IF NOT EXISTS end_date DATE")
|
|
op.execute("ALTER TABLE tenants ADD COLUMN IF NOT EXISTS status VARCHAR")
|
|
op.execute("ALTER TABLE tenants ALTER COLUMN status SET DEFAULT 'ACTIVE'")
|
|
|
|
op.execute(
|
|
"""
|
|
UPDATE tenants
|
|
SET status = CASE
|
|
WHEN is_active = true THEN 'ACTIVE'
|
|
ELSE 'INACTIVE'
|
|
END
|
|
WHERE status IS NULL
|
|
"""
|
|
)
|
|
|
|
op.execute("ALTER TABLE tenants ALTER COLUMN status SET NOT NULL")
|
|
op.execute("ALTER TABLE tenants ALTER COLUMN status DROP DEFAULT")
|
|
op.execute(
|
|
"CREATE INDEX IF NOT EXISTS ix_tenants_status ON tenants (status)"
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.execute("DROP INDEX IF EXISTS ix_tenants_status")
|
|
op.execute("ALTER TABLE tenants DROP COLUMN IF EXISTS status")
|
|
op.execute("ALTER TABLE tenants DROP COLUMN IF EXISTS end_date")
|
|
op.execute("ALTER TABLE tenants DROP COLUMN IF EXISTS start_date")
|