fix(schema): ensureTables() fügt jetzt fehlende Spalten nachträglich hinzu – verhindert altes Schema-Problem
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 11m16s

This commit is contained in:
Pepe Ziberi
2026-05-21 15:42:22 +02:00
parent 6588959b50
commit 08ecf4c61a
2 changed files with 56 additions and 2 deletions

View File

@@ -32,6 +32,32 @@ async function ensureTables() {
"tenantId" TEXT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE "tenantId" TEXT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE
) )
`) `)
const symbolCols = [
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "name" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "svgPath" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "isUploaded" BOOLEAN NOT NULL DEFAULT false`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "categoryId" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "migratedFromIconId" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "customName" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "sortOrder" INTEGER`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
]
for (const sql of symbolCols) {
try { await prisma.$executeRawUnsafe(sql) } catch (e) { /* ignore */ }
}
const catCols = [
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "description" TEXT`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "sortOrder" INTEGER NOT NULL DEFAULT 0`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "isActive" BOOLEAN NOT NULL DEFAULT true`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
]
for (const sql of catCols) {
try { await prisma.$executeRawUnsafe(sql) } catch (e) { /* ignore */ }
}
} }
export async function GET() { export async function GET() {
@@ -93,7 +119,7 @@ export async function GET() {
`SELECT ts.*, tc.id as "catId", tc.name as "catName" `SELECT ts.*, tc.id as "catId", tc.name as "catName"
FROM tenant_symbols ts FROM tenant_symbols ts
LEFT JOIN tenant_categories tc ON ts."categoryId" = tc.id LEFT JOIN tenant_categories tc ON ts."categoryId" = tc.id
WHERE ts."tenantId" = $1 AND ts."isActive" = true WHERE ts."tenantId" = $1 AND (ts."isActive" IS NULL OR ts."isActive" = true)
ORDER BY COALESCE(tc."sortOrder", 9999) ASC, COALESCE(ts."sortOrder", 9999) ASC`, ORDER BY COALESCE(tc."sortOrder", 9999) ASC, COALESCE(ts."sortOrder", 9999) ASC`,
tenantId tenantId
) as any[] ) as any[]

View File

@@ -14,6 +14,7 @@ async function getTenantId() {
} }
async function ensureTables() { async function ensureTables() {
// ─── Create tables (idempotent) ───
await prisma.$executeRawUnsafe(` await prisma.$executeRawUnsafe(`
CREATE TABLE IF NOT EXISTS tenant_symbols ( CREATE TABLE IF NOT EXISTS tenant_symbols (
id TEXT PRIMARY KEY DEFAULT gen_random_uuid(), id TEXT PRIMARY KEY DEFAULT gen_random_uuid(),
@@ -43,6 +44,33 @@ async function ensureTables() {
"tenantId" TEXT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE "tenantId" TEXT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE
) )
`) `)
// ─── Add missing columns (idempotent) ───
const symbolCols = [
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "name" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "svgPath" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "isUploaded" BOOLEAN NOT NULL DEFAULT false`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "categoryId" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "migratedFromIconId" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "customName" TEXT`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "sortOrder" INTEGER`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
`ALTER TABLE tenant_symbols ADD COLUMN IF NOT EXISTS "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
]
for (const sql of symbolCols) {
try { await prisma.$executeRawUnsafe(sql) } catch (e) { /* ignore */ }
}
const catCols = [
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "description" TEXT`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "sortOrder" INTEGER NOT NULL DEFAULT 0`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "isActive" BOOLEAN NOT NULL DEFAULT true`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
`ALTER TABLE tenant_categories ADD COLUMN IF NOT EXISTS "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP`,
]
for (const sql of catCols) {
try { await prisma.$executeRawUnsafe(sql) } catch (e) { /* ignore */ }
}
} }
// ─── GET ──────────────────────────────────────────────────────────────────── // ─── GET ────────────────────────────────────────────────────────────────────
@@ -95,7 +123,7 @@ export async function GET(req: NextRequest) {
// Get categories for ordering // Get categories for ordering
const categories: any[] = await prisma.$queryRawUnsafe( const categories: any[] = await prisma.$queryRawUnsafe(
`SELECT * FROM tenant_categories WHERE "tenantId" = $1 AND "isActive" = true ORDER BY "sortOrder" ASC`, `SELECT * FROM tenant_categories WHERE "tenantId" = $1 AND ("isActive" IS NULL OR "isActive" = true) ORDER BY "sortOrder" ASC`,
tenantId tenantId
) )