Files
Odoo-Modules/fusion_plating/fusion_plating_configurator/migrations/19.0.22.0.0/pre-migrate.py

85 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
"""Pre-migration for Express Orders (19.0.22.0.0).
Runs BEFORE Odoo's field-registration pass on `-u fusion_plating_configurator`,
so the model can register `terms_and_conditions` and `pricelist_id` against
columns that already hold data.
Actions:
1. Rename `fp_direct_order_wizard.notes` → `terms_and_conditions`.
Existing data preserves its semantic (always was customer-facing because
the old `action_create_order` wrote it to sale.order.note).
2. Add the new Express columns (idempotent — IF NOT EXISTS guards).
3. Backfill `pricelist_id` from the legacy `currency_id` via any active
pricelist matching the currency. After this, the model's stored-related
currency_id (related='pricelist_id.currency_id') takes over.
4. (No currency_id column drop here — Odoo's schema sync recognises the
related field and keeps the column shape; data refreshes from the
related lookup on subsequent writes.)
Dev-stage caveat: per the Express Orders design spec section 12, we are
ignoring past orders. If `currency_id` data on legacy rows doesn't line up
with the new pricelist_id-driven flow, that's acceptable.
"""
def migrate(cr, version):
# 1. Rename notes → terms_and_conditions (same column, same data)
cr.execute("""
SELECT 1 FROM information_schema.columns
WHERE table_name = 'fp_direct_order_wizard'
AND column_name = 'notes'
""")
if cr.fetchone():
cr.execute("""
ALTER TABLE fp_direct_order_wizard
RENAME COLUMN notes TO terms_and_conditions
""")
# 2. Add new Express columns (idempotent)
cr.execute("""
ALTER TABLE fp_direct_order_wizard
ADD COLUMN IF NOT EXISTS internal_notes TEXT
""")
cr.execute("""
ALTER TABLE fp_direct_order_wizard
ADD COLUMN IF NOT EXISTS pricelist_id INTEGER
REFERENCES product_pricelist(id) ON DELETE SET NULL
""")
cr.execute("""
ALTER TABLE fp_direct_order_wizard
ADD COLUMN IF NOT EXISTS material_process VARCHAR
""")
cr.execute("""
ALTER TABLE fp_direct_order_wizard
ADD COLUMN IF NOT EXISTS validity_date DATE
""")
cr.execute("""
ALTER TABLE fp_direct_order_wizard
ADD COLUMN IF NOT EXISTS view_source VARCHAR DEFAULT 'legacy'
""")
# Note: view_source defaults to 'legacy' for EXISTING rows — they were
# created via the legacy view. New rows default to 'express' via the model.
# 3. Backfill pricelist_id from any active pricelist matching the
# legacy currency_id (only if the currency_id column still exists).
cr.execute("""
SELECT 1 FROM information_schema.columns
WHERE table_name = 'fp_direct_order_wizard'
AND column_name = 'currency_id'
""")
if cr.fetchone():
cr.execute("""
UPDATE fp_direct_order_wizard w
SET pricelist_id = (
SELECT p.id
FROM product_pricelist p
WHERE p.currency_id = w.currency_id
AND p.active = TRUE
ORDER BY p.id
LIMIT 1
)
WHERE w.pricelist_id IS NULL
AND w.currency_id IS NOT NULL
""")