|
1 | 1 | const sqlite3 = require('sqlite3').verbose(); |
2 | 2 | const path = require('path'); |
3 | 3 |
|
4 | | -// Import PostgreSQL manager for production |
5 | | -let PostgresDatabaseManager = null; |
6 | | -if (process.env.NODE_ENV === 'production' && process.env.DATABASE_URL) { |
7 | | - try { |
8 | | - PostgresDatabaseManager = require('./database-postgres'); |
9 | | - } catch (error) { |
10 | | - console.error('Failed to load PostgreSQL manager:', error); |
11 | | - } |
12 | | -} |
| 4 | +// Import PostgreSQL manager dynamically |
13 | 5 |
|
14 | 6 | class DatabaseManager { |
15 | 7 | constructor() { |
@@ -352,10 +344,23 @@ let dbManager = null; |
352 | 344 |
|
353 | 345 | function getDatabase() { |
354 | 346 | if (!dbManager) { |
355 | | - // Use PostgreSQL in production, SQLite in development |
356 | | - if (process.env.NODE_ENV === 'production' && process.env.DATABASE_URL) { |
357 | | - dbManager = new PostgresDatabaseManager(); |
| 347 | + console.log('🔍 Database selection debug:', { |
| 348 | + NODE_ENV: process.env.NODE_ENV, |
| 349 | + DATABASE_URL_exists: !!process.env.DATABASE_URL |
| 350 | + }); |
| 351 | + |
| 352 | + // Use PostgreSQL if DATABASE_URL is present (indicates production/cloud environment) |
| 353 | + if (process.env.DATABASE_URL) { |
| 354 | + console.log('📊 DATABASE_URL detected, using PostgreSQL database manager'); |
| 355 | + try { |
| 356 | + const PostgresDatabaseManager = require('./database-postgres'); |
| 357 | + dbManager = new PostgresDatabaseManager(); |
| 358 | + } catch (error) { |
| 359 | + console.error('❌ Failed to load PostgreSQL manager, falling back to SQLite:', error); |
| 360 | + dbManager = new DatabaseManager(); |
| 361 | + } |
358 | 362 | } else { |
| 363 | + console.log('📊 No DATABASE_URL, using SQLite database manager'); |
359 | 364 | dbManager = new DatabaseManager(); |
360 | 365 | } |
361 | 366 | } |
|
0 commit comments