feat(backend): a product can be listed in several categories, with one primary category
Platform v2 step 1.13 (APPS_PLATFORM D-23).
- product_categories(product_id, category_id, sort_order); products.category_id becomes
products.primary_category_id (revision 0013 moves the data: one listing per product at its
old position). services/catalog.py keeps the rule "the primary is one of its categories"
and fixes up products when a category is deleted.
- Products API: category_ids + primary_category_id in and out, plus categories
[{category_id, sort_order}]; GET /api/products/?category_id= lists a category's products in
that category's order; PUT /reorder with category_id orders within a category. category_id
stays as dev scaffolding (section 3.3): output = primary, input = "move the primary".
- Primary category: ticket grouping and prep-zone category order, the order-item category
snapshot (1.12), report filters for waste-only rows and /meta/products.
- Every listing: price modifier / deal / min-quantity category targets, deal free choices,
the QR menu snapshot (product in each category it's listed in; images pushed once).
- Catalogue export v2: each category lists all its products with "primary" and
"category_sort_order"; import restores listings and order. v1 files are refused (D-21).
- New errors catalog.unknown_category, catalog.primary_not_listed.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""product_categories + products.primary_category_id (multi-category) (D-23, step 1.13)
|
||||
|
||||
Hand-written data move: products.category_id becomes products.primary_category_id (new
|
||||
column with a named FK, copied, old column dropped by the batch rebuild, which also drops
|
||||
its unnamed FK), and every product with a category gets one listing in it, at its old
|
||||
products.sort_order position. products.sort_order stays (global order for the current
|
||||
screens).
|
||||
|
||||
Revision ID: 0013_product_categories
|
||||
Revises: 0012_drop_product_printer_zone
|
||||
Create Date: 2026-11-13 12:30:55.936976
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = '0013_product_categories'
|
||||
down_revision = '0012_drop_product_printer_zone'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table('product_categories',
|
||||
sa.Column('product_id', sa.Integer(), nullable=False),
|
||||
sa.Column('category_id', sa.Integer(), nullable=False),
|
||||
sa.Column('sort_order', sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ondelete='CASCADE'),
|
||||
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('product_id', 'category_id')
|
||||
)
|
||||
with op.batch_alter_table('product_categories', schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f('ix_product_categories_category_id'), ['category_id'], unique=False)
|
||||
|
||||
with op.batch_alter_table('products', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('primary_category_id', sa.Integer(), nullable=True))
|
||||
batch_op.create_index(batch_op.f('ix_products_primary_category_id'), ['primary_category_id'], unique=False)
|
||||
batch_op.create_foreign_key('fk_products_primary_category_id', 'categories', ['primary_category_id'], ['id'])
|
||||
|
||||
# only ids of categories that still exist (v1 never enforced the FK)
|
||||
op.execute("""UPDATE products SET primary_category_id = category_id
|
||||
WHERE category_id IN (SELECT id FROM categories)""")
|
||||
op.execute("""INSERT INTO product_categories (product_id, category_id, sort_order)
|
||||
SELECT id, primary_category_id, COALESCE(sort_order, 0) FROM products
|
||||
WHERE primary_category_id IS NOT NULL""")
|
||||
|
||||
with op.batch_alter_table('products', schema=None) as batch_op:
|
||||
batch_op.drop_column('category_id')
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table('products', schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column('category_id', sa.INTEGER(), nullable=True))
|
||||
op.execute("UPDATE products SET category_id = primary_category_id")
|
||||
with op.batch_alter_table('products', schema=None) as batch_op:
|
||||
batch_op.drop_constraint('fk_products_primary_category_id', type_='foreignkey')
|
||||
batch_op.drop_index(batch_op.f('ix_products_primary_category_id'))
|
||||
batch_op.drop_column('primary_category_id')
|
||||
|
||||
with op.batch_alter_table('product_categories', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_product_categories_category_id'))
|
||||
|
||||
op.drop_table('product_categories')
|
||||
Reference in New Issue
Block a user