@hogsend/engine 0.58.0 → 0.60.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -11
- package/src/buckets/bucket-access.ts +19 -10
- package/src/buckets/check-membership.ts +61 -8
- package/src/buckets/membership-epoch.ts +11 -1
- package/src/campaigns/cohort-sql.ts +3 -0
- package/src/index.ts +26 -6
- package/src/journeys/execute-journey-run.ts +180 -29
- package/src/journeys/journey-context.ts +43 -7
- package/src/lib/agent/tools.ts +29 -4
- package/src/lib/attribution-backfill.ts +27 -4
- package/src/lib/attribution.ts +22 -3
- package/src/lib/connector-actions.ts +4 -1
- package/src/lib/contacts.ts +518 -148
- package/src/lib/conversion-dispatch.ts +16 -2
- package/src/lib/enrollment-guards.ts +19 -12
- package/src/lib/feed.ts +5 -5
- package/src/lib/flags.ts +26 -9
- package/src/lib/ingestion.ts +23 -2
- package/src/lib/journey-lift.ts +17 -5
- package/src/lib/preferences.ts +116 -30
- package/src/lib/recipient-preferences.ts +16 -2
- package/src/lib/revenue.ts +20 -5
- package/src/lib/sms-tracked.ts +18 -1
- package/src/lib/tracked.ts +50 -8
- package/src/lib/tracking-events.ts +28 -0
- package/src/lib/unique-violation.ts +44 -0
- package/src/routes/admin/buckets.ts +20 -2
- package/src/routes/admin/bulk.ts +18 -2
- package/src/routes/admin/contacts.ts +4 -3
- package/src/routes/admin/conversions.ts +20 -4
- package/src/routes/admin/emails.ts +17 -2
- package/src/routes/admin/events.ts +32 -41
- package/src/routes/admin/funnels.ts +6 -1
- package/src/routes/admin/groups.ts +4 -0
- package/src/routes/admin/impact.ts +30 -10
- package/src/routes/admin/index.ts +2 -0
- package/src/routes/admin/journey-impact.ts +18 -9
- package/src/routes/admin/journeys.ts +8 -1
- package/src/routes/admin/maintenance.ts +216 -0
- package/src/routes/admin/preferences.ts +85 -38
- package/src/routes/admin/reporting.ts +25 -5
- package/src/routes/admin/targeting.ts +1 -0
- package/src/routes/admin/timeline.ts +16 -10
- package/src/routes/email/preferences.ts +18 -1
- package/src/routes/lists/index.ts +11 -6
- package/src/worker.ts +20 -0
- package/src/workflows/backfill-contact-id.ts +951 -0
- package/src/workflows/bucket-backfill.ts +145 -59
- package/src/workflows/bucket-reconcile.ts +154 -50
- package/src/workflows/check-alerts.ts +4 -0
- package/src/workflows/impact-digest.ts +10 -3
- package/src/workflows/journey-blueprint-interpreter.ts +8 -1
- package/src/workflows/send-campaign.ts +18 -11
package/src/lib/contacts.ts
CHANGED
|
@@ -13,11 +13,13 @@ import {
|
|
|
13
13
|
} from "@hogsend/db";
|
|
14
14
|
import {
|
|
15
15
|
and,
|
|
16
|
+
type Column,
|
|
16
17
|
eq,
|
|
17
18
|
ilike,
|
|
18
19
|
inArray,
|
|
19
20
|
isNotNull,
|
|
20
21
|
isNull,
|
|
22
|
+
not,
|
|
21
23
|
or,
|
|
22
24
|
type SQL,
|
|
23
25
|
sql,
|
|
@@ -804,6 +806,111 @@ export function contactKeySql() {
|
|
|
804
806
|
return sql<string>`coalesce(${contacts.externalId}, ${contacts.anonymousId}, ${contacts.id}::text)`;
|
|
805
807
|
}
|
|
806
808
|
|
|
809
|
+
/**
|
|
810
|
+
* The ADOPTION STAMP fragment: `coalesce(<table>.contact_id, :contactId)`.
|
|
811
|
+
*
|
|
812
|
+
* Every statement below that moves a history row's mutable `user_id` string
|
|
813
|
+
* onto an absorbing contact's key ALSO stamps that contact's uuid — so the
|
|
814
|
+
* later read-flip onto `contact_id` leaves no adopted row behind. Two
|
|
815
|
+
* properties make this fragment (rather than a bare `contact_id = :id`) the
|
|
816
|
+
* only correct shape:
|
|
817
|
+
*
|
|
818
|
+
* - SECURITY, not optimization. The `coalesce` is a NULL guard: it stamps only
|
|
819
|
+
* rows nobody owns yet (written while the contact did not exist — the whole
|
|
820
|
+
* point of refusing to mint on observation). A row already carrying ANOTHER
|
|
821
|
+
* contact's id is left alone, so a mis-gated adoption can never re-parent a
|
|
822
|
+
* second person's history even if it does move the string key.
|
|
823
|
+
* - It must ride IN the same UPDATE that rewrites `user_id`. A separate stamp
|
|
824
|
+
* statement afterwards matches zero rows — the key it would filter on has
|
|
825
|
+
* already moved — and one before it would need its own scan of the old key.
|
|
826
|
+
*
|
|
827
|
+
* Idempotent by construction: re-running an adoption finds `contact_id` already
|
|
828
|
+
* non-NULL and changes nothing.
|
|
829
|
+
*
|
|
830
|
+
* The `::uuid` cast pins the bound parameter's type the way {@link
|
|
831
|
+
* contactKeySql}'s `::text` does; without it the param arrives untyped and
|
|
832
|
+
* Postgres has to infer it from the coalesce.
|
|
833
|
+
*/
|
|
834
|
+
function adoptedContactId(column: Column, contactId: string): SQL<string> {
|
|
835
|
+
return sql<string>`coalesce(${column}, ${contactId}::uuid)`;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* How a fold writes its routed rows (PRD 05 T9).
|
|
840
|
+
*
|
|
841
|
+
* - Merge mode (default): the historical shape — rewrite `user_id` onto the
|
|
842
|
+
* survivor key, stamp `contact_id` under the {@link adoptedContactId} NULL
|
|
843
|
+
* guard, denormalize the survivor's email.
|
|
844
|
+
* - `stampOnly` (the adoption path): one column. The loser scope is
|
|
845
|
+
* `orphansOnly` (see {@link foldScopes}), so every matched row carries
|
|
846
|
+
* `contact_id IS NULL` and a plain stamp cannot re-parent another person's
|
|
847
|
+
* history. `user_id` is frozen at its write-time value — reads no longer
|
|
848
|
+
* consult it, and D9 keeps writing it on inserts.
|
|
849
|
+
*/
|
|
850
|
+
interface FoldWriteOpts {
|
|
851
|
+
stampOnly?: boolean;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* PRD 05 T3 — "which rows belong to the loser, and which to the survivor", for
|
|
856
|
+
* the three folds below.
|
|
857
|
+
*
|
|
858
|
+
* Migration 0071 adds a CONTACT-scoped partial unique index to `journey_states`,
|
|
859
|
+
* `bucket_memberships` and `email_preferences`. That makes a string-key-only
|
|
860
|
+
* fold unsafe: adoption stamps `contact_id` WITHOUT rewriting `user_id`, so a
|
|
861
|
+
* row the SURVIVOR already owns can sit under a stale key. A fold that looked
|
|
862
|
+
* only at `user_id = survivorKey` would not see it, would call the slot free,
|
|
863
|
+
* and would re-point (or NULL-stamp) a loser row into a second row satisfying
|
|
864
|
+
* the new index — a 23505 raised inside `resolveContact`'s transaction with no
|
|
865
|
+
* handler, which wedges identity resolution for that person permanently (every
|
|
866
|
+
* retry fails identically). So ownership is asked BOTH ways, `user_id` and
|
|
867
|
+
* `contact_id`.
|
|
868
|
+
*
|
|
869
|
+
* `loserId` is the mirror on the other side and is supplied only by the merge
|
|
870
|
+
* path: the adoption path ({@link adoptOrphanHistory}) folds history onto the
|
|
871
|
+
* contact that already owns its key, where the "loser" rows would carry that
|
|
872
|
+
* same `contact_id` and matching on it would swallow the whole contact. The
|
|
873
|
+
* loser test wins the tie — a row is either being adopted or being folded
|
|
874
|
+
* into, never both.
|
|
875
|
+
*
|
|
876
|
+
* `orphansOnly` (the adoption path, PRD 05 T9): the loser side additionally
|
|
877
|
+
* requires `contact_id IS NULL`. This predicate is the anti-theft guard that
|
|
878
|
+
* replaced `keysAnotherContact`'s adoption-gating role — a row another contact
|
|
879
|
+
* owns already carries that owner's id and can never match, so adoption
|
|
880
|
+
* cannot re-parent a second person's history. Never combined with `loserId`
|
|
881
|
+
* (only the merge path supplies one).
|
|
882
|
+
*/
|
|
883
|
+
function foldScopes(opts: {
|
|
884
|
+
userIdCol: Column;
|
|
885
|
+
contactIdCol: Column;
|
|
886
|
+
survivorKey: string;
|
|
887
|
+
survivorId: string;
|
|
888
|
+
loserKeys: string[];
|
|
889
|
+
loserId?: string;
|
|
890
|
+
orphansOnly?: boolean;
|
|
891
|
+
}): { isLoser: SQL; isSurvivor: SQL } {
|
|
892
|
+
// `IS NOT DISTINCT FROM`, never `=`. `contact_id` is nullable, and a plain
|
|
893
|
+
// equality against a NULL column is UNKNOWN, not false — which `NOT (… OR
|
|
894
|
+
// UNKNOWN)` then swallows, silently dropping EVERY contactless row out of the
|
|
895
|
+
// survivor set. That is the occupancy the folds dedupe against, so the folds
|
|
896
|
+
// would stop seeing the rows they exist to protect.
|
|
897
|
+
const sameContact = (id: string): SQL =>
|
|
898
|
+
sql`${opts.contactIdCol} IS NOT DISTINCT FROM ${id}::uuid`;
|
|
899
|
+
|
|
900
|
+
const byKey = opts.orphansOnly
|
|
901
|
+
? (and(
|
|
902
|
+
inArray(opts.userIdCol, opts.loserKeys),
|
|
903
|
+
isNull(opts.contactIdCol),
|
|
904
|
+
) as SQL)
|
|
905
|
+
: inArray(opts.userIdCol, opts.loserKeys);
|
|
906
|
+
const isLoser = opts.loserId ? or(byKey, sameContact(opts.loserId)) : byKey;
|
|
907
|
+
const isSurvivor = and(
|
|
908
|
+
or(eq(opts.userIdCol, opts.survivorKey), sameContact(opts.survivorId)),
|
|
909
|
+
not(isLoser as SQL),
|
|
910
|
+
);
|
|
911
|
+
return { isLoser: isLoser as SQL, isSurvivor: isSurvivor as SQL };
|
|
912
|
+
}
|
|
913
|
+
|
|
807
914
|
/**
|
|
808
915
|
* The identity keys + options EVERY resolve entry point accepts. Named (rather
|
|
809
916
|
* than inline) so {@link resolveOrCreateContact} and {@link
|
|
@@ -1106,35 +1213,36 @@ async function resolveContactShared(
|
|
|
1106
1213
|
|
|
1107
1214
|
// HISTORY ADOPTION (D2). Once observation stops minting a row, a later
|
|
1108
1215
|
// identify no longer finds an anon contact to fill-in-link — it lands
|
|
1109
|
-
// HERE, in the create arm
|
|
1110
|
-
//
|
|
1111
|
-
//
|
|
1112
|
-
//
|
|
1216
|
+
// HERE, in the create arm. Everything already keyed on the anon id
|
|
1217
|
+
// (user_events, journey_states, bucket_memberships, email_sends,
|
|
1218
|
+
// email_preferences — incl. an unsubscribe) would otherwise be silently
|
|
1219
|
+
// orphaned under a key no contact owns.
|
|
1113
1220
|
//
|
|
1114
|
-
//
|
|
1115
|
-
//
|
|
1116
|
-
//
|
|
1117
|
-
//
|
|
1118
|
-
//
|
|
1119
|
-
//
|
|
1120
|
-
// Gated on `keysAnotherContact`: resolution missed this value in the
|
|
1121
|
-
// ANONYMOUS namespace, which does not mean nobody owns it. If it is some
|
|
1122
|
-
// other live contact's canonical key, those rows are theirs and adopting
|
|
1123
|
-
// them here would hand one person's history to another.
|
|
1221
|
+
// Adoption is self-gating (T9): `adoptOrphanHistory` touches only
|
|
1222
|
+
// `contact_id IS NULL` rows, so a value that is some other live
|
|
1223
|
+
// contact's key cannot surrender their history — those rows carry their
|
|
1224
|
+
// owner's id. `keysAnotherContact` therefore no longer gates adoption;
|
|
1225
|
+
// it survives below ONLY to gate the `mergedKeys` REPORT — a foreign
|
|
1226
|
+
// anon id must never enter the analytics stitch.
|
|
1124
1227
|
const createdKey = contactKey(createdRow);
|
|
1228
|
+
// History written under the row's OWN key BEFORE it existed. Not a
|
|
1229
|
+
// merge: no key is absorbed, so it reports nothing in `mergedKeys` and
|
|
1230
|
+
// fires no analytics stitch.
|
|
1231
|
+
await adoptOrphanHistory(tx, createdKey, createdRow);
|
|
1125
1232
|
let mergedKeys: string[] | undefined;
|
|
1126
|
-
if (
|
|
1127
|
-
|
|
1128
|
-
anonymousId
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
await
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1233
|
+
if (anonymousId && anonymousId !== createdKey) {
|
|
1234
|
+
// The anon key was just superseded as canonical — stamp its orphans.
|
|
1235
|
+
// Only ever FROM `anonymousId`: an email/discord-shaped key is never
|
|
1236
|
+
// canonical, so it never keyed history.
|
|
1237
|
+
await adoptOrphanHistory(tx, anonymousId, createdRow);
|
|
1238
|
+
if (!(await keysAnotherContact(tx, anonymousId, createdRow.id))) {
|
|
1239
|
+
// §5.3 emission point 2 (canonical-key flip): report the absorbed
|
|
1240
|
+
// anon key so `ingestEvent` still fires `mergeAnalyticsIdentities({
|
|
1241
|
+
// reason: "key_flip" })` — otherwise the DB history is adopted but
|
|
1242
|
+
// the PostHog anon→known stitch stays broken. Safe to alias by
|
|
1243
|
+
// construction (an anonymous key never identified a person — MF-2).
|
|
1244
|
+
mergedKeys = [anonymousId];
|
|
1245
|
+
}
|
|
1138
1246
|
}
|
|
1139
1247
|
|
|
1140
1248
|
return {
|
|
@@ -1495,9 +1603,9 @@ async function fillInLink(
|
|
|
1495
1603
|
|
|
1496
1604
|
await tx.update(contacts).set(set).where(eq(contacts.id, row.id));
|
|
1497
1605
|
|
|
1498
|
-
//
|
|
1499
|
-
//
|
|
1500
|
-
//
|
|
1606
|
+
// Adopt the contact's own orphan history (stamp-only since T9; nothing
|
|
1607
|
+
// rewrites the string key anymore). The updated row carries the post-fill
|
|
1608
|
+
// keys the fold scopes resolve ownership with.
|
|
1501
1609
|
const newKey = nextExternalId ?? nextAnonymousId ?? row.id;
|
|
1502
1610
|
// §5.3 emission point 2 (canonical-key flip): when the key flips, the OLD key
|
|
1503
1611
|
// is folded into the NEW one. MF-3 gate — only emit a merge when `oldKey` was
|
|
@@ -1518,9 +1626,12 @@ async function fillInLink(
|
|
|
1518
1626
|
|
|
1519
1627
|
let mergedKeys: string[] | undefined;
|
|
1520
1628
|
let mergedIdentifiedKeys: string[] | undefined;
|
|
1629
|
+
// T9: adoption is unconditional — with nothing keyed on the canonical key,
|
|
1630
|
+
// "did the key flip" no longer decides whether history is stamped. Orphans
|
|
1631
|
+
// under the pre-fill key are adopted either way (idempotent when there are
|
|
1632
|
+
// none); the flip test now gates only the analytics REPORT.
|
|
1633
|
+
await adoptOrphanHistory(tx, oldKey, updatedRow);
|
|
1521
1634
|
if (newKey !== oldKey) {
|
|
1522
|
-
await repointOwnHistory(tx, oldKey, newKey, updatedRow);
|
|
1523
|
-
|
|
1524
1635
|
const oldKeyWasExternalId =
|
|
1525
1636
|
row.externalId != null && oldKey === row.externalId;
|
|
1526
1637
|
if (oldKeyWasExternalId) {
|
|
@@ -1568,7 +1679,7 @@ async function fillInLink(
|
|
|
1568
1679
|
for (const key of claimed) {
|
|
1569
1680
|
if (key.kind !== "anonymous") continue;
|
|
1570
1681
|
if (key.value === newKey || key.value === oldKey) continue;
|
|
1571
|
-
await
|
|
1682
|
+
await adoptOrphanHistory(tx, key.value, updatedRow);
|
|
1572
1683
|
// Reported so `mergeAnalyticsIdentities` still fires the anon→known stitch;
|
|
1573
1684
|
// appended, since a key flip above may already have folded a uuid/anon key.
|
|
1574
1685
|
mergedKeys = [...(mergedKeys ?? []), key.value];
|
|
@@ -1630,22 +1741,38 @@ async function mergeContacts(
|
|
|
1630
1741
|
safeLoserKeys.push(loser.anonymousId ?? loser.id);
|
|
1631
1742
|
}
|
|
1632
1743
|
|
|
1633
|
-
// (ii) user_events.user_id rewrite.
|
|
1744
|
+
// (ii) user_events.user_id rewrite. The paired `contact_id` stamp is the
|
|
1745
|
+
// ADOPTION half only (NULL rows — history the loser's keys accumulated
|
|
1746
|
+
// before any contact existed). Rows already owned by the LOSER keep
|
|
1747
|
+
// `loser.id` here and are re-pointed wholesale by (vi-b-hist) below, which
|
|
1748
|
+
// is the one statement that may overwrite a non-NULL owner. Splitting it
|
|
1749
|
+
// that way keeps a single rule in the fragment (never take a row from
|
|
1750
|
+
// another contact) and one explicit, auditable place where a merge does.
|
|
1634
1751
|
await tx
|
|
1635
1752
|
.update(userEvents)
|
|
1636
|
-
.set({
|
|
1753
|
+
.set({
|
|
1754
|
+
userId: survivorKey,
|
|
1755
|
+
contactId: adoptedContactId(userEvents.contactId, survivor.id),
|
|
1756
|
+
})
|
|
1637
1757
|
.where(inArray(userEvents.userId, loserKeysToRewrite));
|
|
1638
1758
|
|
|
1639
1759
|
// (iii) journey_states — exit the loser's duplicate active/waiting row when
|
|
1640
1760
|
// the survivor already holds an active/waiting row in the same journey
|
|
1641
1761
|
// (respect uq_user_journey_active), THEN rewrite user_id/user_email.
|
|
1642
|
-
await foldJourneyStates(
|
|
1762
|
+
await foldJourneyStates(
|
|
1763
|
+
tx,
|
|
1764
|
+
survivorKey,
|
|
1765
|
+
loserKeysToRewrite,
|
|
1766
|
+
survivor,
|
|
1767
|
+
loser.id,
|
|
1768
|
+
);
|
|
1643
1769
|
|
|
1644
1770
|
// (iv) email_sends rewrite user_id + userEmail to survivor's.
|
|
1645
1771
|
await tx
|
|
1646
1772
|
.update(emailSends)
|
|
1647
1773
|
.set({
|
|
1648
1774
|
userId: survivorKey,
|
|
1775
|
+
contactId: adoptedContactId(emailSends.contactId, survivor.id),
|
|
1649
1776
|
...(survivor.email ? { userEmail: survivor.email } : {}),
|
|
1650
1777
|
})
|
|
1651
1778
|
.where(inArray(emailSends.userId, loserKeysToRewrite));
|
|
@@ -1653,17 +1780,29 @@ async function mergeContacts(
|
|
|
1653
1780
|
// (v) bucket_memberships — soft-leave the loser's duplicate active
|
|
1654
1781
|
// membership when the survivor already holds one in the same bucket (respect
|
|
1655
1782
|
// uq_user_bucket_active, preserve survivor's dwell clock), THEN rewrite.
|
|
1656
|
-
await foldBucketMemberships(
|
|
1783
|
+
await foldBucketMemberships(
|
|
1784
|
+
tx,
|
|
1785
|
+
survivorKey,
|
|
1786
|
+
loserKeysToRewrite,
|
|
1787
|
+
survivor.id,
|
|
1788
|
+
loser.id,
|
|
1789
|
+
);
|
|
1657
1790
|
|
|
1658
1791
|
// (vi) email_preferences FOLD (never blind-rewrite — risk 6).
|
|
1659
|
-
await foldEmailPreferences(
|
|
1792
|
+
await foldEmailPreferences(
|
|
1793
|
+
tx,
|
|
1794
|
+
loserKeysToRewrite,
|
|
1795
|
+
survivorKey,
|
|
1796
|
+
survivor.id,
|
|
1797
|
+
loser.id,
|
|
1798
|
+
);
|
|
1660
1799
|
|
|
1661
1800
|
// (vi-b) deals + crm_links re-point: these carry contact_id uuid FKs
|
|
1662
1801
|
// (not user keys), which the key rewrites above never touch. Without
|
|
1663
1802
|
// this, the loser's open deal is orphaned on a soft-deleted row — the
|
|
1664
1803
|
// survivor's next stage event/trigger would mint a SECOND deal (and a
|
|
1665
|
-
// duplicate deal.sold).
|
|
1666
|
-
// UPDATEs suffice.
|
|
1804
|
+
// duplicate deal.sold). Neither table has a unique index on contact_id,
|
|
1805
|
+
// so plain UPDATEs suffice.
|
|
1667
1806
|
await tx
|
|
1668
1807
|
.update(deals)
|
|
1669
1808
|
.set({ contactId: survivor.id })
|
|
@@ -1673,6 +1812,54 @@ async function mergeContacts(
|
|
|
1673
1812
|
.set({ contactId: survivor.id })
|
|
1674
1813
|
.where(eq(crmLinks.contactId, loser.id));
|
|
1675
1814
|
|
|
1815
|
+
// (vi-b-hist) the five HISTORY tables' `contact_id` re-point (PRD 04 T3).
|
|
1816
|
+
// Same shape as the deals/crm_links repoints above: a uuid column the
|
|
1817
|
+
// string-key rewrites never touch.
|
|
1818
|
+
//
|
|
1819
|
+
// PRD 05 T3: `contact_id` IS now a unique-index column on three of these
|
|
1820
|
+
// five (migration 0071), so "plain UPDATEs suffice" is no longer true on its
|
|
1821
|
+
// own — it holds only because the folds above ran FIRST and already know
|
|
1822
|
+
// about `contact_id`. {@link foldScopes} makes each fold read the survivor's
|
|
1823
|
+
// occupancy AND the loser's rows by `user_id` OR `contact_id`, so by the
|
|
1824
|
+
// time these statements run, every loser row that would have duplicated a
|
|
1825
|
+
// survivor row inside a contact-scoped index has been exited, left or
|
|
1826
|
+
// deleted. That is the invariant these three statements depend on; widen a
|
|
1827
|
+
// fold's scope, never this.
|
|
1828
|
+
// Deliberately lands AHEAD of the dual-write that populates the column
|
|
1829
|
+
// (PRD 04 D9/D10): while every row is still NULL this is a pure no-op, and
|
|
1830
|
+
// the moment the dual-write starts, merges are already correct. Shipped in
|
|
1831
|
+
// the other order, every merge in between would strand history rows
|
|
1832
|
+
// pointing at a soft-deleted loser contact.
|
|
1833
|
+
//
|
|
1834
|
+
// It is also the OVERWRITE half of the key rewrites above: those stamp only
|
|
1835
|
+
// rows nobody owned (the {@link adoptedContactId} NULL guard), so the
|
|
1836
|
+
// loser-OWNED rows arrive here still carrying `loser.id` and this is what
|
|
1837
|
+
// moves them. Between the two halves, every row the merge leaves keyed to
|
|
1838
|
+
// the survivor also carries the survivor's uuid. Runs after the rewrites
|
|
1839
|
+
// rather than before because it filters on `contact_id`, not `user_id` —
|
|
1840
|
+
// the order the key rewrites move is irrelevant to it — and re-running the
|
|
1841
|
+
// pair changes nothing (`contact_id = loser.id` matches zero rows twice).
|
|
1842
|
+
await tx
|
|
1843
|
+
.update(userEvents)
|
|
1844
|
+
.set({ contactId: survivor.id })
|
|
1845
|
+
.where(eq(userEvents.contactId, loser.id));
|
|
1846
|
+
await tx
|
|
1847
|
+
.update(journeyStates)
|
|
1848
|
+
.set({ contactId: survivor.id })
|
|
1849
|
+
.where(eq(journeyStates.contactId, loser.id));
|
|
1850
|
+
await tx
|
|
1851
|
+
.update(bucketMemberships)
|
|
1852
|
+
.set({ contactId: survivor.id })
|
|
1853
|
+
.where(eq(bucketMemberships.contactId, loser.id));
|
|
1854
|
+
await tx
|
|
1855
|
+
.update(emailSends)
|
|
1856
|
+
.set({ contactId: survivor.id })
|
|
1857
|
+
.where(eq(emailSends.contactId, loser.id));
|
|
1858
|
+
await tx
|
|
1859
|
+
.update(emailPreferences)
|
|
1860
|
+
.set({ contactId: survivor.id })
|
|
1861
|
+
.where(eq(emailPreferences.contactId, loser.id));
|
|
1862
|
+
|
|
1676
1863
|
// (vi-c) group_memberships FOLD: another contact_id uuid FK the key
|
|
1677
1864
|
// rewrites never touch. The loser is SOFT-deleted, so `onDelete: cascade`
|
|
1678
1865
|
// never fires — without this the loser's memberships are stranded on a dead
|
|
@@ -1836,11 +2023,10 @@ async function mergeContacts(
|
|
|
1836
2023
|
await ensureIdentityAliases(tx, postSurvivor);
|
|
1837
2024
|
|
|
1838
2025
|
// If the survivor's canonical key flipped (it had no external_id/anonymous_id
|
|
1839
|
-
// and the merge promoted one from the call/loser),
|
|
1840
|
-
//
|
|
1841
|
-
//
|
|
1842
|
-
//
|
|
1843
|
-
// an external_id mid-merge.
|
|
2026
|
+
// and the merge promoted one from the call/loser), stamp any orphans still
|
|
2027
|
+
// sitting under the OLD survivor key (risk 1). The loser folds above already
|
|
2028
|
+
// stamped everything they routed, so this reaches only rows that predate the
|
|
2029
|
+
// survivor's own contact_id era.
|
|
1844
2030
|
const newSurvivorKey =
|
|
1845
2031
|
(survivorSet.externalId as string | undefined) ??
|
|
1846
2032
|
survivor.externalId ??
|
|
@@ -1856,7 +2042,7 @@ async function mergeContacts(
|
|
|
1856
2042
|
(survivorSet.anonymousId as string | undefined) ?? survivor.anonymousId,
|
|
1857
2043
|
email: (survivorSet.email as string | undefined) ?? survivor.email,
|
|
1858
2044
|
};
|
|
1859
|
-
await
|
|
2045
|
+
await adoptOrphanHistory(tx, survivorKey, updatedSurvivor);
|
|
1860
2046
|
}
|
|
1861
2047
|
|
|
1862
2048
|
// `newSurvivorKey` IS the post-merge canonical key of the survivor — the same
|
|
@@ -1880,43 +2066,56 @@ async function mergeContacts(
|
|
|
1880
2066
|
* the survivor key still collides whenever the survivor already holds a live row
|
|
1881
2067
|
* for that journey.
|
|
1882
2068
|
*
|
|
1883
|
-
* Fix: build the survivor's
|
|
1884
|
-
* For
|
|
2069
|
+
* Fix: build the survivor's occupancy and route each loser row to exit / delete /
|
|
2070
|
+
* rewrite. For a LIVE collision, EXIT the loser's row first (preserve the
|
|
1885
2071
|
* survivor's live run) so the rewrite lands an 'exited' (out-of-predicate) row.
|
|
1886
|
-
* For
|
|
1887
|
-
*
|
|
1888
|
-
*
|
|
1889
|
-
*
|
|
1890
|
-
*
|
|
2072
|
+
* For a TERMINAL collision, DELETE the loser's duplicate — no longer REQUIRED by
|
|
2073
|
+
* the constraint (terminal rows are outside the predicate), but kept as hygiene
|
|
2074
|
+
* so the survivor doesn't carry two identical terminal rows (which would inflate
|
|
2075
|
+
* the count()-based ctx.history.journey.entryCount). Rewrite only the
|
|
2076
|
+
* non-colliding remainder onto the survivor key (+ survivor email). Re-check
|
|
1891
2077
|
* 'exited' occupancy after exiting so a just-exited loser row that would now
|
|
1892
2078
|
* duplicate a pre-existing survivor 'exited' row is dropped rather than rewritten.
|
|
2079
|
+
*
|
|
2080
|
+
* Occupancy is asked BOTH ways (see {@link foldScopes}), and LIVE occupancy is
|
|
2081
|
+
* keyed on the journey ALONE: both unique indexes say `status IN ('active',
|
|
2082
|
+
* 'waiting')`, i.e. the two live statuses share ONE slot, so a survivor 'active'
|
|
2083
|
+
* row and a loser 'waiting' row in the same journey collide even though their
|
|
2084
|
+
* (journey|status) pairs differ.
|
|
1893
2085
|
*/
|
|
1894
2086
|
async function foldJourneyStates(
|
|
1895
2087
|
tx: Tx,
|
|
1896
2088
|
survivorKey: string,
|
|
1897
2089
|
loserKeys: string[],
|
|
1898
2090
|
survivor: ContactRow,
|
|
2091
|
+
loserId?: string,
|
|
2092
|
+
opts?: FoldWriteOpts,
|
|
1899
2093
|
): Promise<void> {
|
|
1900
2094
|
const ACTIVE = new Set<string>(["active", "waiting"]);
|
|
2095
|
+
const { isLoser, isSurvivor } = foldScopes({
|
|
2096
|
+
userIdCol: journeyStates.userId,
|
|
2097
|
+
contactIdCol: journeyStates.contactId,
|
|
2098
|
+
survivorKey,
|
|
2099
|
+
survivorId: survivor.id,
|
|
2100
|
+
loserKeys,
|
|
2101
|
+
loserId,
|
|
2102
|
+
orphansOnly: opts?.stampOnly,
|
|
2103
|
+
});
|
|
1901
2104
|
|
|
1902
|
-
// Every (journey_id|status) pair the survivor already holds (ALL statuses).
|
|
1903
2105
|
const survivorRows = await tx
|
|
1904
2106
|
.select({
|
|
1905
2107
|
journeyId: journeyStates.journeyId,
|
|
1906
2108
|
status: journeyStates.status,
|
|
1907
2109
|
})
|
|
1908
2110
|
.from(journeyStates)
|
|
1909
|
-
.where(
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
)
|
|
1915
|
-
// Running occupied set — mutated as we exit/rewrite loser rows so two loser
|
|
1916
|
-
// rows in the same journey/status (3-way merge) can't collide with each other.
|
|
1917
|
-
const occupied = new Set(
|
|
1918
|
-
survivorRows.map((s) => `${s.journeyId}|${s.status}`),
|
|
2111
|
+
.where(and(isSurvivor, isNull(journeyStates.deletedAt)));
|
|
2112
|
+
|
|
2113
|
+
// Both sets are mutated as loser rows are routed, so two loser rows from
|
|
2114
|
+
// different keys (3-way merge) can't collide with each other either.
|
|
2115
|
+
const live = new Set(
|
|
2116
|
+
survivorRows.filter((s) => ACTIVE.has(s.status)).map((s) => s.journeyId),
|
|
1919
2117
|
);
|
|
2118
|
+
const slots = new Set(survivorRows.map((s) => `${s.journeyId}|${s.status}`));
|
|
1920
2119
|
|
|
1921
2120
|
const loserRows = await tx
|
|
1922
2121
|
.select({
|
|
@@ -1925,41 +2124,39 @@ async function foldJourneyStates(
|
|
|
1925
2124
|
status: journeyStates.status,
|
|
1926
2125
|
})
|
|
1927
2126
|
.from(journeyStates)
|
|
1928
|
-
.where(
|
|
1929
|
-
and(
|
|
1930
|
-
inArray(journeyStates.userId, loserKeys),
|
|
1931
|
-
isNull(journeyStates.deletedAt),
|
|
1932
|
-
),
|
|
1933
|
-
);
|
|
2127
|
+
.where(and(isLoser, isNull(journeyStates.deletedAt)));
|
|
1934
2128
|
|
|
1935
2129
|
const idsToExit: string[] = [];
|
|
1936
2130
|
const idsToDelete: string[] = [];
|
|
1937
2131
|
const idsToRewrite: string[] = [];
|
|
1938
2132
|
|
|
1939
2133
|
for (const l of loserRows) {
|
|
1940
|
-
const
|
|
1941
|
-
if (
|
|
1942
|
-
if (
|
|
1943
|
-
//
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
// Terminal collision (both completed/failed/exited the same journey) —
|
|
1955
|
-
// the survivor already records this state; drop the loser duplicate.
|
|
2134
|
+
const slot = `${l.journeyId}|${l.status}`;
|
|
2135
|
+
if (ACTIVE.has(l.status)) {
|
|
2136
|
+
if (!live.has(l.journeyId)) {
|
|
2137
|
+
// The one live slot for this journey is free — claim it.
|
|
2138
|
+
idsToRewrite.push(l.id);
|
|
2139
|
+
live.add(l.journeyId);
|
|
2140
|
+
slots.add(slot);
|
|
2141
|
+
continue;
|
|
2142
|
+
}
|
|
2143
|
+
// Survivor (or a prior loser) already holds the live slot — exit the
|
|
2144
|
+
// loser's row so the live run continues. Only do so if the resulting
|
|
2145
|
+
// 'exited' slot is itself free; otherwise drop it.
|
|
2146
|
+
const exitedSlot = `${l.journeyId}|exited`;
|
|
2147
|
+
if (slots.has(exitedSlot)) {
|
|
1956
2148
|
idsToDelete.push(l.id);
|
|
2149
|
+
} else {
|
|
2150
|
+
idsToExit.push(l.id);
|
|
2151
|
+
slots.add(exitedSlot);
|
|
1957
2152
|
}
|
|
2153
|
+
} else if (slots.has(slot)) {
|
|
2154
|
+
// Terminal collision (both completed/failed/exited the same journey) —
|
|
2155
|
+
// the survivor already records this state; drop the loser duplicate.
|
|
2156
|
+
idsToDelete.push(l.id);
|
|
1958
2157
|
} else {
|
|
1959
|
-
// Free slot — rewrite onto the survivor key. Claim it so a sibling loser
|
|
1960
|
-
// row in the same journey/status routes to exit/delete instead.
|
|
1961
2158
|
idsToRewrite.push(l.id);
|
|
1962
|
-
|
|
2159
|
+
slots.add(slot);
|
|
1963
2160
|
}
|
|
1964
2161
|
}
|
|
1965
2162
|
|
|
@@ -1976,17 +2173,25 @@ async function foldJourneyStates(
|
|
|
1976
2173
|
.where(inArray(journeyStates.id, idsToExit));
|
|
1977
2174
|
}
|
|
1978
2175
|
|
|
1979
|
-
//
|
|
1980
|
-
// the survivor
|
|
2176
|
+
// Route both the originally non-colliding rows AND the just-exited rows to
|
|
2177
|
+
// the survivor (the exited rows now sit in claimed-free 'exited' slots).
|
|
2178
|
+
// Stamp mode (adoption, PRD 05 T9): one column — every matched row is an
|
|
2179
|
+
// orphan (`orphansOnly` scope), so a plain stamp cannot re-parent anything.
|
|
2180
|
+
// The string key and denormalized email stay frozen at write-time values.
|
|
1981
2181
|
const rewriteIds = [...idsToRewrite, ...idsToExit];
|
|
1982
2182
|
if (rewriteIds.length > 0) {
|
|
1983
2183
|
await tx
|
|
1984
2184
|
.update(journeyStates)
|
|
1985
|
-
.set(
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
2185
|
+
.set(
|
|
2186
|
+
opts?.stampOnly
|
|
2187
|
+
? { contactId: survivor.id, updatedAt: new Date() }
|
|
2188
|
+
: {
|
|
2189
|
+
userId: survivorKey,
|
|
2190
|
+
contactId: adoptedContactId(journeyStates.contactId, survivor.id),
|
|
2191
|
+
...(survivor.email ? { userEmail: survivor.email } : {}),
|
|
2192
|
+
updatedAt: new Date(),
|
|
2193
|
+
},
|
|
2194
|
+
)
|
|
1990
2195
|
.where(inArray(journeyStates.id, rewriteIds));
|
|
1991
2196
|
}
|
|
1992
2197
|
}
|
|
@@ -1996,18 +2201,35 @@ async function foldJourneyStates(
|
|
|
1996
2201
|
* a bucket where a loser key also holds one, soft-LEAVE the loser's row first
|
|
1997
2202
|
* (uq_user_bucket_active forbids two active rows for the same (user, bucket);
|
|
1998
2203
|
* preserve the survivor's dwell clock), then rewrite the rest onto the survivor.
|
|
2204
|
+
*
|
|
2205
|
+
* `survivorId` is the absorbing contact's uuid — the soft-left rows are rewritten
|
|
2206
|
+
* by the same statement as the rest, so both halves of a folded pair leave here
|
|
2207
|
+
* carrying it (see {@link adoptedContactId} for the NULL guard).
|
|
1999
2208
|
*/
|
|
2000
2209
|
async function foldBucketMemberships(
|
|
2001
2210
|
tx: Tx,
|
|
2002
2211
|
survivorKey: string,
|
|
2003
2212
|
loserKeys: string[],
|
|
2213
|
+
survivorId: string,
|
|
2214
|
+
loserId?: string,
|
|
2215
|
+
opts?: FoldWriteOpts,
|
|
2004
2216
|
): Promise<void> {
|
|
2217
|
+
const { isLoser, isSurvivor } = foldScopes({
|
|
2218
|
+
userIdCol: bucketMemberships.userId,
|
|
2219
|
+
contactIdCol: bucketMemberships.contactId,
|
|
2220
|
+
survivorKey,
|
|
2221
|
+
survivorId,
|
|
2222
|
+
loserKeys,
|
|
2223
|
+
loserId,
|
|
2224
|
+
orphansOnly: opts?.stampOnly,
|
|
2225
|
+
});
|
|
2226
|
+
|
|
2005
2227
|
const survivorActive = await tx
|
|
2006
2228
|
.select({ bucketId: bucketMemberships.bucketId })
|
|
2007
2229
|
.from(bucketMemberships)
|
|
2008
2230
|
.where(
|
|
2009
2231
|
and(
|
|
2010
|
-
|
|
2232
|
+
isSurvivor,
|
|
2011
2233
|
eq(bucketMemberships.status, "active"),
|
|
2012
2234
|
isNull(bucketMemberships.deletedAt),
|
|
2013
2235
|
),
|
|
@@ -2022,15 +2244,19 @@ async function foldBucketMemberships(
|
|
|
2022
2244
|
.from(bucketMemberships)
|
|
2023
2245
|
.where(
|
|
2024
2246
|
and(
|
|
2025
|
-
|
|
2247
|
+
isLoser,
|
|
2026
2248
|
eq(bucketMemberships.status, "active"),
|
|
2027
2249
|
isNull(bucketMemberships.deletedAt),
|
|
2028
2250
|
),
|
|
2029
2251
|
);
|
|
2030
2252
|
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2253
|
+
// Claim as we go, so two loser rows active in the SAME bucket (3-way merge)
|
|
2254
|
+
// route the second to 'left' instead of both rewriting onto the survivor.
|
|
2255
|
+
const idsToLeave: string[] = [];
|
|
2256
|
+
for (const l of loserActive) {
|
|
2257
|
+
if (occupied.has(l.bucketId)) idsToLeave.push(l.id);
|
|
2258
|
+
else occupied.add(l.bucketId);
|
|
2259
|
+
}
|
|
2034
2260
|
|
|
2035
2261
|
if (idsToLeave.length > 0) {
|
|
2036
2262
|
await tx
|
|
@@ -2041,8 +2267,19 @@ async function foldBucketMemberships(
|
|
|
2041
2267
|
|
|
2042
2268
|
await tx
|
|
2043
2269
|
.update(bucketMemberships)
|
|
2044
|
-
.set(
|
|
2045
|
-
|
|
2270
|
+
.set(
|
|
2271
|
+
opts?.stampOnly
|
|
2272
|
+
? { contactId: survivorId, updatedAt: new Date() }
|
|
2273
|
+
: {
|
|
2274
|
+
userId: survivorKey,
|
|
2275
|
+
contactId: adoptedContactId(
|
|
2276
|
+
bucketMemberships.contactId,
|
|
2277
|
+
survivorId,
|
|
2278
|
+
),
|
|
2279
|
+
updatedAt: new Date(),
|
|
2280
|
+
},
|
|
2281
|
+
)
|
|
2282
|
+
.where(isLoser);
|
|
2046
2283
|
}
|
|
2047
2284
|
|
|
2048
2285
|
/**
|
|
@@ -2096,18 +2333,35 @@ async function foldGroupMemberships(
|
|
|
2096
2333
|
* 3-way merge where two losers each carry a pref for the SAME email folds
|
|
2097
2334
|
* loser2 into loser1's already-folded result instead of colliding on
|
|
2098
2335
|
* `uq(user_id, email)` (risk 3). The loser row is deleted after folding.
|
|
2336
|
+
*
|
|
2337
|
+
* This is the one fold whose SURVIVING row is not the row that moved — a
|
|
2338
|
+
* collision keeps the TARGET and deletes the loser — so `survivorId` is stamped
|
|
2339
|
+
* on both branches: onto the re-pointed row when the slot is free, and onto the
|
|
2340
|
+
* target as part of the fold write when it is not. Same NULL guard either way
|
|
2341
|
+
* (see {@link adoptedContactId}); the fold branch expresses it in TS because the
|
|
2342
|
+
* target row is already in hand.
|
|
2099
2343
|
*/
|
|
2100
2344
|
async function foldEmailPreferences(
|
|
2101
2345
|
tx: Tx,
|
|
2102
2346
|
loserKeys: string[],
|
|
2103
2347
|
survivorKey: string,
|
|
2348
|
+
survivorId: string,
|
|
2349
|
+
loserId?: string,
|
|
2350
|
+
opts?: FoldWriteOpts,
|
|
2104
2351
|
): Promise<void> {
|
|
2105
2352
|
if (loserKeys.length === 0) return;
|
|
2106
2353
|
|
|
2107
|
-
const
|
|
2108
|
-
.
|
|
2109
|
-
.
|
|
2110
|
-
|
|
2354
|
+
const { isLoser, isSurvivor } = foldScopes({
|
|
2355
|
+
userIdCol: emailPreferences.userId,
|
|
2356
|
+
contactIdCol: emailPreferences.contactId,
|
|
2357
|
+
survivorKey,
|
|
2358
|
+
survivorId,
|
|
2359
|
+
loserKeys,
|
|
2360
|
+
loserId,
|
|
2361
|
+
orphansOnly: opts?.stampOnly,
|
|
2362
|
+
});
|
|
2363
|
+
|
|
2364
|
+
const loserPrefs = await tx.select().from(emailPreferences).where(isLoser);
|
|
2111
2365
|
|
|
2112
2366
|
if (loserPrefs.length === 0) return;
|
|
2113
2367
|
|
|
@@ -2118,25 +2372,37 @@ async function foldEmailPreferences(
|
|
|
2118
2372
|
};
|
|
2119
2373
|
|
|
2120
2374
|
for (const lp of loserPrefs) {
|
|
2121
|
-
// Re-read the CURRENT target row for
|
|
2122
|
-
//
|
|
2375
|
+
// Re-read the CURRENT target row for this address — it may be the original
|
|
2376
|
+
// survivor pref, a survivor row adoption stamped under a STALE key, a prior
|
|
2377
|
+
// loser's just-folded pref, or absent. A stamped row is preferred when both
|
|
2378
|
+
// exist: it is the row the contact-scoped read resolves, and the row whose
|
|
2379
|
+
// uniqueness a re-point would violate.
|
|
2123
2380
|
const targetRows = await tx
|
|
2124
2381
|
.select()
|
|
2125
2382
|
.from(emailPreferences)
|
|
2126
|
-
.where(
|
|
2127
|
-
|
|
2128
|
-
eq(emailPreferences.userId, survivorKey),
|
|
2129
|
-
eq(emailPreferences.email, lp.email),
|
|
2130
|
-
),
|
|
2131
|
-
)
|
|
2383
|
+
.where(and(isSurvivor, eq(emailPreferences.email, lp.email)))
|
|
2384
|
+
.orderBy(sql`${emailPreferences.contactId} IS NULL`)
|
|
2132
2385
|
.limit(1);
|
|
2133
2386
|
const target = targetRows[0];
|
|
2134
2387
|
|
|
2135
2388
|
if (!target) {
|
|
2136
|
-
// The
|
|
2389
|
+
// The slot is free on BOTH keys. Merge mode re-points the loser row;
|
|
2390
|
+
// stamp mode (adoption) writes ownership only — the row is an orphan by
|
|
2391
|
+
// scope, so a plain stamp cannot re-parent anything.
|
|
2137
2392
|
await tx
|
|
2138
2393
|
.update(emailPreferences)
|
|
2139
|
-
.set(
|
|
2394
|
+
.set(
|
|
2395
|
+
opts?.stampOnly
|
|
2396
|
+
? { contactId: survivorId, updatedAt: new Date() }
|
|
2397
|
+
: {
|
|
2398
|
+
userId: survivorKey,
|
|
2399
|
+
contactId: adoptedContactId(
|
|
2400
|
+
emailPreferences.contactId,
|
|
2401
|
+
survivorId,
|
|
2402
|
+
),
|
|
2403
|
+
updatedAt: new Date(),
|
|
2404
|
+
},
|
|
2405
|
+
)
|
|
2140
2406
|
.where(eq(emailPreferences.id, lp.id));
|
|
2141
2407
|
continue;
|
|
2142
2408
|
}
|
|
@@ -2162,6 +2428,7 @@ async function foldEmailPreferences(
|
|
|
2162
2428
|
categories: foldedCategories,
|
|
2163
2429
|
suppressedAt: earliest(target.suppressedAt, lp.suppressedAt),
|
|
2164
2430
|
lastBounceAt: earliest(target.lastBounceAt, lp.lastBounceAt),
|
|
2431
|
+
contactId: target.contactId ?? survivorId,
|
|
2165
2432
|
updatedAt: new Date(),
|
|
2166
2433
|
})
|
|
2167
2434
|
.where(eq(emailPreferences.id, target.id));
|
|
@@ -2173,48 +2440,68 @@ async function foldEmailPreferences(
|
|
|
2173
2440
|
}
|
|
2174
2441
|
|
|
2175
2442
|
/**
|
|
2176
|
-
*
|
|
2177
|
-
*
|
|
2178
|
-
*
|
|
2179
|
-
*
|
|
2180
|
-
*
|
|
2181
|
-
*
|
|
2182
|
-
* silently orphaned. Rewrite them from `oldKey` to `newKey`, applying the SAME
|
|
2183
|
-
* active/terminal dedupe as the merge fold so the rewrite can't violate
|
|
2184
|
-
* uq_user_journey_active / uq_user_bucket_active / uq(user_id,email).
|
|
2443
|
+
* Adopt ORPHAN history sitting under `fromKey` onto its owning contact — the
|
|
2444
|
+
* D4 statement, made permanent by PRD 05 T9: `UPDATE <t> SET contact_id = :id
|
|
2445
|
+
* WHERE user_id = :fromKey AND contact_id IS NULL`, on five tables. One
|
|
2446
|
+
* column. The string key is never rewritten again; reads follow `contact_id`
|
|
2447
|
+
* (T4-T8), and D9 keeps writing `user_id` on every insert, so history stays a
|
|
2448
|
+
* frozen record of the key it happened under.
|
|
2185
2449
|
*
|
|
2186
|
-
*
|
|
2450
|
+
* This replaced two functions:
|
|
2451
|
+
* - `repointOwnHistory`, which rewrote `user_id` from the old canonical key
|
|
2452
|
+
* to the new one on a key flip (with three dedupe folds against the
|
|
2453
|
+
* string-scoped unique indexes), and
|
|
2454
|
+
* - `adoptOwnKeyHistory`, which stamped history already sitting under a
|
|
2455
|
+
* brand-new contact's OWN key.
|
|
2456
|
+
* The "did the canonical key flip" test that distinguished them is meaningless
|
|
2457
|
+
* when nothing is keyed on the canonical key, so both call shapes are one
|
|
2458
|
+
* function now: name the key, stamp its orphans.
|
|
2459
|
+
*
|
|
2460
|
+
* The `contact_id IS NULL` predicate is the anti-theft guard (D6): a row
|
|
2461
|
+
* another contact owns already carries that owner's id and can never match.
|
|
2462
|
+
* `keysAnotherContact` no longer gates adoption — it survives only to gate
|
|
2463
|
+
* the `mergedKeys` REPORT (the analytics stitch must never alias a foreign
|
|
2464
|
+
* key) and the attach path inside `claimIdentityKey`.
|
|
2465
|
+
*
|
|
2466
|
+
* The folds still run, re-expressed on the contact scope (D4/D5): the
|
|
2467
|
+
* adopting contact may ALREADY own a live row for the same journey/bucket (or
|
|
2468
|
+
* a pref row for the same address), and a bare stamp would mint a second row
|
|
2469
|
+
* satisfying `uq_contact_journey_active` / `uq_contact_bucket_active` /
|
|
2470
|
+
* `email_preferences_contact_email_idx` — a 23505 inside the resolve
|
|
2471
|
+
* transaction. Same routing as the merge fold (exit / soft-leave / dedupe /
|
|
2472
|
+
* pref-fold), stamp-only writes.
|
|
2473
|
+
*
|
|
2474
|
+
* Idempotent: a second run finds `contact_id` already set and matches nothing.
|
|
2187
2475
|
*/
|
|
2188
|
-
async function
|
|
2476
|
+
async function adoptOrphanHistory(
|
|
2189
2477
|
tx: Tx,
|
|
2190
|
-
|
|
2191
|
-
newKey: string,
|
|
2478
|
+
fromKey: string,
|
|
2192
2479
|
row: ContactRow,
|
|
2193
2480
|
): Promise<void> {
|
|
2194
|
-
|
|
2481
|
+
const ownKey = contactKey(row);
|
|
2195
2482
|
|
|
2196
|
-
// user_events: no unique constraint
|
|
2483
|
+
// user_events + email_sends: no unique constraint involved — plain stamps.
|
|
2197
2484
|
await tx
|
|
2198
2485
|
.update(userEvents)
|
|
2199
|
-
.set({
|
|
2200
|
-
.where(eq(userEvents.userId,
|
|
2201
|
-
|
|
2202
|
-
// journey_states + bucket_memberships: dedupe against the survivor/new key's
|
|
2203
|
-
// existing rows (the folds already handle the collision logic).
|
|
2204
|
-
await foldJourneyStates(tx, newKey, [oldKey], row);
|
|
2205
|
-
await foldBucketMemberships(tx, newKey, [oldKey]);
|
|
2206
|
-
|
|
2207
|
-
// email_sends: no unique constraint on user_id — blind rewrite.
|
|
2486
|
+
.set({ contactId: row.id })
|
|
2487
|
+
.where(and(eq(userEvents.userId, fromKey), isNull(userEvents.contactId)));
|
|
2208
2488
|
await tx
|
|
2209
2489
|
.update(emailSends)
|
|
2210
|
-
.set({
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2490
|
+
.set({ contactId: row.id })
|
|
2491
|
+
.where(and(eq(emailSends.userId, fromKey), isNull(emailSends.contactId)));
|
|
2492
|
+
|
|
2493
|
+
// journey_states / bucket_memberships / email_preferences: stamp through
|
|
2494
|
+
// the folds so a collision with a row the contact already owns is routed
|
|
2495
|
+
// (exited / left / folded) instead of violating the contact-scoped index.
|
|
2496
|
+
await foldJourneyStates(tx, ownKey, [fromKey], row, undefined, {
|
|
2497
|
+
stampOnly: true,
|
|
2498
|
+
});
|
|
2499
|
+
await foldBucketMemberships(tx, ownKey, [fromKey], row.id, undefined, {
|
|
2500
|
+
stampOnly: true,
|
|
2501
|
+
});
|
|
2502
|
+
await foldEmailPreferences(tx, [fromKey], ownKey, row.id, undefined, {
|
|
2503
|
+
stampOnly: true,
|
|
2504
|
+
});
|
|
2218
2505
|
}
|
|
2219
2506
|
|
|
2220
2507
|
/** RECORD a contact_aliases row per loser key → survivor (reason 'merge'). */
|
|
@@ -2624,6 +2911,89 @@ export async function resolveRecipient(opts: {
|
|
|
2624
2911
|
};
|
|
2625
2912
|
}
|
|
2626
2913
|
|
|
2914
|
+
/**
|
|
2915
|
+
* Resolve the live contact that OWNS a canonical history key, for the PRD 04
|
|
2916
|
+
* `contact_id` DUAL-WRITE and nothing else.
|
|
2917
|
+
*
|
|
2918
|
+
* WHAT IT IS FOR. Every history row (`user_events`, `journey_states`,
|
|
2919
|
+
* `bucket_memberships`, `email_sends`, `email_preferences`) is stored under a
|
|
2920
|
+
* canonical STRING key (`external_id ?? anonymous_id ?? id`). The dual-write
|
|
2921
|
+
* stamps the owning `contacts.id` alongside it at insert time, so this must
|
|
2922
|
+
* resolve EXACTLY the key the row is stored under — no widening, no minting.
|
|
2923
|
+
* A miss is a legal, expected `null`: the column is nullable bookkeeping and a
|
|
2924
|
+
* later backfill fills what this could not see. Callers wrap it per D6 (a
|
|
2925
|
+
* bookkeeping resolve may never fail the operation it rides on).
|
|
2926
|
+
*
|
|
2927
|
+
* WHY ALIAS-AWARE. Since PRD 03 a second device's anonymous id can live ONLY in
|
|
2928
|
+
* `contact_aliases` (the contact row already carries a different
|
|
2929
|
+
* `anonymous_id`), so a column-only probe would stamp NULL for that visitor
|
|
2930
|
+
* FOREVER — the backfill reads the same columns and would miss it too. The
|
|
2931
|
+
* alias probe is restricted to kinds `external`/`anonymous` because those are
|
|
2932
|
+
* the only kinds a CANONICAL key can be: folding in an `email`/`discord` alias
|
|
2933
|
+
* would resolve history that today resolves to nothing, which is a read-shape
|
|
2934
|
+
* change smuggled in through a write. When both kinds carry the value,
|
|
2935
|
+
* `external` wins — mirroring the canonical-key precedence `external_id ??
|
|
2936
|
+
* anonymous_id` so the answer is deterministic rather than index-order luck.
|
|
2937
|
+
*
|
|
2938
|
+
* WHY NOT {@link findByKey}. That one takes a `(kind, value)` pair, probes
|
|
2939
|
+
* aliases FIRST across every kind, and carries a uuid-as-external fallback with
|
|
2940
|
+
* merge/mint semantics attached — resolution for a WRITE that may change the
|
|
2941
|
+
* identity graph. This is a read-only ownership question about one already-fixed
|
|
2942
|
+
* string, so it stays a separate, narrower probe.
|
|
2943
|
+
*/
|
|
2944
|
+
export async function lookupContactIdByKey(
|
|
2945
|
+
db: Database,
|
|
2946
|
+
key: string,
|
|
2947
|
+
): Promise<string | null> {
|
|
2948
|
+
const value = key?.trim();
|
|
2949
|
+
if (!value) return null;
|
|
2950
|
+
|
|
2951
|
+
// (1) COLUMN probe — one statement over the three indexed identity columns a
|
|
2952
|
+
// canonical key can occupy (Postgres serves the OR as a BitmapOr). The uuid
|
|
2953
|
+
// leg is REGEX-GUARDED rather than cast (`id::text = $1`): a bare cast is
|
|
2954
|
+
// unindexed AND an unguarded `id = $1` throws 22P02 on a non-uuid key.
|
|
2955
|
+
const direct = await db
|
|
2956
|
+
.select({ id: contacts.id })
|
|
2957
|
+
.from(contacts)
|
|
2958
|
+
.where(
|
|
2959
|
+
and(
|
|
2960
|
+
isNull(contacts.deletedAt),
|
|
2961
|
+
or(
|
|
2962
|
+
eq(contacts.externalId, value),
|
|
2963
|
+
eq(contacts.anonymousId, value),
|
|
2964
|
+
UUID_REGEX.test(value) ? eq(contacts.id, value) : undefined,
|
|
2965
|
+
),
|
|
2966
|
+
),
|
|
2967
|
+
)
|
|
2968
|
+
.limit(1);
|
|
2969
|
+
if (direct[0]) return direct[0].id;
|
|
2970
|
+
|
|
2971
|
+
// (2) ALIAS probe — only reached on a column miss. `deleted_at IS NULL` lives
|
|
2972
|
+
// INSIDE the join (the live-target rule, as in `findByKey`): an alias whose
|
|
2973
|
+
// target is soft-deleted must produce NO row rather than stamp a tombstone.
|
|
2974
|
+
const viaAlias = await db
|
|
2975
|
+
.select({ id: contacts.id })
|
|
2976
|
+
.from(contactAliases)
|
|
2977
|
+
.innerJoin(
|
|
2978
|
+
contacts,
|
|
2979
|
+
and(
|
|
2980
|
+
eq(contacts.id, contactAliases.contactId),
|
|
2981
|
+
isNull(contacts.deletedAt),
|
|
2982
|
+
),
|
|
2983
|
+
)
|
|
2984
|
+
.where(
|
|
2985
|
+
and(
|
|
2986
|
+
eq(contactAliases.aliasValue, value),
|
|
2987
|
+
inArray(contactAliases.aliasKind, ["external", "anonymous"]),
|
|
2988
|
+
),
|
|
2989
|
+
)
|
|
2990
|
+
.orderBy(
|
|
2991
|
+
sql`case when ${contactAliases.aliasKind} = 'external' then 0 else 1 end`,
|
|
2992
|
+
)
|
|
2993
|
+
.limit(1);
|
|
2994
|
+
return viaAlias[0]?.id ?? null;
|
|
2995
|
+
}
|
|
2996
|
+
|
|
2627
2997
|
/** Alias-aware lookup helper (mirrors findByKey's alias probe but on the
|
|
2628
2998
|
* top-level db handle, no tx). Engine-internal export: `resolveRecipient`
|
|
2629
2999
|
* below and the feed's anonymous-recipient resolver (PRD 03 T5 — a second
|