Skip to content

Instantly share code, notes, and snippets.

@erikhansen
Last active October 24, 2019 19:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikhansen/aa30947f2df263a95a054d51c5277692 to your computer and use it in GitHub Desktop.
Save erikhansen/aa30947f2df263a95a054d51c5277692 to your computer and use it in GitHub Desktop.
Sanitize customer emails from Magento database. UPDATE: Use this instead: https://github.com/elgentos/masquerade
/*
Run the following query to get a set of queries that will purge all tables of email addresses. The queries that are output from
this should be manually reviewed to remove queries for any unnecessary tables and can then be run manually or via a Magerun "db:query"
to include it as a part of a scripted cloning process.
IMPORTANT: Make sure to update the @db_name variable
What the resulting query will do:
-- Replace the emails in Magento with dummy emails (unless email is one of the whitelisted domains) in order to prevent emails erroneously being sent to customers.
-- Make all emails with @example.com and use and MD5 of the original domain of the email as the tag for the email. This is important in case we have two emails with the
-- same "local part" but different "domain part". For example, bob.smith@yahoo.com would become bob.smith+c9d12f@example.com
Credit to ericthehacker for the bulk of this script.
*/
SET @db_name = 'example_dev';
SET @whitelist_domain_1 = 'krakencommerce.com';
-- If you don't want to replace email addresses for the merchant, enter their domain here
SET @whitelist_domain_2 = 'merchantdomain.com';
USE information_schema;
SET SESSION group_concat_max_len=10000000;
SELECT
group_concat(
concat('UPDATE `',`table_name`,'` SET `',`column_name`,'` = REPLACE(`',`column_name`,'`, SUBSTRING(`',`column_name`,'`, LOCATE("@", `',`column_name`,'`)), CONCAT("+", SUBSTRING(MD5(SUBSTRING(`',`column_name`,'`, LOCATE("@", `',`column_name`,'`))) FROM 1 FOR 6), "@example.com")) WHERE `',`column_name`,'` NOT LIKE "%@', @whitelist_domain_1, '" ', 'AND `', `column_name`, '` NOT LIKE "%@', @whitelist_domain_2, '";', "\n") SEPARATOR '') AS q
FROM `columns` AS c
WHERE table_schema = @db_name
AND `column_name`
LIKE
'%email%'
AND `data_type` IN ('varchar', 'text');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment