"""create builder_modules table and link builder_categories Revision ID: 0005_builder_modules_table Revises: 0004_module_builder_tables Create Date: 2026-11-13 """ from __future__ import annotations from collections.abc import Sequence from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql revision: str = "0005_builder_modules_table" down_revision: str | None = "0004_module_builder_tables" branch_labels: Sequence[str] | None = None depends_on: Sequence[str] | None = None CONTENT_SCHEMA = "content" def upgrade() -> None: bind = op.get_bind() inspector = sa.inspect(bind) tables = inspector.get_table_names(schema=CONTENT_SCHEMA) if "builder_modules" not in tables: op.create_table( "builder_modules", sa.Column( "id", postgresql.UUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()"), ), sa.Column("code", sa.String(length=32), nullable=False, unique=True), sa.Column("name", sa.String(length=255), nullable=False), sa.Column("slug", sa.String(length=255), nullable=False), sa.Column("description", sa.Text(), nullable=True), sa.Column("short_description", sa.String(length=500), nullable=True), sa.Column( "kind", sa.String(length=64), nullable=False, server_default="therapeutic", ), sa.Column( "managed_by", sa.String(length=64), nullable=False, server_default="module_builder", ), sa.Column("default_template_code", sa.String(length=64), nullable=True), sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"), sa.Column("is_active", sa.Boolean(), nullable=False, server_default="true"), sa.Column( "created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()"), ), sa.Column( "updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()"), ), schema=CONTENT_SCHEMA, ) op.create_index( "idx_builder_modules_code", "builder_modules", ["code"], unique=True, schema=CONTENT_SCHEMA, ) # Seed therapeutics modules (m0041, m0042) bind.execute( sa.text( f""" INSERT INTO {CONTENT_SCHEMA}.builder_modules (code, name, slug, description, short_description, kind, default_template_code, sort_order, managed_by, is_active) VALUES ( 'm0041', 'Auriculotherapy', 'auriculotherapy', 'Auriculotherapy protocols and treatment points for musculoskeletal and clinical disorders.', 'Ear reflexology and clinical auriculotherapy protocols.', 'therapeutic', 'm0041_ct0001', 40001, 'module_builder', TRUE ), ( 'm0042', 'Gynecological Disorders', 'gynecological-disorders', 'TCM therapeutics and pattern differentiation for women''s health and gynecological disorders.', 'TCM gynecology and women''s health therapeutics.', 'therapeutic', 'm0042_ct0001', 42000, 'module_builder', TRUE ) ON CONFLICT (code) DO NOTHING """ ) ) # Ensure any orphan module_codes in builder_categories have a parent row bind.execute( sa.text( f""" INSERT INTO {CONTENT_SCHEMA}.builder_modules (code, name, slug, kind, sort_order, managed_by, is_active) SELECT DISTINCT bc.module_code, INITCAP(REPLACE(bc.module_code, '_', ' ')), LOWER(REPLACE(bc.module_code, '_', '-')), 'therapeutic', 40000, 'module_builder', TRUE FROM {CONTENT_SCHEMA}.builder_categories bc WHERE NOT EXISTS ( SELECT 1 FROM {CONTENT_SCHEMA}.builder_modules bm WHERE bm.code = bc.module_code ) ON CONFLICT (code) DO NOTHING """ ) ) # Add foreign key constraint if not present existing_fks = [ fk["name"] for fk in inspector.get_foreign_keys( "builder_categories", schema=CONTENT_SCHEMA ) ] if "fk_builder_categories_module" not in existing_fks: op.create_foreign_key( "fk_builder_categories_module", "builder_categories", "builder_modules", ["module_code"], ["code"], source_schema=CONTENT_SCHEMA, referent_schema=CONTENT_SCHEMA, ondelete="CASCADE", onupdate="CASCADE", ) def downgrade() -> None: bind = op.get_bind() inspector = sa.inspect(bind) existing_fks = [ fk["name"] for fk in inspector.get_foreign_keys( "builder_categories", schema=CONTENT_SCHEMA ) ] if "fk_builder_categories_module" in existing_fks: op.drop_constraint( "fk_builder_categories_module", "builder_categories", schema=CONTENT_SCHEMA, type_="foreignkey", ) op.drop_table("builder_modules", schema=CONTENT_SCHEMA)