--
-- Migration: Change mappings.input_value from TEXT to JSONB
-- Allows multi-capture-group regex results to be used as mapping keys
--

SET search_path TO dataflow, public;

-- Drop dependent constraint and index first
ALTER TABLE dataflow.mappings DROP CONSTRAINT mappings_source_name_rule_name_input_value_key;
DROP INDEX IF EXISTS dataflow.idx_mappings_input;

-- Convert column: existing TEXT values become JSONB strings e.g. "MEIJER"
ALTER TABLE dataflow.mappings
    ALTER COLUMN input_value TYPE JSONB
    USING to_jsonb(input_value);

-- Recreate constraint and index
ALTER TABLE dataflow.mappings
    ADD CONSTRAINT mappings_source_name_rule_name_input_value_key
    UNIQUE (source_name, rule_name, input_value);

CREATE INDEX idx_mappings_input ON dataflow.mappings(source_name, rule_name, input_value);
