barclay
barclay

Reputation: 4552

How would I write this SQL script?

I have a dev database that I periodically rebuild from a data dump of the prod database. After doing the initial rebuild, I have a cleanup script that I run that does a few things, notably changing all emails to a dev address so our customers don't get spammed inadvertently when we develop. This worked fine until I changed the structure of our db so that each customer can have multiple emails, with a unique constraint disallowing the same email multiple times per customer. Now I am trying to rewrite my cleanup script to change all emails to "[email protected]" but it's failing because of the unique constraint.

I don't want remove the unique constraint on my dev db for obvious reasons. I need to figure out a method to assign fake but unique emails for the customers that have multiple contacts.

Here's my data structure:

CREATE TABLE contact (
    contactid decimal(22,0) DEFAULT '0' NOT NULL,
    ...,
    PRIMARY KEY (contactid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE contact_email (
    contactemailid decimal(22,0) DEFAULT '0' NOT NULL,
    contactid decimal(22,0) DEFAULT '0' NOT NULL,
    emailaddress varchar(255) COLLATE latin1_bin NOT NULL,
    PRIMARY KEY (contactemailid),
    CONSTRAINT ce_contactid_fk FOREIGN KEY (contactid) REFERENCES contact (contactid) ON DELETE NO ACTION ON UPDATE NO ACTION,
    INDEX ce_contactid_fk (contactid),
    CONSTRAINT ce_uniquecombokey UNIQUE KEY (contactid, emailaddress)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE merchant_contact (
    merchantcontactid decimal(22,0) DEFAULT '0' NOT NULL,
    merchantacctid decimal(22,0) DEFAULT '0' NOT NULL,
    contactid decimal(22,0) DEFAULT '0' NOT NULL,
    billingcontact tinyint(1) default '0' NOT NULL,
    PRIMARY KEY (merchantcontactid),
    CONSTRAINT mc_contactid_fk FOREIGN KEY (contactid) REFERENCES contact (contactid) ON DELETE NO ACTION ON UPDATE NO ACTION,
    INDEX mc_contactid_fk (contactid),
    CONSTRAINT mc_uniquecombokey UNIQUE KEY (merchantacctid, billingcontact)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Before I allowed multiple emails per customer, this part of my cleanup script was easy:

update contact_email set emailaddress = '[email protected]';

Now it fails because of the constraint. I get an Error Code: 1062, SQL State: 23000 (integrity constraint violation).

Any suggestions? Thanks in advance. I'm going to continue to hammer on it myself, but would appreciate suggestions/ideas from experienced SQL scripters.

ETA: Thanks for the ideas so far. One thing I left out is that the fake address that I currently set them all to, actually does come to me, so I can see the email when I am testing. I am asking our ops group if something like 'dev-{contactemailid}@abc.com' could be configured to all come to me, but I'm not too optimistic.

Upvotes: 1

Views: 83

Answers (1)

Sebas
Sebas

Reputation: 21542

update contact_email set emailaddress = CONCAT(contactemailid, '[email protected]')

Upvotes: 2

Related Questions