Skip to content

Commit 64aa220

Browse files
committed
Don't block deleting non-FxA subscribers
Legacy feature flags that were assigned a fake feature flag user in the feature_flag_events table, were associated with a subscriber without an FxA UID. Now that we want to delete those subcribers, the foreign key on this table is blocking it. Thus, this changes the assciated fake feature flag user.
1 parent ade8661 commit 64aa220

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
/**
6+
* @param { import("knex").Knex } knex
7+
* @returns { Promise<void> }
8+
*/
9+
export async function up(knex) {
10+
// Legacy feature flags that were assigned a fake feature flag user
11+
// in the feature_flag_events table, were associated with a subscriber
12+
// without an FxA UID. Now that we want to delete those subcribers,
13+
// the foreign key on this table is blocking it.
14+
// Thus, this changes the assciated fake feature flag user to one with
15+
// an FxA UID.
16+
17+
const previousFakeFirstuser = await knex("feature_flag_events")
18+
.join(
19+
"subscribers",
20+
"feature_flag_events.created_by_subscriber_id",
21+
"subscribers.id",
22+
)
23+
.first("created_by_subscriber_id")
24+
.whereLike("primary_email", "%@mozilla.com")
25+
.andWhere("feature_flag_events.created_at", "<", "2025-03-06");
26+
if (!previousFakeFirstuser) {
27+
console.log(
28+
"No existing feature flag events with fake authors, so nothing to migrate.",
29+
);
30+
return;
31+
}
32+
33+
const newFakeFirstuser = await knex("subscribers")
34+
.select("id")
35+
.whereLike("primary_email", "%@mozilla.com")
36+
.whereNotNull("fxa_uid")
37+
.first();
38+
if (!newFakeFirstuser) {
39+
throw new Error(
40+
"Couldn't find a subscriber with an @mozilla.com email address and an FxA UID to attribute pre-existing feature flags to.",
41+
);
42+
}
43+
44+
console.log({ previousFakeFirstuser, newFakeFirstuser });
45+
await knex("feature_flag_events")
46+
.update("created_by_subscriber_id", newFakeFirstuser.id)
47+
.where(
48+
"created_by_subscriber_id",
49+
previousFakeFirstuser.created_by_subscriber_id,
50+
)
51+
// We ran the migration `20250225111207_add_feature_flag_events` on March 5th,
52+
// so no feature flags should have been assigned to `previousFakeFirstuser` after that date:
53+
.andWhere("created_at", "<", "2025-03-06");
54+
}
55+
/**
56+
* @param { import("knex").Knex } knex
57+
* @returns { Promise<void> }
58+
*/
59+
export async function down(knex) {
60+
const existingFeatureFlagEvents = await knex("feature_flag_events").select(
61+
"*",
62+
);
63+
64+
if (existingFeatureFlagEvents.length === 0) {
65+
console.log(
66+
"No existing feature flag events with fake authors, so nothing to migrate.",
67+
);
68+
return;
69+
}
70+
71+
const previousFakeFirstuser = await knex("subscribers")
72+
.select("id")
73+
.whereLike("primary_email", "%@mozilla.com")
74+
.first();
75+
if (!previousFakeFirstuser) {
76+
throw new Error(
77+
"Couldn't find a subscriber with an @mozilla.com email address that pre-existing feature flags were attributed to.",
78+
);
79+
}
80+
81+
const newFakeFirstuser = await knex("subscribers")
82+
.select("id")
83+
.whereLike("primary_email", "%@mozilla.com")
84+
.whereNotNull("fxa_uid")
85+
.first();
86+
if (!newFakeFirstuser) {
87+
throw new Error(
88+
"Couldn't find a subscriber with an @mozilla.com email address and an FxA UID that pre-existing feature flags were attributed to.",
89+
);
90+
}
91+
92+
await knex("feature_flag_events")
93+
.update("created_by_subscriber_id", previousFakeFirstuser.id)
94+
.where("created_by_subscriber_id", newFakeFirstuser.id)
95+
.andWhere("created_at", "<", "2025-03-06");
96+
}

0 commit comments

Comments
 (0)