{"record":{"id":"5d72d432b159e734","repo":"coleam00/Archon","slug":"database-dialect-not-initialized-this-indicates-t","errorCode":null,"errorMessage":"Database dialect not initialized. This indicates the database connection failed during initialization. Check logs for database connection errors.","messagePattern":"Database dialect not initialized\\. This indicates the database connection failed during initialization\\. Check logs for database connection errors\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"packages/core/src/db/connection.ts","lineNumber":85,"sourceCode":"        'db.docker_using_sqlite'\n      );\n    }\n  }\n\n  return database;\n}\n\n/**\n * Get the SQL dialect for the current database\n */\nexport function getDialect(): SqlDialect {\n  if (!dialect) {\n    // Initialize database to set dialect\n    getDatabase();\n  }\n\n  if (!dialect) {\n    throw new Error(\n      'Database dialect not initialized. This indicates the database connection failed during initialization. ' +\n        'Check logs for database connection errors.'\n    );\n  }\n\n  return dialect;\n}\n\n/**\n * Get the current database type without initializing the database\n * Useful for version/info commands that don't need a connection\n */\nexport function getDatabaseType(): 'postgresql' | 'sqlite' {\n  return process.env.DATABASE_URL ? 'postgresql' : 'sqlite';\n}\n\n/**\n * Read the recorded schema vintage (#2316): which Archon build created this database","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/core/src/db/connection.ts#L67-L103","documentation":"getDialect returns the database dialect detected during initialization. If it is still unset after triggering getDatabase(), initialization failed (connection refused, bad DSN, driver error), so it throws explaining that the dialect was never initialized and pointing at connection-error logs. Everything that needs dialect-aware SQL (e.g. dialect.now()) depends on this succeeding.","triggerScenarios":"Any call to getDialect()/dialect()/now() before or after a failed getDatabase() initialization — e.g. database server down, wrong DSN, unsupported dialect string in config.","commonSituations":"Postgres not running or wrong port; DATABASE_URL typo or missing env var; SQLite file path not writable; config dialect set to an unsupported value so getDatabase() threw during boot and left `dialect` unset; calling a query function before the app's DB init step completed.","solutions":["Fix the database connection: check DATABASE_URL/DSN, server reachability (pg_isready / ping), and credentials.","Read earlier logs for the underlying connection error thrown by getDatabase() — this error is secondary to that one.","Verify the configured dialect is supported (sqlite/postgres) and matches the DSN.","Ensure DB init (getDatabase()) completes at startup before any code path queries the dialect.","If embedded, confirm the SQLite data directory exists and is writable."],"exampleFix":"// before: DSN misconfigured\nDATABASE_URL=postgres://localhost:5433/archon  // wrong port\n// after\nDATABASE_URL=postgres://localhost:5432/archon  # and verify: pg_isready -p 5432","handlingStrategy":"try-catch","validationCode":"// health probe before any dialect-dependent query\nasync function assertDbReady() {\n  try { await pool.query('SELECT 1'); } catch (e) {\n    throw new Error(`database unreachable at startup: ${e.message}`);\n  }\n  getDatabase(); // ensures dialect is set\n}","typeGuard":"function isDialectNotInitialized(e: unknown): boolean {\n  return e instanceof Error && e.message.startsWith('Database dialect not initialized');\n}","tryCatchPattern":"try {\n  await queryWithDialect(...);\n} catch (e) {\n  if (isDialectNotInitialized(e)) {\n    console.error('DB init failed earlier; check DATABASE_URL and that the server is reachable');\n    process.exit(1); // fail fast at boot, not per-request\n  }\n  throw e;\n}","preventionTips":["Call getDatabase() once at application startup and abort boot if it throws.","Validate DATABASE_URL/DSN and dialect config with a startup connectivity check (SELECT 1).","Use a health endpoint/probe so orchestrators restart the app on DB-init failure.","Confirm the Postgres host/port and that SQLite data dirs are writable before deployment."],"tags":["database","initialization","configuration","connection"],"backgroundTag":"database-not-initialized","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}