"""Migration: Create workspace tables. Revision ID: 0072 Revises: 0071 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.postgresql import UUID as PGUUID, JSONB revision = "0072" down_revision = "0071" branch_labels = None depends_on = None def upgrade() -> None: # workspaces op.create_table( "workspaces", sa.Column("id", PGUUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), sa.Column("tenant_id", PGUUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False), sa.Column("name", sa.String(100), nullable=False), sa.Column("icon", sa.String(50), nullable=False, server_default="LayoutGrid"), sa.Column("description", sa.String(500), nullable=True), sa.Column("is_default", sa.Boolean, nullable=False, server_default=sa.text("false")), sa.Column("is_active", sa.Boolean, nullable=False, server_default=sa.text("true")), sa.Column("created_by", PGUUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), sa.UniqueConstraint("tenant_id", "name", name="uq_workspaces_tenant_name"), ) op.create_index("ix_workspaces_tenant", "workspaces", ["tenant_id"]) op.execute( "CREATE UNIQUE INDEX uq_workspace_default_per_tenant " "ON workspaces (tenant_id) WHERE is_default = true" ) # workspace_modules op.create_table( "workspace_modules", sa.Column("id", PGUUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), sa.Column("tenant_id", PGUUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False), sa.Column("workspace_id", PGUUID(as_uuid=True), sa.ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False), sa.Column("module_key", sa.String(100), nullable=False), sa.Column("is_visible", sa.Boolean, nullable=False, server_default=sa.text("true")), sa.Column("menu_order", sa.Integer, nullable=False, server_default=sa.text("0")), sa.Column("config", JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), sa.UniqueConstraint("tenant_id", "workspace_id", "module_key", name="uq_wm_tenant_workspace_module"), ) op.create_index("ix_wm_workspace", "workspace_modules", ["tenant_id", "workspace_id", "menu_order"]) # workspace_users op.create_table( "workspace_users", sa.Column("id", PGUUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), sa.Column("tenant_id", PGUUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False), sa.Column("workspace_id", PGUUID(as_uuid=True), sa.ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False), sa.Column("user_id", PGUUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False), sa.Column("role", sa.String(20), nullable=False, server_default="member"), sa.Column("is_default", sa.Boolean, nullable=False, server_default=sa.text("false")), sa.Column("assigned_by", PGUUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="SET NULL"), nullable=True), sa.Column("assigned_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), sa.UniqueConstraint("tenant_id", "workspace_id", "user_id", name="uq_wu_tenant_workspace_user"), sa.CheckConstraint("role IN ('member', 'manager')", name="ck_wu_role"), ) op.create_index("ix_wu_workspace", "workspace_users", ["tenant_id", "workspace_id"]) op.create_index("ix_wu_user", "workspace_users", ["tenant_id", "user_id"]) # workspace_widgets op.create_table( "workspace_widgets", sa.Column("id", PGUUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")), sa.Column("tenant_id", PGUUID(as_uuid=True), sa.ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False), sa.Column("workspace_id", PGUUID(as_uuid=True), sa.ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False), sa.Column("widget_key", sa.String(100), nullable=False), sa.Column("position_x", sa.Integer, nullable=False, server_default=sa.text("0")), sa.Column("position_y", sa.Integer, nullable=False, server_default=sa.text("0")), sa.Column("width", sa.Integer, nullable=False, server_default=sa.text("1")), sa.Column("height", sa.Integer, nullable=False, server_default=sa.text("1")), sa.Column("config", JSONB, nullable=False, server_default=sa.text("'{}'::jsonb")), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False), ) op.create_index("ix_ww_workspace", "workspace_widgets", ["tenant_id", "workspace_id"]) # RLS on all workspace tables for table in ["workspaces", "workspace_modules", "workspace_users", "workspace_widgets"]: op.execute(f"ALTER TABLE {table} ENABLE ROW LEVEL SECURITY") op.execute( f"CREATE POLICY {table}_tenant_isolation ON {table} " "FOR ALL " "USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid) " "WITH CHECK (tenant_id = current_setting('app.current_tenant_id', true)::uuid)" ) op.execute(f"GRANT SELECT, INSERT, UPDATE, DELETE ON {table} TO crm_api, crm_worker") def downgrade() -> None: for table in ["workspace_widgets", "workspace_users", "workspace_modules", "workspaces"]: op.execute(f"DROP POLICY IF EXISTS {table}_tenant_isolation ON {table}") op.drop_table(table)