arm-core/api/alembic/versions/0011_vgo_assignment.py

61 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Хранение назначения строк ОСВ правилам по версии консолидации."""
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from alembic import op
from src.models.base import SCHEMA
revision = "0011_vgo_assignment"
down_revision = "0010_dbml_v5_23"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"vgo_assignment",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column("consolidation_version_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column(
"statement_type",
postgresql.ENUM("balance", "income", name="statement_type", schema=SCHEMA, create_type=False),
nullable=False,
),
sa.Column("osv_entry_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("rule_id", postgresql.UUID(as_uuid=True)),
sa.Column("action", sa.String(), nullable=False),
sa.Column("is_exclude", sa.Boolean(), server_default=sa.text("false"), nullable=False),
sa.Column("created_by", postgresql.UUID(as_uuid=True)),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("updated_by", postgresql.UUID(as_uuid=True)),
sa.Column("updated_at", sa.DateTime(timezone=True)),
sa.ForeignKeyConstraint(
["consolidation_version_id"], [f"{SCHEMA}.consolidation_version.id"], name="fk_vgo_assignment_version"
),
sa.ForeignKeyConstraint(["osv_entry_id"], [f"{SCHEMA}.osv_entry.id"], name="fk_vgo_assignment_entry"),
sa.ForeignKeyConstraint(["rule_id"], [f"{SCHEMA}.elimination_rule.id"], name="fk_vgo_assignment_rule"),
sa.ForeignKeyConstraint(["created_by"], [f"{SCHEMA}.app_user.id"], name="fk_vgo_assignment_created_by"),
sa.ForeignKeyConstraint(["updated_by"], [f"{SCHEMA}.app_user.id"], name="fk_vgo_assignment_updated_by"),
sa.CheckConstraint("action IN ('ASSIGN', 'UNASSIGNED')", name="ck_vgo_assignment_action_values"),
sa.CheckConstraint(
"(action = 'ASSIGN' AND rule_id IS NOT NULL) OR (action = 'UNASSIGNED' AND rule_id IS NULL)",
name="ck_vgo_assignment_action_rule_consistency",
),
schema=SCHEMA,
)
op.create_index(
"uq_vgo_assignment_version_type_entry",
"vgo_assignment",
["consolidation_version_id", "statement_type", "osv_entry_id"],
unique=True,
schema=SCHEMA,
)
op.create_index("ix_vgo_assignment_osv_entry_id", "vgo_assignment", ["osv_entry_id"], schema=SCHEMA)
def downgrade() -> None:
op.drop_index("ix_vgo_assignment_osv_entry_id", table_name="vgo_assignment", schema=SCHEMA)
op.drop_index("uq_vgo_assignment_version_type_entry", table_name="vgo_assignment", schema=SCHEMA)
op.drop_table("vgo_assignment", schema=SCHEMA)