Files
HartOMat/backend/alembic/versions/029_seed_default_render_positions.py
T
2026-03-05 22:12:38 +01:00

54 lines
1.7 KiB
Python

"""Seed default render positions (3/4 Front + 3/4 Rear) for all existing products.
Revision ID: 029
Revises: 028
Create Date: 2026-03-04
"""
from alembic import op
revision = "029"
down_revision = "028"
branch_labels = None
depends_on = None
def upgrade():
# Insert two default positions for every product that currently has none.
# The CTE guarantees both rows are inserted for the same set of products.
op.execute("""
WITH products_without_positions AS (
SELECT p.id AS product_id
FROM products p
WHERE NOT EXISTS (
SELECT 1 FROM product_render_positions rp WHERE rp.product_id = p.id
)
)
INSERT INTO product_render_positions
(id, product_id, name, rotation_x, rotation_y, rotation_z,
is_default, sort_order, created_at, updated_at)
SELECT gen_random_uuid(), product_id,
'3/4 Front', -15.0, 45.0, 0.0, true, 0, NOW(), NOW()
FROM products_without_positions
UNION ALL
SELECT gen_random_uuid(), product_id,
'3/4 Rear', -15.0, -135.0, 0.0, false, 1, NOW(), NOW()
FROM products_without_positions
""")
def downgrade():
# Remove positions named exactly '3/4 Front' or '3/4 Rear'
# where they are the only two positions on that product (i.e. seeded ones).
op.execute("""
DELETE FROM product_render_positions
WHERE name IN ('3/4 Front', '3/4 Rear')
AND product_id IN (
SELECT product_id
FROM product_render_positions
GROUP BY product_id
HAVING COUNT(*) = 2
AND bool_or(name = '3/4 Front')
AND bool_or(name = '3/4 Rear')
)
""")