Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/setup-graphile-worker-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/world-postgres": patch
---

`workflow-postgres-setup` now also bootstraps the `graphile_worker` schema, fixing potential race on setup when starting the app and a test runner at the same time
18 changes: 18 additions & 0 deletions packages/world-postgres/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url';
import { config } from 'dotenv';
import { drizzle } from 'drizzle-orm/node-postgres';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import { makeWorkerUtils } from 'graphile-worker';
import { Pool } from 'pg';

const __dirname = dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -44,6 +45,23 @@ async function setupDatabase() {
migrationsSchema: 'workflow_drizzle',
});

// Also bootstrap the graphile-worker schema. Without this, the first
// process to call `world.start()` against a fresh DB is responsible
// for running graphile-worker's `installSchema`, and concurrent
// callers (e.g. the dev server + the test runner) can race on the
// not-race-safe `CREATE SCHEMA IF NOT EXISTS` and fail with
// `duplicate key value violates unique constraint
// "pg_namespace_nspname_index"`. Running it here, single-process,
// before any consumer starts means later `installSchema` calls find
// the schema present and skip the racing DDL path entirely.
console.log('📂 Bootstrapping graphile-worker schema...');
const workerUtils = await makeWorkerUtils({ pgPool: pool });
try {
await workerUtils.migrate();
} finally {
await workerUtils.release();
}

console.log('✅ Database schema created successfully!');

await pool.end();
Expand Down
Loading