ALTER TABLE module
    ADD COLUMN doh_resolver_policy TEXT NOT NULL DEFAULT 'primary_only'
        CHECK (doh_resolver_policy IN ('primary_only', 'failover', 'union'));

CREATE TABLE module_doh_profile (
    module_id      TEXT NOT NULL REFERENCES module (id) ON DELETE CASCADE,
    doh_profile_id TEXT NOT NULL REFERENCES doh_profile (id) ON DELETE CASCADE,
    sort_order     INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY (module_id, doh_profile_id)
);

CREATE INDEX idx_module_doh_profile_doh ON module_doh_profile (doh_profile_id);

INSERT INTO module_doh_profile (module_id, doh_profile_id, sort_order)
SELECT id, doh_profile_id, 0
FROM module
WHERE doh_profile_id IS NOT NULL
  AND deleted_at IS NULL;
