113 lines
3.9 KiB
Python
113 lines
3.9 KiB
Python
"""introduce product details
|
|
|
|
Revision ID: 3b61a6fbc6b2
|
|
Revises: 958d7aee2b21
|
|
Create Date: 2026-04-22 11:56:31.223186
|
|
|
|
"""
|
|
|
|
import datetime
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "3b61a6fbc6b2"
|
|
down_revision: Union[str, Sequence[str], None] = "958d7aee2b21"
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""Upgrade schema."""
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.create_table(
|
|
"aps_product_detail",
|
|
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
|
sa.Column("name", sa.String(), nullable=False),
|
|
sa.Column("product_id", sa.Integer(), nullable=False),
|
|
sa.Column("description", sa.String(), nullable=True),
|
|
sa.Column("price", sa.Numeric(precision=10, scale=2), nullable=False),
|
|
sa.Column(
|
|
"unit_of_measure",
|
|
sa.Enum("g", "kg", "l", "piece", native_enum=False),
|
|
nullable=False,
|
|
),
|
|
sa.Column("allow_fractional", sa.Boolean(), nullable=False),
|
|
sa.Column("temporarily_unavailable", sa.Boolean(), nullable=False),
|
|
sa.Column("vat_rate", sa.Numeric(precision=10, scale=2), nullable=False),
|
|
sa.Column("valid_from", sa.DateTime(), nullable=False),
|
|
sa.ForeignKeyConstraint(
|
|
["product_id"],
|
|
["aps_product.id"],
|
|
),
|
|
sa.PrimaryKeyConstraint("id"),
|
|
sa.UniqueConstraint("name"),
|
|
)
|
|
|
|
# Step 2: Copy data from aps_product to aps_product_detail
|
|
# Bind the current connection to a temporary session
|
|
conn = op.get_bind()
|
|
|
|
# Fetch all products
|
|
products = conn.execute(
|
|
sa.text(
|
|
"SELECT id, name, price, unit_of_measure, allow_fractional, vat_rate FROM aps_product"
|
|
)
|
|
).fetchall()
|
|
|
|
# Insert data into aps_product_detail
|
|
for product in products:
|
|
conn.execute(
|
|
sa.text(
|
|
"""
|
|
INSERT INTO aps_product_detail
|
|
(name, product_id, description, price, unit_of_measure, allow_fractional, temporarily_unavailable, vat_rate, valid_from)
|
|
VALUES (:name, :product_id, :description, :price, :unit_of_measure, :allow_fractional, :temporarily_unavailable, :vat_rate, :valid_from)
|
|
"""
|
|
),
|
|
{
|
|
"name": product.name,
|
|
"product_id": product.id,
|
|
"description": "",
|
|
"price": product.price,
|
|
"unit_of_measure": product.unit_of_measure,
|
|
"allow_fractional": product.allow_fractional,
|
|
"temporarily_unavailable": False,
|
|
"vat_rate": product.vat_rate,
|
|
"valid_from": datetime.datetime(1970, 1, 1, 0, 0, 0),
|
|
},
|
|
)
|
|
with op.batch_alter_table("aps_product") as batch_op:
|
|
batch_op.drop_column("vat_rate")
|
|
batch_op.drop_column("name")
|
|
|
|
batch_op.drop_column("allow_fractional")
|
|
batch_op.drop_column("unit_of_measure")
|
|
batch_op.drop_column("price")
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Downgrade schema."""
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.add_column(
|
|
"aps_product",
|
|
sa.Column("price", sa.NUMERIC(precision=10, scale=2), nullable=False),
|
|
)
|
|
op.add_column(
|
|
"aps_product",
|
|
sa.Column("unit_of_measure", sa.VARCHAR(length=5), nullable=False),
|
|
)
|
|
op.add_column(
|
|
"aps_product", sa.Column("allow_fractional", sa.BOOLEAN(), nullable=False)
|
|
)
|
|
op.add_column(
|
|
"aps_product",
|
|
sa.Column("vat_rate", sa.NUMERIC(precision=10, scale=2), nullable=False),
|
|
)
|
|
op.add_column("aps_product", sa.Column("name", sa.VARCHAR(), nullable=False))
|
|
op.drop_table("aps_product_detail")
|
|
# ### end Alembic commands ###
|