diff --git a/src/db/client.ts b/src/db/client.ts index a8f9c94..d411e2e 100644 --- a/src/db/client.ts +++ b/src/db/client.ts @@ -31,9 +31,6 @@ export function getDb(env?: Env) { let authToken: string | 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; authToken = Bun.env.TURSO_AUTH_TOKEN; } else if (env) { @@ -42,12 +39,15 @@ export function getDb(env?: Env) { authToken = env.TURSO_AUTH_TOKEN; } + // Standalone fallback: if no external Turso database is provided, fall back to local container SQLite file 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) { - 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 diff --git a/src/db/migrate.ts b/src/db/migrate.ts index 8c95759..5ec83c5 100644 --- a/src/db/migrate.ts +++ b/src/db/migrate.ts @@ -11,15 +11,17 @@ console.log('🚀 Running database migrations...'); async function runMigrations() { // Get database URL and auth token - const databaseUrl = process.env.TURSO_DATABASE_URL || Bun.env?.TURSO_DATABASE_URL; - const authToken = process.env.TURSO_AUTH_TOKEN || Bun.env?.TURSO_AUTH_TOKEN; + let databaseUrl = process.env.TURSO_DATABASE_URL || Bun.env?.TURSO_DATABASE_URL; + let authToken = process.env.TURSO_AUTH_TOKEN || Bun.env?.TURSO_AUTH_TOKEN; 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) { - 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 diff --git a/src/index.ts b/src/index.ts index 1f17158..4cddbb1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,21 @@ import type { HonoContext } from './types/context'; const app = new Hono(); +// 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 const isDevelopment = typeof Bun !== 'undefined' ? Bun.env.NODE_ENV !== 'production' : false;