| 18 | constructor(@Inject(Pool) private readonly pool: Pool) {} |
| 19 | |
| 20 | async onModuleInit(): Promise<void> { |
| 21 | if (process.env.SHIPSEC_SKIP_MIGRATION_CHECK === 'true') { |
| 22 | this.logger.warn('Skipping migration check because SHIPSEC_SKIP_MIGRATION_CHECK=true.'); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | const client = await this.pool.connect(); |
| 27 | |
| 28 | try { |
| 29 | const { rows } = await client.query<{ table_name: string }>( |
| 30 | `select table_name |
| 31 | from information_schema.tables |
| 32 | where table_schema = 'public' |
| 33 | and table_name = any($1::text[])`, |
| 34 | [REQUIRED_TABLES], |
| 35 | ); |
| 36 | |
| 37 | const present = new Set(rows.map((row) => row.table_name)); |
| 38 | const missing = REQUIRED_TABLES.filter((table) => !present.has(table)); |
| 39 | |
| 40 | if (missing.length > 0) { |
| 41 | const message = |
| 42 | `Database schema incomplete: missing tables [${missing.join(', ')}]. ` + |
| 43 | 'Run `bun run migrate` (alias for `bun --cwd backend x drizzle-kit push`) before starting the backend.'; |
| 44 | this.logger.error(message); |
| 45 | throw new Error(message); |
| 46 | } |
| 47 | |
| 48 | this.logger.log('Database schema check passed – required tables are present.'); |
| 49 | } catch (error) { |
| 50 | this.logger.error( |
| 51 | 'Failed to verify database schema. Run `bun run migrate` to ensure migrations are applied.', |
| 52 | error instanceof Error ? error.stack : undefined, |
| 53 | ); |
| 54 | throw error; |
| 55 | } finally { |
| 56 | client.release(); |
| 57 | } |
| 58 | } |
| 59 | } |