Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(config): catch MAIN_DATABASE_NAME set to the data DB's default file

Yudhi Armyndharis7871a115e808
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index c88d19faa..7b68a8d43 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -428,6 +428,14 @@ describe('validateEnv', () => {
).not.toThrow();
});
+ it("catches MAIN_DATABASE_NAME pointing at the data connection's default file", () => {
+ // DATABASE_NAME unset resolves to ./data/openwa.sqlite at runtime, so the guard must compare that.
+ expect(() => validateEnv({ MAIN_DATABASE_NAME: './data/openwa.sqlite' })).toThrow(/\.\/data\/openwa\.sqlite/);
+ expect(() => validateEnv({ DATABASE_TYPE: 'sqlite', MAIN_DATABASE_NAME: './data/../data/openwa.sqlite' })).toThrow(
+ /main database file/,
+ );
+ });
+
it('rejects DATABASE_SYNCHRONIZE=true with DATABASE_TYPE=postgres (drops body_ts → /search 501)', () => {
// The Postgres data connection hardcodes migrationsRun=true; an opted-in synchronize=true makes
// TypeORM re-sync from entities on every boot, dropping the migration-created `body_ts` generated
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 9944f6a54..79c3c95f5 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -8,6 +8,8 @@ type EnvConfig = Record<string, unknown>;
// the main DB and DATABASE_NAME follows it, and false-positive when the main DB moved away but
// DATABASE_NAME still points at the (now unused) default file.
const MAIN_DB_DEFAULT_PATH = './data/main.sqlite';
+// Default SQLite file of the 'data' connection (configuration.ts / data-source.ts), for the same reason.
+const DATA_DB_DEFAULT_PATH = './data/openwa.sqlite';
// Duplicated rather than imported from configuration.ts (see MAIN_DB_DEFAULT_PATH above); the spec
// asserts the two agree.
@@ -32,11 +34,10 @@ export function sqliteDataMainPathCollision(config: EnvConfig): string | null {
// Postgres uses a bare database NAME, never a file path — no collision is possible there.
const dbType = read('DATABASE_TYPE');
if (dbType !== undefined && dbType !== 'sqlite') return null;
- const dataDbName = read('DATABASE_NAME');
- if (!dataDbName) return null;
+ const dataDbName = read('DATABASE_NAME') || DATA_DB_DEFAULT_PATH;
const mainDbPath = read('MAIN_DATABASE_NAME') || MAIN_DB_DEFAULT_PATH;
if (resolve(dataDbName) === resolve(mainDbPath)) {
- return `DATABASE_NAME must not point at the main database file (${mainDbPath}); use a separate file`;
+ return `DATABASE_NAME (${dataDbName}) must not point at the main database file (${mainDbPath}); use a separate file`;
}
return null;
}