Skip to content

Commit

Permalink
chore: fix sanitize-hypen migration #713
Browse files Browse the repository at this point in the history
chore: fix sanitize-hypen migration
  • Loading branch information
GPaoloni authored Aug 5, 2024
2 parents cbaec2f + 7afbbfb commit a4fb74c
Showing 1 changed file with 28 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,74 +19,53 @@
module.exports = {
up: async queryInterface => {
await queryInterface.sequelize.query(`
-- Fix the contacts for the conflicting records
UPDATE "Contacts" SET "profileId" = sanitized."targetProfile", "identifierId" = sanitized."targetId"
FROM (
BEGIN;
CREATE TEMPORARY TABLE "sanitized" AS (
--- All identifiers with conflicts, plus the "target id" which is the minimal id that matches the sanitized identifier
SELECT "main"."id" AS "conflictId", "main"."accountSid", "grouped"."targetId", "p2i"."profileId" AS "targetProfile"
FROM "Identifiers" "main"
INNER JOIN (
SELECT "idx"."accountSid", replace("idx"."identifier", '-', '') AS "sanitized", MIN("idx"."id") AS "targetId" FROM "Identifiers" "idx"
JOIN "Contacts" "contacts" ON "idx"."id" = "contacts"."identifierId"
WHERE "idx"."identifier" LIKE '%-%' AND "contacts"."channel" IN ('voice', 'whatsapp', 'sms', 'modica')
GROUP BY "idx"."accountSid", replace("idx"."identifier", '-', '') HAVING COUNT(*) > 1
GROUP BY "idx"."accountSid", replace("idx"."identifier", '-', '') HAVING COUNT(*) > 1
) "grouped" ON replace("main"."identifier", '-', '')="grouped"."sanitized" AND "main"."accountSid"="grouped"."accountSid"
INNER JOIN "Contacts" "contacts" ON "main"."id" = "contacts"."identifierId" AND "main"."accountSid" = "contacts"."accountSid"
INNER JOIN "ProfilesToIdentifiers" "p2i" ON "p2i"."identifierId" = "grouped"."targetId"
WHERE "contacts"."channel" IN ('voice', 'whatsapp', 'sms', 'modica')
ORDER BY replace("identifier", '-', '')
) AS sanitized WHERE "identifierId" = sanitized."conflictId" AND "identifierId" != sanitized."targetId"
`);
console.log('Contacts fixed');
);
-- Fix the contacts for the conflicting records
UPDATE "Contacts"
SET "profileId" = "sanitized"."targetProfile", "identifierId" = "sanitized"."targetId"
FROM "sanitized"
WHERE "identifierId" = "sanitized"."conflictId" AND "identifierId" != sanitized."targetId";
await queryInterface.sequelize.query(`
DELETE FROM "Profiles" WHERE "id" IN (
SELECT "p2i"."profileId" AS "conflictProfileId"
FROM (
--- All identifiers with conflicts, plus the "target id" which is the minimal id that matches the sanitized identifier
SELECT "main"."id" AS "conflictId", "main"."accountSid", "grouped"."targetId", "p2i"."profileId" AS "targetProfile"
FROM "Identifiers" "main"
INNER JOIN (
SELECT "idx"."accountSid", replace("idx"."identifier", '-', '') AS "sanitized", MIN("idx"."id") AS "targetId" FROM "Identifiers" "idx"
JOIN "Contacts" "contacts" ON "idx"."id" = "contacts"."identifierId"
WHERE "idx"."identifier" LIKE '%-%' AND "contacts"."channel" IN ('voice', 'whatsapp', 'sms', 'modica')
GROUP BY "idx"."accountSid", replace("idx"."identifier", '-', '') HAVING COUNT(*) > 1
) "grouped" ON replace("main"."identifier", '-', '')="grouped"."sanitized" AND "main"."accountSid"="grouped"."accountSid"
INNER JOIN "ProfilesToIdentifiers" "p2i" ON "p2i"."identifierId" = "grouped"."targetId"
ORDER BY replace("identifier", '-', '')
) AS "sanitized"
FROM "sanitized"
INNER JOIN "ProfilesToIdentifiers" "p2i" ON "p2i"."identifierId" = "sanitized"."conflictId" AND "p2i"."identifierId" != "sanitized"."targetId"
)
`);
console.log('Conflicting profiles deleted');

await queryInterface.sequelize.query(`
);
DELETE FROM "Identifiers" WHERE "id" IN (
SELECT "idx"."id" AS "conflictIdentifierId"
FROM (
--- All identifiers with conflicts, plus the "target id" which is the minimal id that matches the sanitized identifier
SELECT "main"."id" AS "conflictId", "grouped"."targetId"
FROM "Identifiers" "main"
INNER JOIN (
SELECT "idx"."accountSid", replace("idx"."identifier", '-', '') AS "sanitized", MIN("idx"."id") AS "targetId" FROM "Identifiers" "idx"
JOIN "Contacts" "contacts" ON "idx"."id" = "contacts"."identifierId"
WHERE "idx"."identifier" LIKE '%-%' AND "contacts"."channel" IN ('voice', 'whatsapp', 'sms', 'modica')
GROUP BY "idx"."accountSid", replace("idx"."identifier", '-', '') HAVING COUNT(*) > 1
) "grouped" ON replace("main"."identifier", '-', '')="grouped"."sanitized" AND "main"."accountSid"="grouped"."accountSid"
ORDER BY replace("identifier", '-', '')
) AS "sanitized"
FROM "sanitized"
INNER JOIN "Identifiers" "idx" ON "idx"."id" = "sanitized"."conflictId" AND "idx"."id" != "sanitized"."targetId"
)
`);
console.log('Conflicting identifiers deleted');
);
await queryInterface.sequelize.query(`
UPDATE "Identifiers" "idx" SET "identifier" = replace("idx"."identifier", '-', '')
FROM (
SELECT DISTINCT identifiers.* FROM "Identifiers" identifiers
JOIN "Contacts" contacts ON identifiers.id = contacts."identifierId"
WHERE identifiers.identifier LIKE '%-%' AND contacts."channel" IN ('voice', 'whatsapp', 'sms', 'modica')
) AS "hyphened" WHERE "idx"."id" = "hyphened"."id"
SELECT DISTINCT "identifiers".*
FROM "Identifiers" "identifiers"
JOIN "Contacts" "contacts" ON "identifiers"."id" = "contacts"."identifierId" AND "identifiers"."accountSid" = "contacts"."accountSid"
WHERE "identifiers"."identifier" LIKE '%-%' AND "contacts"."channel" IN ('voice', 'whatsapp', 'sms', 'modica')
) AS "hyphened"
WHERE "idx"."id" = "hyphened"."id";
DROP TABLE IF EXISTS "sanitized";
COMMIT;
`);
console.log('hyphened identifiers sanitized');
},

down: async () => {},
Expand Down

0 comments on commit a4fb74c

Please sign in to comment.