acupedia-api/alembic/versions/0003_monthly_invoice_seq.py

42 lines
1.2 KiB
Python

"""add invoice monthly sequences table
Revision ID: 0003_monthly_invoice_seq
Revises: 0002_add_template_search_flags
Create Date: 2026-10-20
"""
from __future__ import annotations
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
revision: str = "0003_monthly_invoice_seq"
down_revision: str | None = "0002_add_template_search_flags"
branch_labels: Sequence[str] | None = None
depends_on: Sequence[str] | None = None
COMMERCE_SCHEMA = "commerce"
TABLE_NAME = "invoice_monthly_sequences"
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
tables = inspector.get_table_names(schema=COMMERCE_SCHEMA)
if TABLE_NAME not in tables:
op.create_table(
TABLE_NAME,
sa.Column("year_month", sa.String(length=7), nullable=False),
sa.Column("last_serial", sa.Integer(), nullable=False, server_default="0"),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("now()")),
sa.PrimaryKeyConstraint("year_month"),
schema=COMMERCE_SCHEMA,
)
def downgrade() -> None:
op.drop_table(TABLE_NAME, schema=COMMERCE_SCHEMA)