-- migrate:up -- Nueva tabla simplificada para cantidades por producto/evento/persona create table if not exists product_qty_rules ( id bigserial primary key, tenant_id uuid not null references tenants(id) on delete cascade, woo_product_id integer not null, event_type text not null, -- 'asado' | 'horno' | 'cumple' | 'almuerzo' person_type text not null, -- 'adult' | 'child' qty_per_person numeric(6,3), -- cantidad por persona unit text not null default 'kg', -- 'kg' | 'g' | 'unidad' created_at timestamptz not null default now(), updated_at timestamptz not null default now(), unique (tenant_id, woo_product_id, event_type, person_type) ); create index if not exists product_qty_rules_tenant_idx on product_qty_rules(tenant_id); create index if not exists product_qty_rules_product_idx on product_qty_rules(tenant_id, woo_product_id); create index if not exists product_qty_rules_event_idx on product_qty_rules(tenant_id, event_type); -- Migrar datos existentes de reco_rule_items (donde la regla es tipo qty_per_person) insert into product_qty_rules (tenant_id, woo_product_id, event_type, person_type, qty_per_person, unit) select r.tenant_id, i.woo_product_id, coalesce(r.trigger_event, 'asado') as event_type, coalesce(i.audience_type, 'adult') as person_type, i.qty_per_person, coalesce(i.unit, 'kg') as unit from reco_rule_items i inner join product_reco_rules r on r.id = i.rule_id where r.rule_type = 'qty_per_person' and i.qty_per_person is not null on conflict (tenant_id, woo_product_id, event_type, person_type) do update set qty_per_person = excluded.qty_per_person, unit = excluded.unit, updated_at = now(); -- migrate:down drop table if exists product_qty_rules;