Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

chore(db): scope the data migration CLI to data-owned entities (#391)

Yudhi Armyndharis43ef9498f00e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 132364483..c39855492 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
GitHub Release job now depends on the Docker image job, so a `v*` tag can no longer publish release
notes without a matching multi-arch image on GHCR. A failed image build leaves the tag without a
Release until the workflow is re-run. (#389)
+- **The data migration CLI is scoped to the data-owned tables.** `data-source.ts` (used by
+ `migration:generate` / `migration:run`) now lists only the data connection's entities
+ (session/webhook/message/template/engine), mirroring the runtime data connection, instead of a
+ broad glob that also pulled in the main-owned `api_keys`/`audit_logs` entities. Generating a data
+ migration no longer emits spurious auth/audit DDL into the data database. No runtime or schema
+ change for existing installs. (#391)
### Fixed
diff --git a/src/database/data-source.spec.ts b/src/database/data-source.spec.ts
new file mode 100644
index 000000000..b145edf46
--- /dev/null
+++ b/src/database/data-source.spec.ts
@@ -0,0 +1,34 @@
+import { globSync } from 'glob';
+import dataDataSource from './data-source';
+
+// The data CLI DataSource manages the DATA connection's migrations (session/webhook/message/
+// template/engine). It must NOT pull in the auth/audit entities — those belong to the always-SQLite
+// MAIN connection (data-source-main.ts). A broad '**' entity glob would sweep the main-owned
+// entities into `migration:generate` against the data DB and emit spurious auth/audit DDL.
+describe('data CLI DataSource', () => {
+ const resolveEntityFiles = (): string[] =>
+ (dataDataSource.options.entities as string[])
+ .flatMap(pattern => globSync(pattern))
+ .map(file => file.replace(/\\/g, '/'));
+
+ it('resolves the data-owned entities (session, webhook, message, template, engine)', () => {
+ const files = resolveEntityFiles();
+ expect(files.some(f => f.endsWith('session.entity.ts'))).toBe(true);
+ expect(files.some(f => f.endsWith('webhook.entity.ts'))).toBe(true);
+ expect(files.some(f => f.endsWith('message.entity.ts'))).toBe(true);
+ expect(files.some(f => f.endsWith('template.entity.ts'))).toBe(true);
+ expect(files.some(f => f.endsWith('lid-mapping.entity.ts'))).toBe(true);
+ });
+
+ it('never resolves the main-owned api-key/audit-log entities', () => {
+ const files = resolveEntityFiles();
+ expect(files.some(f => f.endsWith('api-key.entity.ts'))).toBe(false);
+ expect(files.some(f => f.endsWith('audit-log.entity.ts'))).toBe(false);
+ });
+
+ it('does not use a catch-all entity glob (guards against re-broadening)', () => {
+ for (const pattern of dataDataSource.options.entities as string[]) {
+ expect(pattern).not.toMatch(/\/\.\.\/\*\*\/\*\.entity/);
+ }
+ });
+});
diff --git a/src/database/data-source.ts b/src/database/data-source.ts
index b1715a5d8..f5b6cadb3 100644
--- a/src/database/data-source.ts
+++ b/src/database/data-source.ts
@@ -10,7 +10,16 @@ const dbType = process.env.DATABASE_TYPE || 'sqlite';
const sqliteDataSource = new DataSource({
type: 'sqlite',
database: process.env.DATABASE_NAME || './data/openwa.sqlite',
- entities: [__dirname + '/../**/*.entity{.ts,.js}'],
+ // Scoped to the DATA-owned modules only (session/webhook/message/template/engine), mirroring the
+ // runtime data connection (app.module.ts). A broad '**' glob would also sweep in the main-owned
+ // auth/audit entities and pollute `migration:generate` against the data DB with their DDL.
+ entities: [
+ __dirname + '/../modules/session/**/*.entity{.ts,.js}',
+ __dirname + '/../modules/webhook/**/*.entity{.ts,.js}',
+ __dirname + '/../modules/message/**/*.entity{.ts,.js}',
+ __dirname + '/../modules/template/**/*.entity{.ts,.js}',
+ __dirname + '/../engine/**/*.entity{.ts,.js}',
+ ],
migrations: [__dirname + '/migrations/*{.ts,.js}'],
synchronize: false,
logging: process.env.DATABASE_LOGGING === 'true',
@@ -24,7 +33,16 @@ const postgresDataSource = new DataSource({
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME || 'openwa',
- entities: [__dirname + '/../**/*.entity{.ts,.js}'],
+ // Scoped to the DATA-owned modules only (session/webhook/message/template/engine), mirroring the
+ // runtime data connection (app.module.ts). A broad '**' glob would also sweep in the main-owned
+ // auth/audit entities and pollute `migration:generate` against the data DB with their DDL.
+ entities: [
+ __dirname + '/../modules/session/**/*.entity{.ts,.js}',
+ __dirname + '/../modules/webhook/**/*.entity{.ts,.js}',
+ __dirname + '/../modules/message/**/*.entity{.ts,.js}',
+ __dirname + '/../modules/template/**/*.entity{.ts,.js}',
+ __dirname + '/../engine/**/*.entity{.ts,.js}',
+ ],
migrations: [__dirname + '/migrations/*{.ts,.js}'],
synchronize: false, // Never auto-sync in production
logging: process.env.DATABASE_LOGGING === 'true',