From d342f1ad25e9f63874369c4257b7a1bb78cd3cb5 Mon Sep 17 00:00:00 2001 From: Chris Parsons <438676+chrisparsons83@users.noreply.github.com> Date: Wed, 6 May 2026 20:35:02 -0700 Subject: [PATCH] Remove backfill-flag-configs script (#389) * Remove backfill-flag-configs script One-time migration has been run; script is no longer needed. https://claude.ai/code/session_014RUwPfm1qc539xLxW4m9Uy * Clean up flag config code - FlagSvg: remove redundant getDisplayFlagConfig call; config is already a valid FlagConfig by type, so the validation/fallback was dead code. Also replace the unreachable `?? "#adf661"` fallback with a non-null assertion. - team/user models: pre-generate ID before insert so flagConfig is persisted immediately on creation rather than relying on display-time generation. - auth.server.ts: add create.after hook so BetterAuth-created users also get a flagConfig written to the DB on signup. https://claude.ai/code/session_014RUwPfm1qc539xLxW4m9Uy * Fix lint: replace non-null assertion in FlagSvg with destructure defaults oxlint forbids the ! operator; destructuring with empty-string defaults is equivalent since FlagConfig always has 3 colors. https://claude.ai/code/session_014RUwPfm1qc539xLxW4m9Uy --------- Co-authored-by: Claude --- app/components/ui/FlagSvg.tsx | 35 +++++++++++------------------ app/lib/auth.server.ts | 9 ++++++++ app/models/team.ts | 13 +++++++++-- app/models/user.ts | 7 +++++- scripts/backfill-flag-configs.ts | 38 -------------------------------- 5 files changed, 39 insertions(+), 63 deletions(-) delete mode 100644 scripts/backfill-flag-configs.ts diff --git a/app/components/ui/FlagSvg.tsx b/app/components/ui/FlagSvg.tsx index d47b92b..eafa649 100644 --- a/app/components/ui/FlagSvg.tsx +++ b/app/components/ui/FlagSvg.tsx @@ -1,5 +1,4 @@ import { cn } from "~/lib/utils"; -import { getDisplayFlagConfig } from "~/lib/avatar-data"; import type { FlagConfig } from "~/lib/flag-types"; interface FlagSvgProps { @@ -9,16 +8,8 @@ interface FlagSvgProps { title?: string; } -function colorAt(colors: string[], index: number): string { - return colors[index % colors.length] ?? "#adf661"; -} - export function FlagSvg({ config, size = 48, className, title }: FlagSvgProps) { - const safeConfig = getDisplayFlagConfig(`${config.pattern}:${config.colors.join("-")}`, config); - const colors = safeConfig.colors; - const c0 = colorAt(colors, 0); - const c1 = colorAt(colors, 1); - const c2 = colorAt(colors, 2); + const [c0 = "", c1 = "", c2 = ""] = config.colors; return ( {title ? {title} : null} - {safeConfig.pattern === "triband-h" && ( + {config.pattern === "triband-h" && ( <> )} - {safeConfig.pattern === "triband-v" && ( + {config.pattern === "triband-v" && ( <> )} - {safeConfig.pattern === "diagonal" && ( + {config.pattern === "diagonal" && ( <> )} - {safeConfig.pattern === "nordic-cross" && ( + {config.pattern === "nordic-cross" && ( <> @@ -60,7 +51,7 @@ export function FlagSvg({ config, size = 48, className, title }: FlagSvgProps) { )} - {safeConfig.pattern === "cross" && ( + {config.pattern === "cross" && ( <> @@ -69,28 +60,28 @@ export function FlagSvg({ config, size = 48, className, title }: FlagSvgProps) { )} - {safeConfig.pattern === "canton" && ( + {config.pattern === "canton" && ( <> )} - {safeConfig.pattern === "triangle" && ( + {config.pattern === "triangle" && ( <> )} - {safeConfig.pattern === "chevron" && ( + {config.pattern === "chevron" && ( <> )} - {safeConfig.pattern === "quartered" && ( + {config.pattern === "quartered" && ( <> @@ -98,21 +89,21 @@ export function FlagSvg({ config, size = 48, className, title }: FlagSvgProps) { )} - {safeConfig.pattern === "bordered" && ( + {config.pattern === "bordered" && ( <> )} - {safeConfig.pattern === "circle" && ( + {config.pattern === "circle" && ( <> )} - {safeConfig.pattern === "star" && ( + {config.pattern === "star" && ( <> { + await db + .update(schema.users) + .set({ flagConfig: generateFlagConfig(user.id) }) + .where(eq(schema.users.id, user.id)); + }, + }, update: { before: async (data) => { const incomingImage = diff --git a/app/models/team.ts b/app/models/team.ts index 3e5cf0e..122f6e2 100644 --- a/app/models/team.ts +++ b/app/models/team.ts @@ -1,19 +1,28 @@ import { eq, and, isNull } from "drizzle-orm"; import { database } from "~/database/context"; import * as schema from "~/database/schema"; +import { generateFlagConfig } from "~/lib/flag-generator"; export type Team = typeof schema.teams.$inferSelect; export type NewTeam = typeof schema.teams.$inferInsert; export async function createTeam(data: NewTeam): Promise { const db = database(); - const [team] = await db.insert(schema.teams).values(data).returning(); + const id = data.id ?? crypto.randomUUID(); + const [team] = await db + .insert(schema.teams) + .values({ ...data, id, flagConfig: data.flagConfig ?? generateFlagConfig(id) }) + .returning(); return team; } export async function createManyTeams(teams: NewTeam[]): Promise { const db = database(); - return await db.insert(schema.teams).values(teams).returning(); + const withFlags = teams.map((team) => { + const id = team.id ?? crypto.randomUUID(); + return { ...team, id, flagConfig: team.flagConfig ?? generateFlagConfig(id) }; + }); + return await db.insert(schema.teams).values(withFlags).returning(); } export async function findTeamById(id: string): Promise { diff --git a/app/models/user.ts b/app/models/user.ts index 238c593..81d616f 100644 --- a/app/models/user.ts +++ b/app/models/user.ts @@ -1,6 +1,7 @@ import { eq, inArray, and } from "drizzle-orm"; import { database } from "~/database/context"; import * as schema from "~/database/schema"; +import { generateFlagConfig } from "~/lib/flag-generator"; export type User = typeof schema.users.$inferSelect; export type NewUser = typeof schema.users.$inferInsert; @@ -13,7 +14,11 @@ export function getUserDisplayName( export async function createUser(data: NewUser): Promise { const db = database(); - const [user] = await db.insert(schema.users).values(data).returning(); + const id = data.id ?? crypto.randomUUID(); + const [user] = await db + .insert(schema.users) + .values({ ...data, id, flagConfig: data.flagConfig ?? generateFlagConfig(id) }) + .returning(); return user; } diff --git a/scripts/backfill-flag-configs.ts b/scripts/backfill-flag-configs.ts deleted file mode 100644 index f98df21..0000000 --- a/scripts/backfill-flag-configs.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { eq, isNull } from "drizzle-orm"; -import { db } from "~/server/db"; -import * as schema from "~/database/schema"; -import { generateFlagConfig } from "~/lib/flag-generator"; - -async function main() { - const users = await db.query.users.findMany({ - where: isNull(schema.users.flagConfig), - }); - const teams = await db.query.teams.findMany({ - where: isNull(schema.teams.flagConfig), - }); - - for (const user of users) { - await db - .update(schema.users) - .set({ flagConfig: generateFlagConfig(user.id), updatedAt: new Date() }) - .where(eq(schema.users.id, user.id)); - } - - for (const team of teams) { - await db - .update(schema.teams) - .set({ flagConfig: generateFlagConfig(team.id), updatedAt: new Date() }) - .where(eq(schema.teams.id, team.id)); - } - - console.log(`Backfilled ${users.length} users and ${teams.length} teams.`); -} - -main() - .then(() => { - process.exit(0); - }) - .catch((error) => { - console.error(error); - process.exit(1); - });