Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): keep a SQLite file path out of the PostgreSQL database name
Yudhi Armyndharis70beb439b250
diff --git a/src/modules/infra/config-sections.spec.ts b/src/modules/infra/config-sections.spec.tsindex 931d93ca2..392f9dd32 100644--- a/src/modules/infra/config-sections.spec.ts+++ b/src/modules/infra/config-sections.spec.ts@@ -412,3 +412,24 @@ describe('Redis username ownership', () => {expect(savedValues(existing, ctx).REDIS_USERNAME).toBe('worker');});});++describe('PostgreSQL database name', () => {+ const external = { host: 'db.example.invalid', username: 'app', password: 'DummyPassword123456' };++ it.each([['sqlite'], [undefined]])('does not keep a SQLite file path as the name (stored type %s)', type => {+ const existing = { ...(type ? { DATABASE_TYPE: type } : {}), 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();+ applyDatabaseSection({ type: 'postgres', builtIn: false, ...external }, existing, kept);+ expect(savedValues(existing, kept).DATABASE_NAME).toBe('appdb');+ const sent = context();+ applyDatabaseSection({ type: 'postgres', builtIn: false, ...external, database: 'other' }, existing, sent);+ expect(savedValues(existing, sent).DATABASE_NAME).toBe('other');+ });+});diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.tsindex 3e6f66b27..35de7eef9 100644--- a/src/modules/infra/config-sections.ts+++ b/src/modules/infra/config-sections.ts@@ -134,6 +134,8 @@ export function applyDatabaseSection(if (database.username !== undefined) updates.DATABASE_USERNAME = database.username.trim() || 'postgres';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');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;diff --git a/src/modules/infra/infra-config.controller.ts b/src/modules/infra/infra-config.controller.tsindex 129781b80..7ea8e218d 100644--- a/src/modules/infra/infra-config.controller.ts+++ b/src/modules/infra/infra-config.controller.ts@@ -130,14 +130,16 @@ export class InfraConfigController {// Secrets (passwords, S3 keys) are never returned; the form shows a "set" indicator// and an empty submission preserves the stored value (see saveConfig). This lets// the dashboard hydrate the form so a save no longer overwrites unseen fields (#226).+ const databaseType = effective('DATABASE_TYPE') === 'postgres' ? 'postgres' : 'sqlite';return {database: {- type: effective('DATABASE_TYPE') === 'postgres' ? 'postgres' : 'sqlite',+ type: databaseType,...savedBuiltInMode('database', saved),host: effective('DATABASE_HOST') || '',port: effective('DATABASE_PORT') || '',username: effective('DATABASE_USERNAME') || '',- database: effective('DATABASE_NAME') || '',+ // The form sends this back for a PostgreSQL save; under SQLite the key holds a file path, not a name.+ database: databaseType === 'postgres' ? effective('DATABASE_NAME') || '' : '',schema: effective('POSTGRES_SCHEMA') || 'public',poolSize: Number(effective('DATABASE_POOL_SIZE')) || 10,sslEnabled: effective('DATABASE_SSL') === 'true',diff --git a/src/modules/infra/infra-config.persistence.spec.ts b/src/modules/infra/infra-config.persistence.spec.tsindex 62d10dde0..2aa627159 100644--- a/src/modules/infra/infra-config.persistence.spec.ts+++ b/src/modules/infra/infra-config.persistence.spec.ts@@ -94,6 +94,34 @@ it.each([true, false])('preserves the SQLite file path with an explicit type %s'expect(dotenv.parse(actualFs.readFileSync(file)).DATABASE_NAME).toBe('./data/customer.sqlite');});+describe('a SQLite file path in DATABASE_NAME', () => {+ const stored = { DATABASE_NAME: './data/customer.sqlite' };+ const postgres = { database: { type: 'postgres' as const, host: 'db.example.invalid', username: 'app' } };+ const passwordless = { ...postgres, database: { ...postgres.database, password: 'DummyPassword123456' } };++ it.each([+ ['sqlite', ''],+ ['postgres', 'appdb'],+ ])('is reported to the form as a name only under %s', (type, name) => {+ seed({ ...stored, DATABASE_TYPE: type, ...(type === 'postgres' ? { ...database, DATABASE_NAME: 'appdb' } : {}) });+ expect(build().controller.getConfig().database.database).toBe(name);+ });++ it('does not become the PostgreSQL database name on a direct switch', async () => {+ seed(stored);+ expect((await save(passwordless)).saved).toBe(true);+ expect(dotenv.parse(actualFs.readFileSync(file))).not.toHaveProperty('DATABASE_NAME');+ });++ it('does not become the PostgreSQL database name after a SQLite save', async () => {+ seed(stored);+ expect((await save({ database: { type: 'sqlite' } })).saved).toBe(true);+ expect(dotenv.parse(actualFs.readFileSync(file)).DATABASE_NAME).toBe('./data/customer.sqlite');+ expect((await save(passwordless)).saved).toBe(true);+ expect(dotenv.parse(actualFs.readFileSync(file))).not.toHaveProperty('DATABASE_NAME');+ });+});+it('drops the PostgreSQL database name when leaving PostgreSQL for SQLite', () => {seed({ DATABASE_TYPE: 'postgres', DATABASE_HOST: 'db.example.invalid', DATABASE_NAME: 'openwa' });const result = build().controller.saveConfig({ database: { type: 'sqlite' } });