Move user flags to table#3074
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
163553e to
e12cbed
Compare
0012c7a to
38ceefb
Compare
9d8dc3d to
dd6a738
Compare
fcdcb07 to
8b99d17
Compare
8b99d17 to
3cfbcf7
Compare
4ca7774 to
1d5e85b
Compare
80d6a1d to
31c06b8
Compare
1676261 to
4910b0a
Compare
f851e77 to
2f02a97
Compare
4ec7e5b to
6eb1a5a
Compare
00fd8d9 to
33af8fa
Compare
3e4873e to
f466c43
Compare
|
|
||
| */ | ||
| -- AlterTable | ||
| ALTER TABLE "ow_user" DROP COLUMN "flags"; |
There was a problem hiding this comment.
Critical: Data Loss - The migration drops the flags column without migrating existing flag data. Any users who currently have flags assigned will lose them.
The migration should:
- Create the new tables first
- Migrate existing flag data to
user_flag_linkrecords - Only then drop the old column
Example migration logic needed:
-- Insert user_flag_link records for existing flags
INSERT INTO "user_flag_link" ("id", "user_id", "user_flag_id", "awarded_at")
SELECT
gen_random_uuid(),
u.id,
uf.id,
u.updated_at
FROM "ow_user" u
CROSS JOIN LATERAL unnest(u.flags) flag_name
JOIN "user_flag" uf ON uf.name = flag_name;| ALTER TABLE "ow_user" DROP COLUMN "flags"; | |
| -- Migrate existing flag data to user_flag_link records | |
| INSERT INTO "user_flag_link" ("id", "user_id", "user_flag_id", "awarded_at") | |
| SELECT | |
| gen_random_uuid(), | |
| u.id, | |
| uf.id, | |
| u.updated_at | |
| FROM "ow_user" u | |
| CROSS JOIN LATERAL unnest(u.flags) flag_name | |
| JOIN "user_flag" uf ON uf.name = flag_name; | |
| ALTER TABLE "ow_user" DROP COLUMN "flags"; | |
Spotted by Graphite
Is this helpful? React 👍 or 👎 to let us know.
| async assignFlagToUser(handle, userId, flagName) { | ||
| await handle.userFlagLink.create({ | ||
| data: { | ||
| user: { connect: { id: userId } }, | ||
| userFlag: { connect: { name: flagName } }, | ||
| awardedAt: new Date(), | ||
| }, | ||
| }) | ||
| }, |
There was a problem hiding this comment.
Critical: Duplicate Flag Assignments - This method doesn't check if a flag is already assigned to the user before creating a new link. Since the database schema has no unique constraint on (userId, userFlagId), this will create duplicate userFlagLink records if called multiple times.
Fix by using upsert or checking for existence first:
async assignFlagToUser(handle, userId, flagName) {
const flag = await handle.userFlag.findUnique({ where: { name: flagName } })
if (!flag) throw new Error(`Flag ${flagName} not found`)
const existing = await handle.userFlagLink.findFirst({
where: { userId, userFlagId: flag.id }
})
if (!existing) {
await handle.userFlagLink.create({
data: {
userId,
userFlagId: flag.id,
awardedAt: new Date(),
},
})
}
}Alternatively, add a unique constraint to the schema: @@unique([userId, userFlagId])
| async assignFlagToUser(handle, userId, flagName) { | |
| await handle.userFlagLink.create({ | |
| data: { | |
| user: { connect: { id: userId } }, | |
| userFlag: { connect: { name: flagName } }, | |
| awardedAt: new Date(), | |
| }, | |
| }) | |
| }, | |
| async assignFlagToUser(handle, userId, flagName) { | |
| const flag = await handle.userFlag.findUnique({ where: { name: flagName } }) | |
| if (!flag) throw new Error(`Flag ${flagName} not found`) | |
| const existing = await handle.userFlagLink.findFirst({ | |
| where: { userId, userFlagId: flag.id }, | |
| }) | |
| if (!existing) { | |
| await handle.userFlagLink.create({ | |
| data: { | |
| userId, | |
| userFlagId: flag.id, | |
| awardedAt: new Date(), | |
| }, | |
| }) | |
| } | |
| }, | |
Spotted by Graphite
Is this helpful? React 👍 or 👎 to let us know.
f466c43 to
5a91de4
Compare

No description provided.