Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(db): run the main database migrations by default
Yudhi Armyndharis0aefc13627dc
diff --git a/.env.example b/.env.exampleindex a5f23732b..88063f42d 100644--- a/.env.example+++ b/.env.example@@ -331,10 +331,11 @@ DATABASE_SYNCHRONIZE=false # WARNING: Set false in production! (data DB# DATABASE_LOGGING=false# SQLite file for the 'main' (auth/audit) connection. Must not resolve to DATABASE_NAME.# MAIN_DATABASE_NAME=./data/main.sqlite-# Auth/audit (main) DB schema management. Default ON (zero-config first boot).-# Set to "false" to manage the api_keys/audit_logs schema via the bundled main-owned-# migration instead of synchronize (migrationsRun creates them at boot).-# MAIN_DATABASE_SYNCHRONIZE=true+# Auth/audit (main) DB schema management. By default the bundled main-owned migrations create+# and upgrade api_keys/audit_logs at boot (an existing synchronize-built file is adopted in place).+# "true" switches to TypeORM synchronize instead: for development only, and warned about under+# NODE_ENV=production.+# MAIN_DATABASE_SYNCHRONIZE=false# DATABASE_SSL=false # Set true for managed Postgres (Supabase, Heroku, Render, Railway)# DATABASE_SSL_REJECT_UNAUTHORIZED=true # Set false to allow self-signed certs (only when DATABASE_SSL=true)# PostgreSQL connection pool size.diff --git a/docs/05-database-design.md b/docs/05-database-design.mdindex 1bb22b892..aa3dae35c 100644--- a/docs/05-database-design.md+++ b/docs/05-database-design.md@@ -818,13 +818,15 @@ OpenWA runs **two separate TypeORM connections**, each with its own migrations dMigrations are hand-authored and idempotent (`IF NOT EXISTS`) so they are safe to adopt on a database originally created by `synchronize`. The two connections differ in how schema is managed:- **data** — `synchronize` defaults **off**, so this connection is migration-managed by default. On PostgreSQL `migrationsRun` is hardcoded on, and `DATABASE_SYNCHRONIZE=true` is rejected outright at boot validation (it would drop the migration-created `body_ts` tsvector column that `/search` depends on). On SQLite there is no such rejection and `migrationsRun` is the inverse of `synchronize` — so an opted-in `DATABASE_SYNCHRONIZE=true` switches the data connection to entity-synchronized schema and turns its migrations **off**.-- **main** — `synchronize` defaults **on** (zero-config first boot) regardless of `NODE_ENV`; set `MAIN_DATABASE_SYNCHRONIZE=false` to manage `api_keys` / `audit_logs` via `migrations-main/` instead. Never both at once — `migrationsRun` on this connection is the inverse of `synchronize`.+- **main**: migration-managed by default, like data: `migrations-main/` creates and upgrades `api_keys` / `audit_logs` at boot. The chain is idempotent, so a `main.sqlite` an earlier release built with synchronize is adopted in place on the first boot (rows kept, missing columns added, the ledger written). `MAIN_DATABASE_SYNCHRONIZE=true` opts into synchronize instead, for development; boot logs a warning when it is set under `NODE_ENV=production`. `migrationsRun` on this connection is the inverse of `synchronize`.### Migration Files```src/database/migrations-main/ # main connection (auth + audit, SQLite)-└── 1779900000000-CreateAuthAuditTables.ts # creates api_keys + audit_logs+├── 1779900000000-CreateAuthAuditTables.ts # creates api_keys + audit_logs+├── 1786600000000-AddApiKeyAllowedChats.ts # api_keys.allowedChats+└── 1786610000000-DropSynchronizeIndexDuplicates.ts # drops synchronize-named duplicate indexessrc/database/migrations/ # data connection (pluggable)├── 1770108659848-AddMessageStatus.tsdiff --git a/src/app.module.ts b/src/app.module.tsindex f02e8122a..f625c6251 100644--- a/src/app.module.ts+++ b/src/app.module.ts@@ -10,6 +10,7 @@ import { createThrottlerRedisClient } from './common/throttler/throttler-redis.cimport configuration from './config/configuration';import { validateEnv } from './config/env.validation';import { createBootDataSource } from './database/pg-boot-migrations';+import { mainConnectionOptions } from './database/main-connection';import { SessionModule } from './modules/session/session.module';import { MessageModule } from './modules/message/message.module';import { TemplateModule } from './modules/template/template.module';@@ -129,27 +130,8 @@ if (dashboardServingEnabled && dashboardBuildPresent) {name: 'main',imports: [ConfigModule],inject: [ConfigService],- useFactory: (configService: ConfigService) => {- // Default ON for zero-config first boot. When disabled- // (MAIN_DATABASE_SYNCHRONIZE=false), the main-owned migrations create the- // api_keys/audit_logs schema instead — never both at once.- const synchronize = configService.get<boolean>('database.synchronize', true);- return {- name: 'main',- type: 'better-sqlite3' as const,- database: configService.get<string>('database.database', './data/main.sqlite'),- entities: [- __dirname + '/modules/auth/**/*.entity{.ts,.js}',- __dirname + '/modules/audit/**/*.entity{.ts,.js}',- ],- // Dedicated migrations dir for the main connection only (must NOT run the- // data-connection migrations, which target session/webhook/message tables).- migrations: [__dirname + '/database/migrations-main/*{.ts,.js}'],- synchronize,- migrationsRun: !synchronize,- logging: configService.get<boolean>('database.logging', false),- };- },+ // Migrations by default, synchronize only on MAIN_DATABASE_SYNCHRONIZE=true (see the helper).+ useFactory: (configService: ConfigService) => mainConnectionOptions(configService),}),// Data Storage Database (pluggable - user data)diff --git a/src/config/bootstrap-security.spec.ts b/src/config/bootstrap-security.spec.tsindex 3bdb67332..d21f1d04d 100644--- a/src/config/bootstrap-security.spec.ts+++ b/src/config/bootstrap-security.spec.ts@@ -7,6 +7,7 @@ import {resolveBodyLimit,assertNoDefaultSecretsInProduction,isApiKeyPepperMissingInProduction,+ isMainDbSynchronizeInProduction,isNodeEnvUnset,} from './bootstrap-security';@@ -419,6 +420,17 @@ describe('assertNoDefaultSecretsInProduction', () => {});});+describe('isMainDbSynchronizeInProduction', () => {+ it('is true only for an explicit MAIN_DATABASE_SYNCHRONIZE=true in production', () => {+ expect(isMainDbSynchronizeInProduction('production', 'true')).toBe(true);+ expect(isMainDbSynchronizeInProduction('production', 'false')).toBe(false);+ expect(isMainDbSynchronizeInProduction('production', undefined)).toBe(false);+ expect(isMainDbSynchronizeInProduction('production', '')).toBe(false);+ expect(isMainDbSynchronizeInProduction('test', 'true')).toBe(false);+ expect(isMainDbSynchronizeInProduction(undefined, 'true')).toBe(false);+ });+});+describe('isApiKeyPepperMissingInProduction', () => {it('is true in production when no pepper is set (incl. empty/whitespace)', () => {expect(isApiKeyPepperMissingInProduction('production', undefined)).toBe(true);diff --git a/src/config/bootstrap-security.ts b/src/config/bootstrap-security.tsindex 45b100084..582c3f98b 100644--- a/src/config/bootstrap-security.ts+++ b/src/config/bootstrap-security.ts@@ -135,6 +135,16 @@ export function isApiKeyPepperMissingInProduction(nodeEnv?: string, apiKeyPepperreturn nodeEnv === 'production' && !apiKeyPepper?.trim();}+/**+ * Whether to warn that the main (auth/audit) DB schema is managed by synchronize in production.+ * Synchronize rebuilds api_keys/audit_logs from whatever release is running, so a rollback to an+ * older image reshapes them (dropping columns it does not know) and the migration chain no longer+ * applies. Advisory only: the explicit opt-in keeps working.+ */+export function isMainDbSynchronizeInProduction(nodeEnv?: string, mainDbSynchronize?: string): boolean {+ return nodeEnv === 'production' && mainDbSynchronize === 'true';+}+/*** Whether NODE_ENV is unset or blank. That is the deliberate local-dev default, but it silently* degrades four controls to their dev posture: the default-secret assert is skipped, a wildcarddiff --git a/src/config/configuration.spec.ts b/src/config/configuration.spec.tsindex c75cc6be5..7c0f1a119 100644--- a/src/config/configuration.spec.ts+++ b/src/config/configuration.spec.ts@@ -14,12 +14,14 @@ describe('configuration — main DB synchronize', () => {else process.env.MAIN_DATABASE_SYNCHRONIZE = orig;});- it('defaults main synchronize ON (zero-config first boot)', () => {+ it('defaults the main DB to its migration chain (synchronize off), including a blank compose forward', () => {delete process.env.MAIN_DATABASE_SYNCHRONIZE;- expect(configuration().database.synchronize).toBe(true);+ expect(configuration().database.synchronize).toBe(false);+ process.env.MAIN_DATABASE_SYNCHRONIZE = '';+ expect(configuration().database.synchronize).toBe(false);});- it('disables synchronize only when MAIN_DATABASE_SYNCHRONIZE="false"', () => {+ it('synchronizes only when MAIN_DATABASE_SYNCHRONIZE="true"', () => {process.env.MAIN_DATABASE_SYNCHRONIZE = 'false';expect(configuration().database.synchronize).toBe(false);process.env.MAIN_DATABASE_SYNCHRONIZE = 'true';diff --git a/src/config/configuration.ts b/src/config/configuration.tsindex b831f3577..781dac810 100644--- a/src/config/configuration.ts+++ b/src/config/configuration.ts@@ -160,13 +160,12 @@ export default () => ({// SQLite file for the auth/audit DB. Overridable (e.g. e2e points it at a temp file) so tests// never write api keys into the developer's ./data/main.sqlite.database: process.env.MAIN_DATABASE_NAME || './data/main.sqlite',- // Schema management for the auth/audit DB. Default ON (zero-config first boot).- // Set MAIN_DATABASE_SYNCHRONIZE=false to manage schema via the main-owned migrations- // instead (migrationsRun then creates api_keys/audit_logs). When disabled, run the- // main-connection migrations explicitly with `npm run migration:run:main` (or- // `migration:run:main:prod` for the compiled image) — the plain `migration:run` only- // manages the data connection.- synchronize: process.env.MAIN_DATABASE_SYNCHRONIZE !== 'false',+ // Schema management for the auth/audit DB. By default the main-owned migrations run at boot+ // (they also adopt a file an earlier release built with synchronize); run them by hand with+ // `npm run migration:run:main` (or `migration:run:main:prod` for the compiled image), since the+ // plain `migration:run` only manages the data connection. MAIN_DATABASE_SYNCHRONIZE=true opts+ // into synchronize instead, which main.ts warns about in production.+ synchronize: process.env.MAIN_DATABASE_SYNCHRONIZE === 'true',logging: process.env.DATABASE_LOGGING === 'true',},diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 6eafd47d4..595a9b562 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -465,9 +465,8 @@ export function validateEnv(config: EnvConfig): EnvConfig {// `!== 'false'`, so a typo keeps the SECURE value — but it is still not the flag the operator set,// and it is only meaningful alongside DATABASE_SSL above.'DATABASE_SSL_REJECT_UNAUTHORIZED',- // `!== 'false'`, so a typo keeps synchronize ON — and app.module.ts derives `migrationsRun` from- // its negation, so the main connection's migration ledger silently never advances for an operator- // who deliberately opted into migration-managed api_keys/audit_logs.+ // `=== 'true'`: a typo keeps the main connection on its migrations, so an operator who meant to+ // opt into synchronize for api_keys/audit_logs silently does not get it.'MAIN_DATABASE_SYNCHRONIZE',// Read with `=== 'true'` by the plugin ingress gate: a typo turns an intentional// `ALLOW_UNSIGNED_INGRESS=true` back off, and a route the operator meant to open stops loading.diff --git a/src/database/data-source-main.ts b/src/database/data-source-main.tsindex f1b16fa7c..03cb513df 100644--- a/src/database/data-source-main.ts+++ b/src/database/data-source-main.ts@@ -20,8 +20,7 @@ if (sqlitePathCollision) {* The app runs the main connection as a separate, ALWAYS-SQLite connection (app.module.ts), distinct* from the pluggable data connection. The default data-source.ts CLI only manages the data* connection's migrations, so without this the CLI could not run/generate the main-owned migrations- * (migrations-main) — which matters the moment boot auto-migration is turned off- * (MAIN_DATABASE_SYNCHRONIZE=false), where the schema must be managed via the CLI instead.+ * (migrations-main), which also run at boot by default (MAIN_DATABASE_SYNCHRONIZE unset or false).** Mirrors the runtime main connection exactly: SQLite at ./data/main.sqlite, auth/audit entities,* migrations-main. synchronize is always false here — the CLI manages schema via migrations.diff --git a/src/database/main-connection.ts b/src/database/main-connection.tsnew file mode 100644index 000000000..46900bb5f--- /dev/null+++ b/src/database/main-connection.ts@@ -0,0 +1,32 @@+import { join } from 'path';+import type { ConfigService } from '@nestjs/config';+import type { DataSourceOptions } from 'typeorm';++/**+ * TypeORM options for the main (auth/audit) connection, which is always a node-local SQLite file.+ *+ * Schema management defaults to the main-owned migration chain (migrations-main), like the data+ * connection. The chain is idempotent, so a file an earlier release built with synchronize is+ * adopted in place on the first boot: existing tables are kept, missing columns are added, and the+ * migrations ledger is written. MAIN_DATABASE_SYNCHRONIZE=true opts back into synchronize, which+ * then runs instead of the migrations.+ */+export function mainConnectionOptions(configService: ConfigService): DataSourceOptions & { name: string } {+ const synchronize = configService.get<boolean>('database.synchronize', false);+ return {+ // Nest's TypeOrmCoreModule resolves the DataSource to close at shutdown from these options.+ name: 'main',+ type: 'better-sqlite3',+ database: configService.get<string>('database.database', './data/main.sqlite'),+ entities: [+ join(__dirname, '..', 'modules/auth/**/*.entity{.ts,.js}'),+ join(__dirname, '..', 'modules/audit/**/*.entity{.ts,.js}'),+ ],+ // Dedicated migrations dir for the main connection only (must NOT run the data-connection+ // migrations, which target session/webhook/message tables).+ migrations: [join(__dirname, 'migrations-main/*{.ts,.js}')],+ synchronize,+ migrationsRun: !synchronize,+ logging: configService.get<boolean>('database.logging', false),+ };+}diff --git a/src/database/migrations-main/1779900000000-CreateAuthAuditTables.ts b/src/database/migrations-main/1779900000000-CreateAuthAuditTables.tsindex 561f582a2..7ba30cf46 100644--- a/src/database/migrations-main/1779900000000-CreateAuthAuditTables.ts+++ b/src/database/migrations-main/1779900000000-CreateAuthAuditTables.ts@@ -6,9 +6,9 @@ import { MigrationInterface, QueryRunner } from 'typeorm';* `synchronize: true`, with no migrations — so turning synchronize off would leave a* fresh install with no `api_keys` table and total auth failure at boot.*- * This migration is what the `main` connection runs when `MAIN_DATABASE_SYNCHRONIZE=false`- * (`migrationsRun: !synchronize`). The main DB is always SQLite (boot config). `IF NOT EXISTS`- * makes it idempotent so it is also safe to adopt on a DB previously created by synchronize.+ * The `main` connection runs this chain at boot by default (`migrationsRun: !synchronize`; only+ * `MAIN_DATABASE_SYNCHRONIZE=true` opts out). The main DB is always SQLite (boot config). `IF NOT+ * EXISTS` makes it idempotent, so it adopts a DB an earlier release created by synchronize.*/export class CreateAuthAuditTables1779900000000 implements MigrationInterface {name = 'CreateAuthAuditTables1779900000000';diff --git a/src/database/migrations-main/1786610000000-DropSynchronizeIndexDuplicates.ts b/src/database/migrations-main/1786610000000-DropSynchronizeIndexDuplicates.tsnew file mode 100644index 000000000..3f64abd29--- /dev/null+++ b/src/database/migrations-main/1786610000000-DropSynchronizeIndexDuplicates.ts@@ -0,0 +1,41 @@+import { MigrationInterface, QueryRunner } from 'typeorm';++/**+ * Drops the indexes synchronize created under TypeORM's generated names on a main.sqlite built+ * before the main connection ran migrations by default.+ *+ * Adopting such a file runs CreateAuthAuditTables1779900000000, whose `CREATE INDEX IF NOT EXISTS`+ * adds the same indexes again under their migration names, leaving two indexes on api_keys.keyHash+ * and on four audit_logs columns: every audit insert maintained both. The migration-named ones+ * stay; on a file the chain built, none of these exist and each DROP is a no-op.+ *+ * CreateAuthAuditTables may already be recorded on a file whose indexes a later synchronize boot+ * replaced with generated names, so it will not run again. up() therefore creates the+ * migration-named indexes itself before dropping anything, so no column is left unindexed. The+ * unique one cannot fail on duplicate keys: the generated unique index still holds at that point.+ *+ * down() does not recreate them: they were only ever duplicates.+ */+export class DropSynchronizeIndexDuplicates1786610000000 implements MigrationInterface {+ name = 'DropSynchronizeIndexDuplicates1786610000000';++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_api_keys_keyHash" ON "api_keys" ("keyHash")`);+ for (const column of ['action', 'apiKeyId', 'sessionId', 'createdAt']) {+ await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_audit_logs_${column}" ON "audit_logs" ("${column}")`);+ }+ for (const index of [+ 'IDX_df3b25181df0b4b59bd93f16e1', // api_keys (keyHash), unique+ 'IDX_cee5459245f652b75eb2759b4c', // audit_logs (action)+ 'IDX_741fa976d1e04e695f3aa23cb8', // audit_logs (apiKeyId)+ 'IDX_dd2b6e43c767b6b5b2bb227ace', // audit_logs (sessionId)+ 'IDX_c69efb19bf127c97e6740ad530', // audit_logs (createdAt)+ ]) {+ await queryRunner.query(`DROP INDEX IF EXISTS "${index}"`);+ }+ }++ public async down(): Promise<void> {+ // Nothing to restore.+ }+}diff --git a/src/database/migrations-main/__tests__/adopt-existing-main-db.spec.ts b/src/database/migrations-main/__tests__/adopt-existing-main-db.spec.tsnew file mode 100644index 000000000..87338bdb1--- /dev/null+++ b/src/database/migrations-main/__tests__/adopt-existing-main-db.spec.ts@@ -0,0 +1,141 @@+import { mkdtempSync, readdirSync, rmSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';+import type { ConfigService } from '@nestjs/config';+import { DataSource } from 'typeorm';+import { ApiKey } from '../../../modules/auth/entities/api-key.entity';+import { AuditLog } from '../../../modules/audit/entities/audit-log.entity';+import { mainConnectionOptions } from '../../main-connection';++/**+ * The main connection runs its migration chain by default. Existing installs have a main.sqlite+ * built by synchronize and no migrations ledger, so the first boot must adopt that file in place:+ * keep every row, add the columns an older release lacked, record the chain, and leave one index+ * per indexed column.+ */+describe('main connection adopts an existing main.sqlite', () => {+ let dir: string;+ let file: string;++ beforeEach(() => {+ dir = mkdtempSync(join(tmpdir(), 'openwa-main-'));+ file = join(dir, 'main.sqlite');+ });+ afterEach(() => rmSync(dir, { recursive: true, force: true }));++ const SHIPPED = readdirSync(join(__dirname, '..'))+ .filter(name => name.endsWith('.ts'))+ .map(name => name.replace(/^(\d+)-(\w+)\.ts$/, '$2$1'))+ .sort();++ const config = (values: Record<string, unknown> = {}): ConfigService =>+ ({+ get: (key: string, fallback?: unknown) => (key in values ? values[key] : fallback),+ }) as unknown as ConfigService;++ /** Boot the file the way the app does (the current default configuration). */+ const boot = async (): Promise<DataSource> => {+ const ds = new DataSource(mainConnectionOptions(config({ 'database.database': file })));+ return ds.initialize();+ };++ /** A main.sqlite as an earlier release left it: built by synchronize, with a scoped key and an audit row. */+ const seedSynchronizeBuilt = async (withAllowedChats: boolean): Promise<void> => {+ const ds = await new DataSource({+ type: 'better-sqlite3',+ database: file,+ entities: [ApiKey, AuditLog],+ synchronize: true,+ }).initialize();+ await ds.query(+ `INSERT INTO api_keys (id, name, keyHash, keyPrefix, allowedSessions, allowedChats) ` ++ `VALUES ('k1', 'scoped', 'h1', 'owa_k1', 's1', '[email protected]')`,+ );+ await ds.query(`INSERT INTO audit_logs (id, action) VALUES ('a1', 'api_key_created')`);+ if (!withAllowedChats) await ds.query(`ALTER TABLE api_keys DROP COLUMN allowedChats`);+ await ds.destroy();+ };++ const ledger = async (ds: DataSource): Promise<string[]> =>+ (await ds.query<Array<{ name: string }>>(`SELECT name FROM migrations`)).map(r => r.name).sort();++ /** Indexed columns per table, one entry per index: a duplicate index shows up as a repeated entry. */+ const indexedColumns = async (ds: DataSource, table: string): Promise<string[]> => {+ const indexes = await ds.query<Array<{ name: string; origin: string }>>(`PRAGMA index_list("${table}")`);+ const columns: string[] = [];+ for (const index of indexes.filter(i => i.origin === 'c')) {+ const info = await ds.query<Array<{ name: string }>>(`PRAGMA index_info("${index.name}")`);+ columns.push(info.map(c => c.name).join(','));+ }+ return columns.sort();+ };++ const expectOneIndexPerColumn = async (ds: DataSource): Promise<void> => {+ expect(await indexedColumns(ds, 'api_keys')).toEqual(['keyHash']);+ expect(await indexedColumns(ds, 'audit_logs')).toEqual(['action', 'apiKeyId', 'createdAt', 'sessionId']);+ };++ it('defaults to the migration chain on a fresh file', async () => {+ const ds = await boot();+ try {+ expect(ds.options.synchronize).toBe(false);+ expect(await ledger(ds)).toEqual(SHIPPED);+ await ds+ .getRepository(ApiKey)+ .save({ id: 'k1', name: 'n', keyHash: 'h', keyPrefix: 'p', allowedChats: ['[email protected]'] });+ expect((await ds.getRepository(ApiKey).findOneByOrFail({ id: 'k1' })).allowedChats).toEqual(['[email protected]']);+ await expectOneIndexPerColumn(ds);+ } finally {+ await ds.destroy();+ }+ });++ it('adopts a synchronize-built file from the previous release without losing a row or a scope', async () => {+ await seedSynchronizeBuilt(true);++ const ds = await boot();+ try {+ expect(await ledger(ds)).toEqual(SHIPPED);+ const key = await ds.getRepository(ApiKey).findOneByOrFail({ id: 'k1' });+ expect(key.allowedSessions).toEqual(['s1']);+ expect(key.allowedChats).toEqual(['[email protected]']);+ expect(await ds.getRepository(AuditLog).count()).toBe(1);+ await expectOneIndexPerColumn(ds);+ } finally {+ await ds.destroy();+ }++ // A second boot has nothing left to run.+ const again = await boot();+ try {+ expect(await again.showMigrations()).toBe(false);+ } finally {+ await again.destroy();+ }+ });++ it('adds the chat-scope column to a file from a release that predates it', async () => {+ await seedSynchronizeBuilt(false);++ const ds = await boot();+ try {+ expect(await ledger(ds)).toEqual(SHIPPED);+ const key = await ds.getRepository(ApiKey).findOneByOrFail({ id: 'k1' });+ expect(key.allowedSessions).toEqual(['s1']);+ expect(key.allowedChats).toBeNull();+ await expectOneIndexPerColumn(ds);+ } finally {+ await ds.destroy();+ }+ });++ it('still synchronizes, without migrations, when MAIN_DATABASE_SYNCHRONIZE=true', () => {+ const options = mainConnectionOptions(config({ 'database.synchronize': true }));+ expect(options.synchronize).toBe(true);+ expect(options.migrationsRun).toBe(false);+ });++ it('keeps the connection name Nest resolves the main DataSource by at shutdown', () => {+ expect(mainConnectionOptions(config()).name).toBe('main');+ });+});diff --git a/src/main.ts b/src/main.tsindex 610ab538d..d4e0fb8cd 100644--- a/src/main.ts+++ b/src/main.ts@@ -22,6 +22,7 @@ import {isDashboardCspUpgradeTrapLikely,assertNoDefaultSecretsInProduction,isApiKeyPepperMissingInProduction,+ isMainDbSynchronizeInProduction,isNodeEnvUnset,} from './config/bootstrap-security';import { BullBoardAuthMiddleware } from './common/security/bull-board-auth.middleware';@@ -100,6 +101,15 @@ async function bootstrap() {);}+ // Advisory (not enforced): the main DB normally runs its migration chain; synchronize is an opt-in.+ if (isMainDbSynchronizeInProduction(process.env.NODE_ENV, process.env.MAIN_DATABASE_SYNCHRONIZE)) {+ bootstrapLogger.warn(+ 'MAIN_DATABASE_SYNCHRONIZE=true in production: the auth/audit database schema is rebuilt from the ' ++ "running release's entities instead of its migrations, so running an older release against it " ++ 'reshapes api_keys and audit_logs. Unset it to use the main migrations.',+ );+ }+// Advisory (not enforced): a TRUSTED_PROXIES entry that is not an IP or CIDR never matches, so the// proxy it meant to name is treated as a client. Failing the boot would break configs that run today.const badTrustedProxies = invalidTrustedProxies(process.env.TRUSTED_PROXIES);diff --git a/test/sqlite-chain-boot.e2e-spec.ts b/test/sqlite-chain-boot.e2e-spec.tsindex 67899f1a4..179362199 100644--- a/test/sqlite-chain-boot.e2e-spec.ts+++ b/test/sqlite-chain-boot.e2e-spec.ts@@ -12,9 +12,8 @@ process.env.RATE_LIMIT_SHORT_LIMIT = '100000';process.env.RATE_LIMIT_MEDIUM_LIMIT = '100000';process.env.RATE_LIMIT_LONG_LIMIT = '100000';process.env.DATABASE_SYNCHRONIZE = 'false';-// The main connection defaults synchronize to true when the env is merely absent (configuration.ts-// maps !== 'false'), so deleting the env would silently boot main through synchronize. Set it to-// 'false' explicitly: BOTH connections must run their migration chains.+// The main connection already runs its chain when the env is absent; 'false' is set explicitly so a+// value leaking in from the shell cannot boot main through synchronize. BOTH chains must run.process.env.MAIN_DATABASE_SYNCHRONIZE = 'false';// Fresh throwaway files per run; the chain must CREATE everything that exists in them.import { existsSync, readFileSync, rmSync } from 'fs';