CREATE TABLE asn_prefix_cache_row (
    asn     BIGINT NOT NULL REFERENCES asn_prefix_cache (asn) ON DELETE CASCADE,
    prefix  CIDR NOT NULL,
    PRIMARY KEY (asn, prefix)
);

INSERT INTO asn_prefix_cache_row (asn, prefix)
SELECT apc.asn, t.elem::cidr
FROM asn_prefix_cache apc
CROSS JOIN LATERAL jsonb_array_elements_text(apc.prefixes_json) AS t(elem)
WHERE jsonb_typeof(apc.prefixes_json) = 'array'
  AND jsonb_array_length(apc.prefixes_json) > 0;

ALTER TABLE asn_prefix_cache DROP COLUMN prefixes_json;
