Fix local Docker startup for workflow app
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { mkdirSync } from 'node:fs';
|
||||
import { dirname } from 'node:path';
|
||||
import { Database } from 'bun:sqlite';
|
||||
import { drizzle } from 'drizzle-orm/bun-sqlite';
|
||||
import { migrate } from 'drizzle-orm/bun-sqlite/migrator';
|
||||
import { drizzle as drizzlePostgres } from 'drizzle-orm/postgres-js';
|
||||
import { migrate as migratePostgres } from 'drizzle-orm/postgres-js/migrator';
|
||||
import postgres from 'postgres';
|
||||
import {
|
||||
ensureFinancialIngestionSchemaHealthy,
|
||||
resolveFinancialSchemaRepairMode
|
||||
} from '../lib/server/db/financial-ingestion-schema';
|
||||
import { ensureLocalSqliteSchema } from '../lib/server/db/sqlite-schema-compat';
|
||||
import { resolveSqlitePath } from './dev-env';
|
||||
|
||||
function trim(value: string | undefined) {
|
||||
@@ -46,19 +47,27 @@ function getDatabasePath() {
|
||||
return databasePath;
|
||||
}
|
||||
|
||||
function runWorkflowSetup() {
|
||||
async function runWorkflowSetup() {
|
||||
const startedAt = performance.now();
|
||||
const result = spawnSync('./node_modules/.bin/workflow-postgres-setup', [], {
|
||||
env: process.env,
|
||||
stdio: 'inherit'
|
||||
});
|
||||
const connectionString = trim(process.env.WORKFLOW_POSTGRES_URL)
|
||||
|| trim(process.env.DATABASE_URL)
|
||||
|| 'postgres://world:world@localhost:5432/world';
|
||||
const migrationsFolder = trim(process.env.WORKFLOW_MIGRATIONS_DIR) || '/app/workflow-migrations';
|
||||
const pgClient = postgres(connectionString, { max: 1 });
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
console.info('🔧 Setting up database schema...');
|
||||
console.info(`📍 Connection: ${connectionString.replace(/^(\w+:\/\/)([^@]+)@/, '$1[redacted]@')}`);
|
||||
console.info(`📂 Running migrations from: ${migrationsFolder}`);
|
||||
|
||||
if (result.status !== 0) {
|
||||
throw new Error(`workflow-postgres-setup failed with exit code ${result.status ?? 'unknown'}`);
|
||||
try {
|
||||
const db = drizzlePostgres(pgClient);
|
||||
await migratePostgres(db, {
|
||||
migrationsFolder,
|
||||
migrationsTable: 'workflow_migrations',
|
||||
migrationsSchema: 'workflow_drizzle'
|
||||
});
|
||||
} finally {
|
||||
await pgClient.end({ timeout: 5 });
|
||||
}
|
||||
|
||||
log(`workflow-postgres-setup completed in ${formatDuration(startedAt)}`);
|
||||
@@ -77,7 +86,7 @@ function runDatabaseMigrations() {
|
||||
|
||||
try {
|
||||
client.exec('PRAGMA foreign_keys = ON;');
|
||||
migrate(drizzle(client), { migrationsFolder: './drizzle' });
|
||||
ensureLocalSqliteSchema(client);
|
||||
|
||||
const repairResult = ensureFinancialIngestionSchemaHealthy(client, {
|
||||
mode: resolveFinancialSchemaRepairMode(process.env.FINANCIAL_SCHEMA_REPAIR_MODE)
|
||||
@@ -102,7 +111,7 @@ try {
|
||||
log('starting production bootstrap');
|
||||
|
||||
if (shouldRunWorkflowSetup) {
|
||||
runWorkflowSetup();
|
||||
await runWorkflowSetup();
|
||||
} else {
|
||||
log('workflow-postgres-setup skipped');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user