feat(db): auto-migrate local SQLite database inside container on boot

This commit is contained in:
2026-05-29 14:19:08 -07:00
parent 98e75b81b5
commit 6d06cd9426
3 changed files with 26 additions and 9 deletions

View File

@@ -31,9 +31,6 @@ export function getDb(env?: Env) {
let authToken: string | undefined; let authToken: string | undefined;
if (typeof Bun !== 'undefined') { if (typeof Bun !== 'undefined') {
// Check if we're running in test mode
// The test runner (run-tests.ts) overrides TURSO_DATABASE_URL with the test database
// so we can just use the regular environment variables
databaseUrl = Bun.env.TURSO_DATABASE_URL; databaseUrl = Bun.env.TURSO_DATABASE_URL;
authToken = Bun.env.TURSO_AUTH_TOKEN; authToken = Bun.env.TURSO_AUTH_TOKEN;
} else if (env) { } else if (env) {
@@ -42,12 +39,15 @@ export function getDb(env?: Env) {
authToken = env.TURSO_AUTH_TOKEN; authToken = env.TURSO_AUTH_TOKEN;
} }
// Standalone fallback: if no external Turso database is provided, fall back to local container SQLite file
if (!databaseUrl) { if (!databaseUrl) {
throw new Error('TURSO_DATABASE_URL environment variable is required'); console.log('[Database Client] TURSO_DATABASE_URL not found. Falling back to local SQLite: file:local.db');
databaseUrl = "file:local.db";
} }
if (!authToken) { if (!authToken) {
throw new Error('TURSO_AUTH_TOKEN environment variable is required'); console.log('[Database Client] TURSO_AUTH_TOKEN not found. Using mock-token.');
authToken = "mock-token";
} }
// Create Turso client // Create Turso client

View File

@@ -11,15 +11,17 @@ console.log('🚀 Running database migrations...');
async function runMigrations() { async function runMigrations() {
// Get database URL and auth token // Get database URL and auth token
const databaseUrl = process.env.TURSO_DATABASE_URL || Bun.env?.TURSO_DATABASE_URL; let databaseUrl = process.env.TURSO_DATABASE_URL || Bun.env?.TURSO_DATABASE_URL;
const authToken = process.env.TURSO_AUTH_TOKEN || Bun.env?.TURSO_AUTH_TOKEN; let authToken = process.env.TURSO_AUTH_TOKEN || Bun.env?.TURSO_AUTH_TOKEN;
if (!databaseUrl) { if (!databaseUrl) {
throw new Error('TURSO_DATABASE_URL environment variable is required'); console.log('[Migration] TURSO_DATABASE_URL not found. Migrating local SQLite: file:local.db');
databaseUrl = "file:local.db";
} }
if (!authToken) { if (!authToken) {
throw new Error('TURSO_AUTH_TOKEN environment variable is required'); console.log('[Migration] TURSO_AUTH_TOKEN not found. Using mock-token.');
authToken = "mock-token";
} }
// Create Turso client // Create Turso client

View File

@@ -27,6 +27,21 @@ import type { HonoContext } from './types/context';
const app = new Hono<HonoContext>(); const app = new Hono<HonoContext>();
// Auto-run migrations on startup if using local SQLite fallback inside container
const isBunRuntime = typeof Bun !== 'undefined';
if (isBunRuntime && !Bun.env.TURSO_DATABASE_URL) {
console.log('[Startup] Auto-running database migrations for local SQLite...');
try {
const proc = Bun.spawnSync(["bun", "run", "src/db/migrate.ts"]);
console.log('[Startup] Migration output:', proc.stdout?.toString());
if (proc.stderr && proc.stderr.length > 0) {
console.error('[Startup] Migration error:', proc.stderr.toString());
}
} catch (err) {
console.error('[Startup] Failed to run database migrations on boot:', err);
}
}
// Determine if running in development mode // Determine if running in development mode
const isDevelopment = typeof Bun !== 'undefined' ? Bun.env.NODE_ENV !== 'production' : false; const isDevelopment = typeof Bun !== 'undefined' ? Bun.env.NODE_ENV !== 'production' : false;