"""fiscal_series + fiscal_certificates + fiscal_documents (Task 3): the three tables that "change owner" per the design spec (decision #2) -- ported from the auto's `fiscal_document_series` (tenants), `fiscal_ certificates`/`fiscal_documents` (fiscal), with product_id/tenant_ref/ branch_ref replacing organization_id/branch_id (porte table). Revision ID: 523235d06bd5 Revises: fd0ffe65c993 Create Date: 2026-07-22 00:00:00.000000 """ from __future__ import annotations from typing import Sequence, Union import sqlalchemy as sa from alembic import op revision: str = "523235d06bd5" down_revision: Union[str, Sequence[str], None] = "fd0ffe65c993" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.create_table( "fiscal_series", sa.Column("id", sa.UUID(), nullable=False), sa.Column("product_id", sa.UUID(), nullable=False), sa.Column("tenant_ref", sa.String(length=64), nullable=False), sa.Column("branch_ref", sa.String(length=64), nullable=False), sa.Column("document_model", sa.String(length=2), nullable=False), sa.Column("serie", sa.Integer(), nullable=False), sa.Column("next_number", sa.Integer(), nullable=False), 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.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["product_id"], ["products.id"]), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint( "product_id", "tenant_ref", "branch_ref", "document_model", "serie", name="uq_fiscal_series_product_tenant_branch_model_serie", ), ) op.create_table( "fiscal_certificates", sa.Column("id", sa.UUID(), nullable=False), sa.Column("product_id", sa.UUID(), nullable=False), sa.Column("tenant_ref", sa.String(length=64), nullable=False), sa.Column("branch_ref", sa.String(length=64), nullable=False), sa.Column("cnpj", sa.String(length=14), nullable=False), sa.Column("pfx_encrypted", sa.LargeBinary(), nullable=False), sa.Column("password_encrypted", sa.LargeBinary(), nullable=False), sa.Column("subject_cn", sa.String(length=255), nullable=False), sa.Column("cnpj_certificado", sa.String(length=14), nullable=False), sa.Column("not_valid_before", sa.DateTime(timezone=True), nullable=False), sa.Column("not_valid_after", sa.DateTime(timezone=True), nullable=False), 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.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["product_id"], ["products.id"]), sa.PrimaryKeyConstraint("id"), ) op.create_index( "ix_fiscal_certificates_product_branch_live", "fiscal_certificates", ["product_id", "branch_ref"], unique=True, postgresql_where=sa.text("deleted_at IS NULL"), ) op.create_table( "fiscal_documents", sa.Column("id", sa.UUID(), nullable=False), sa.Column("product_id", sa.UUID(), nullable=False), sa.Column("tenant_ref", sa.String(length=64), nullable=False), sa.Column("branch_ref", sa.String(length=64), nullable=False), sa.Column("series_id", sa.UUID(), nullable=False), sa.Column("document_model", sa.String(length=2), nullable=False), sa.Column("serie", sa.Integer(), nullable=False), sa.Column("numero", sa.Integer(), nullable=False), sa.Column("chave_acesso", sa.String(length=44), nullable=False), sa.Column("codigo_numerico", sa.String(length=8), nullable=False), sa.Column("status", sa.String(length=20), nullable=False), sa.Column("ambiente", sa.String(length=12), nullable=False), sa.Column("xml_assinado", sa.Text(), nullable=False), sa.Column("rejeicao_codigo", sa.String(length=10), nullable=True), sa.Column("rejeicao_motivo", sa.String(length=500), nullable=True), sa.Column("protocolo", sa.String(length=20), nullable=True), sa.Column("autorizada_em", sa.DateTime(timezone=True), 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.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.ForeignKeyConstraint(["product_id"], ["products.id"]), sa.ForeignKeyConstraint(["series_id"], ["fiscal_series.id"]), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("chave_acesso", name="uq_fiscal_documents_chave_acesso"), ) op.create_index( "ix_fiscal_documents_product_tenant_branch_status", "fiscal_documents", ["product_id", "tenant_ref", "branch_ref", "status"], ) def downgrade() -> None: op.drop_index("ix_fiscal_documents_product_tenant_branch_status", table_name="fiscal_documents") op.drop_table("fiscal_documents") op.drop_index("ix_fiscal_certificates_product_branch_live", table_name="fiscal_certificates") op.drop_table("fiscal_certificates") op.drop_table("fiscal_series")