From 09e7fe88d6025dbf82958404f82724e357560445 Mon Sep 17 00:00:00 2001 From: gc <30398469+gc@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:13:20 +1100 Subject: [PATCH] Add item metadata table --- prisma/schema.prisma | 8 ++++++++ src/lib/startupScripts.ts | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index fafeb0c934..ade618d928 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1110,3 +1110,11 @@ model GiftBox { @@map("gift_box") } + +model ItemMetadata { + id Int @id + + name String? + + @@map("item_metadata") +} diff --git a/src/lib/startupScripts.ts b/src/lib/startupScripts.ts index 845224ce6e..193b93ef20 100644 --- a/src/lib/startupScripts.ts +++ b/src/lib/startupScripts.ts @@ -1,3 +1,5 @@ +import { Items } from 'oldschooljs'; + import { prisma } from './settings/prisma'; import { logError } from './util/logError'; @@ -121,6 +123,18 @@ startupScripts.push({ sql: 'CREATE UNIQUE INDEX IF NOT EXISTS activity_only_one_task ON activity (user_id, completed) WHERE NOT completed;' }); +const itemMetaDataNames = Items.map(item => `(${item.id}, '${item.name.replace(/'/g, "''")}')`).join(', '); +const itemMetaDataQuery = ` +INSERT INTO item_metadata (id, name) +VALUES ${itemMetaDataNames} +ON CONFLICT (id) +DO + UPDATE SET name = EXCLUDED.name +WHERE item_metadata.name IS DISTINCT FROM EXCLUDED.name; +`; + +startupScripts.push({ sql: itemMetaDataQuery }); + export async function runStartupScripts() { for (const query of startupScripts) { await prisma