diff --git a/migrations/1729121498808_add-null-not-distinct-to-email-domains.cjs b/migrations/1729121498808_add-null-not-distinct-to-email-domains.cjs new file mode 100644 index 00000000..6ea6f527 --- /dev/null +++ b/migrations/1729121498808_add-null-not-distinct-to-email-domains.cjs @@ -0,0 +1,40 @@ +exports.shorthands = undefined; + +exports.up = async (pgm) => { + await pgm.db.query(` + WITH cte AS ( + SELECT id, + ROW_NUMBER() + OVER (PARTITION BY organization_id, domain, verification_type ORDER BY updated_at DESC) AS rn + FROM email_domains + ) + DELETE + FROM email_domains + WHERE id IN ( + SELECT id + FROM cte + WHERE rn > 1 + ); + `); + await pgm.db.query(` + ALTER TABLE email_domains + DROP CONSTRAINT unique_organization_domain; + `); + await pgm.db.query(` + ALTER TABLE email_domains + ADD CONSTRAINT unique_organization_domain + UNIQUE NULLS NOT DISTINCT (organization_id, domain, verification_type); + `); +}; + +exports.down = async (pgm) => { + await pgm.db.query(` + ALTER TABLE email_domains + DROP CONSTRAINT unique_organization_domain; + `); + await pgm.db.query(` + ALTER TABLE email_domains + ADD CONSTRAINT unique_organization_domain + UNIQUE (organization_id, domain, verification_type); + `); +};