From 1db53c540a4f571882e4f5f260f59f25a1271495 Mon Sep 17 00:00:00 2001 From: w0244079 <michael.caines@nscc.ca> Date: Wed, 26 Feb 2025 13:52:59 -0400 Subject: [PATCH] fix: modify db seed to ensure extension exists before transaction --- dashboard/final-example/app/seed/route.ts | 25 ++++++++++++--------- dashboard/starter-example/app/seed/route.ts | 25 ++++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/dashboard/final-example/app/seed/route.ts b/dashboard/final-example/app/seed/route.ts index c6428b27..80053900 100644 --- a/dashboard/final-example/app/seed/route.ts +++ b/dashboard/final-example/app/seed/route.ts @@ -4,8 +4,11 @@ import { invoices, customers, revenue, users } from '../lib/placeholder-data'; const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' }); -async function seedUsers() { +async function ensureExtension() { await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; +} + +async function seedUsers() { await sql` CREATE TABLE IF NOT EXISTS users ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, @@ -30,8 +33,6 @@ async function seedUsers() { } async function seedInvoices() { - await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - await sql` CREATE TABLE IF NOT EXISTS invoices ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, @@ -56,8 +57,6 @@ async function seedInvoices() { } async function seedCustomers() { - await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - await sql` CREATE TABLE IF NOT EXISTS customers ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, @@ -103,12 +102,16 @@ async function seedRevenue() { export async function GET() { try { - const result = await sql.begin((sql) => [ - seedUsers(), - seedCustomers(), - seedInvoices(), - seedRevenue(), - ]); + // Ensure the extension is created before running the transaction + await ensureExtension(); + + // Run seeding within a transaction + const result = await sql.begin(async (sql) => { + await seedUsers(); + await seedCustomers(); + await seedInvoices(); + await seedRevenue(); + }); return Response.json({ message: 'Database seeded successfully' }); } catch (error) { diff --git a/dashboard/starter-example/app/seed/route.ts b/dashboard/starter-example/app/seed/route.ts index c6428b27..80053900 100644 --- a/dashboard/starter-example/app/seed/route.ts +++ b/dashboard/starter-example/app/seed/route.ts @@ -4,8 +4,11 @@ import { invoices, customers, revenue, users } from '../lib/placeholder-data'; const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' }); -async function seedUsers() { +async function ensureExtension() { await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; +} + +async function seedUsers() { await sql` CREATE TABLE IF NOT EXISTS users ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, @@ -30,8 +33,6 @@ async function seedUsers() { } async function seedInvoices() { - await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - await sql` CREATE TABLE IF NOT EXISTS invoices ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, @@ -56,8 +57,6 @@ async function seedInvoices() { } async function seedCustomers() { - await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - await sql` CREATE TABLE IF NOT EXISTS customers ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, @@ -103,12 +102,16 @@ async function seedRevenue() { export async function GET() { try { - const result = await sql.begin((sql) => [ - seedUsers(), - seedCustomers(), - seedInvoices(), - seedRevenue(), - ]); + // Ensure the extension is created before running the transaction + await ensureExtension(); + + // Run seeding within a transaction + const result = await sql.begin(async (sql) => { + await seedUsers(); + await seedCustomers(); + await seedInvoices(); + await seedRevenue(); + }); return Response.json({ message: 'Database seeded successfully' }); } catch (error) {