()
| 18 | } |
| 19 | |
| 20 | async initialize() { |
| 21 | if (this.initialized) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | try { |
| 26 | logger.info('Attempting to connect to PostgreSQL...'); |
| 27 | const pgConnected = await pgDb.connect(); |
| 28 | if (pgConnected) { |
| 29 | this.db = pgDb; |
| 30 | this.connectionType = 'postgresql'; |
| 31 | this.degradedReason = null; |
| 32 | logger.info('✅ PostgreSQL Database initialized - using persistent database'); |
| 33 | this.initialized = true; |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | const pgFailure = pgDb.getLastFailure?.(); |
| 38 | if (pgFailure?.reason === 'SCHEMA_VERSION_MISMATCH') { |
| 39 | const schemaError = new Error( |
| 40 | `Schema version mismatch detected (${pgFailure.message}). Run migrations before startup.` |
| 41 | ); |
| 42 | schemaError.code = 'SCHEMA_VERSION_MISMATCH'; |
| 43 | throw schemaError; |
| 44 | } |
| 45 | } catch (error) { |
| 46 | logger.warn('PostgreSQL connection failed:', error.message); |
| 47 | |
| 48 | if (error.code === 'SCHEMA_VERSION_MISMATCH') { |
| 49 | throw error; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | this.db = new MemoryStorage(); |
| 54 | this.useFallback = true; |
| 55 | this.connectionType = 'memory'; |
| 56 | this.degradedReason = 'POSTGRES_UNAVAILABLE'; |
| 57 | logger.warn('⚠️ DATABASE DEGRADED MODE ENABLED - Using in-memory storage (data will be lost on restart)'); |
| 58 | logger.warn('⚠️ Please check PostgreSQL connection and restart the bot when fixed'); |
| 59 | this.initialized = true; |
| 60 | this.degradedModeWarningShown = true; |
| 61 | } |
| 62 | |
| 63 | async set(key, value, ttl = null) { |
| 64 | if (this.useFallback) { |
no test coverage detected