CREATE TABLE service_binding_ips (
    binding_id INTEGER NOT NULL REFERENCES service_bindings(id) ON DELETE CASCADE,
    ip TEXT NOT NULL,
    PRIMARY KEY (binding_id, ip)
);

CREATE INDEX idx_service_binding_ips_binding_id ON service_binding_ips(binding_id);

INSERT INTO service_binding_ips (binding_id, ip)
SELECT sbr.binding_id, dr.content
FROM service_binding_records sbr
JOIN dns_records dr ON dr.id = sbr.dns_record_id
WHERE dr.record_type = 'A'
  AND dr.content IS NOT NULL
  AND TRIM(dr.content) != ''
ON CONFLICT(binding_id, ip) DO NOTHING;

INSERT INTO service_binding_ips (binding_id, ip)
SELECT sb.id, dr.content
FROM service_bindings sb
JOIN dns_records dr ON dr.id = sb.dns_record_id
WHERE dr.record_type = 'A'
  AND dr.content IS NOT NULL
  AND TRIM(dr.content) != ''
ON CONFLICT(binding_id, ip) DO NOTHING;
