Skip to content

Move user flags to table#3074

Open
brage-andreas wants to merge 1 commit into
mainfrom
move-user-flags-to-table
Open

Move user flags to table#3074
brage-andreas wants to merge 1 commit into
mainfrom
move-user-flags-to-table

Conversation

@brage-andreas

Copy link
Copy Markdown
Member

No description provided.

brage-andreas commented Apr 1, 2026

Copy link
Copy Markdown
Member Author

@brage-andreas brage-andreas changed the title feat: move user flags to table Move user flags to table Apr 1, 2026
@brage-andreas
brage-andreas marked this pull request as ready for review April 1, 2026 14:45
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 3 times, most recently from 163553e to e12cbed Compare April 8, 2026 17:14
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 3 times, most recently from 0012c7a to 38ceefb Compare April 20, 2026 08:54
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 3 times, most recently from 9d8dc3d to dd6a738 Compare April 25, 2026 14:01
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 7 times, most recently from fcdcb07 to 8b99d17 Compare May 1, 2026 23:01
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch from 8b99d17 to 3cfbcf7 Compare May 2, 2026 16:58
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 2 times, most recently from 4ca7774 to 1d5e85b Compare May 3, 2026 17:14
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 4 times, most recently from 80d6a1d to 31c06b8 Compare May 10, 2026 11:49
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 8 times, most recently from 1676261 to 4910b0a Compare May 14, 2026 17:24
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 8 times, most recently from f851e77 to 2f02a97 Compare May 25, 2026 22:03
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 3 times, most recently from 4ec7e5b to 6eb1a5a Compare June 17, 2026 15:48
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 2 times, most recently from 00fd8d9 to 33af8fa Compare June 26, 2026 15:52
@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch 2 times, most recently from 3e4873e to f466c43 Compare July 7, 2026 09:17

*/
-- AlterTable
ALTER TABLE "ow_user" DROP COLUMN "flags";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Create the new tables first
  2. Migrate existing flag data to user_flag_link records
  3. 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;
Suggested change
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

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Comment on lines +306 to +314
async assignFlagToUser(handle, userId, flagName) {
await handle.userFlagLink.create({
data: {
user: { connect: { id: userId } },
userFlag: { connect: { name: flagName } },
awardedAt: new Date(),
},
})
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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])

Suggested change
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

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

@brage-andreas
brage-andreas force-pushed the move-user-flags-to-table branch from f466c43 to 5a91de4 Compare July 11, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants