Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): keep a stored database name under a pinned PostgreSQL type

Yudhi Armyndharisa122fd6b4dab
diff --git a/src/modules/infra/config-sections.spec.ts b/src/modules/infra/config-sections.spec.ts
index 392f9dd32..399a8bb79 100644
--- a/src/modules/infra/config-sections.spec.ts
+++ b/src/modules/infra/config-sections.spec.ts
@@ -423,6 +423,28 @@ describe('PostgreSQL database name', () => {
expect(savedValues(existing, ctx)).not.toHaveProperty('DATABASE_NAME');
});
+ it.each([
+ ['the host environment', true],
+ ['the project .env', false],
+ ])('keeps the stored name when %s alone sets the type to postgres', (_origin, host) => {
+ process.env.DATABASE_TYPE = 'postgres';
+ recordOsEnvKeys(host ? { DATABASE_TYPE: 'postgres' } : {});
+ recordPinnedEnvKeys({ DATABASE_TYPE: 'postgres' });
+ const existing = { DATABASE_NAME: 'appdb', DATABASE_HOST: 'db.example.invalid' };
+ const ctx = context();
+ applyDatabaseSection({ builtIn: false, ...external }, existing, ctx);
+ expect(savedValues(existing, ctx).DATABASE_NAME).toBe('appdb');
+ });
+
+ it('still drops a SQLite file path when the environment pins the type to sqlite', () => {
+ process.env.DATABASE_TYPE = 'sqlite';
+ recordPinnedEnvKeys({ DATABASE_TYPE: 'sqlite' });
+ const existing = { DATABASE_NAME: './data/customer.sqlite' };
+ const ctx = context();
+ applyDatabaseSection({ type: 'postgres', builtIn: false, ...external }, existing, ctx);
+ expect(savedValues(existing, ctx)).not.toHaveProperty('DATABASE_NAME');
+ });
+
it('keeps the stored name of a PostgreSQL configuration and writes one that is sent', () => {
const existing = { DATABASE_TYPE: 'postgres', DATABASE_NAME: 'appdb' };
const kept = context();
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index 35de7eef9..744b674bd 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -135,7 +135,10 @@ export function applyDatabaseSection(
setSecret(updates, 'DATABASE_PASSWORD', database.password);
if (database.database !== undefined) updates.DATABASE_NAME = database.database || 'openwa';
// Left from SQLite, the key holds a file path, which the next boot would open as a PostgreSQL database name.
- else if (existing.DATABASE_TYPE !== 'postgres') staleKeys.add('DATABASE_NAME');
+ // A file with no type beside a PostgreSQL pin holds a name the pinned server uses, so that one stays.
+ else if (existing.DATABASE_TYPE !== 'postgres' && nextBootValue('DATABASE_TYPE', existing) !== 'postgres') {
+ staleKeys.add('DATABASE_NAME');
+ }
if (database.schema !== undefined) {
// Refuse a schema the next boot would reject, or the saved file crash-loops the instance.
const schemaError = database.schema ? postgresSchemaError(database.schema) : null;