SET lock_timeout = '10s';
SET statement_timeout = '120000';

CREATE EXTENSION IF NOT EXISTS btree_gist;

ALTER TABLE catalog.inventario_variantes_categoria
    DROP CONSTRAINT IF EXISTS inventario_variantes_categori_business_id_id_item_coalesce_excl;

ALTER TABLE catalog.inventario_variantes_categoria
    DROP CONSTRAINT IF EXISTS inventario_variantes_categoria_non_promotional_slot_overlap_excl;

ALTER TABLE catalog.inventario_variantes_categoria
    ADD CONSTRAINT inventario_variantes_categoria_non_promotional_slot_overlap_excl
    EXCLUDE USING gist (
        (business_id::text) WITH =,
        id_item WITH =,
        COALESCE(category_id, '-1'::integer) WITH =,
        tstzrange(
            COALESCE(vigencia_inicio, '-infinity'::timestamp with time zone),
            COALESCE(vigencia_fin, 'infinity'::timestamp with time zone),
            '[]'
        ) WITH &&
    )
    WHERE (
        is_active = TRUE
        AND soft_delete IS NOT TRUE
        AND es_promocion IS NOT TRUE
    );

ANALYZE catalog.inventario_variantes_categoria;
