Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(database): probe the messages author column without getTable

Yudhi Armyndharis55515717f8df
diff --git a/src/database/migrations/1784908800000-AddMessageAuthor.ts b/src/database/migrations/1784908800000-AddMessageAuthor.ts
index a15990b68..45f3d44ea 100644
--- a/src/database/migrations/1784908800000-AddMessageAuthor.ts
+++ b/src/database/migrations/1784908800000-AddMessageAuthor.ts
@@ -8,22 +8,37 @@ import { MigrationInterface, QueryRunner } from 'typeorm';
*
* Hand-authored because `synchronize` is off for the `data` connection on PostgreSQL (and optional
* on SQLite via DATABASE_SYNCHRONIZE=false). Idempotent: checks for column existence first.
+ *
+ * NOTE: the existence check deliberately avoids `queryRunner.getTable('messages')`. Since the FTS
+ * migration added the STORED generated column `body_ts`, loading the `messages` table metadata on
+ * Postgres makes TypeORM look the expression up in `typeorm_metadata` — a table nothing in this
+ * migration context creates (the schema builder only creates it for entity-declared generated
+ * columns), so the lookup fails with "relation typeorm_metadata does not exist". A raw
+ * dialect-aware probe sidesteps that path entirely.
*/
export class AddMessageAuthor1784908800000 implements MigrationInterface {
name = 'AddMessageAuthor1784908800000';
+ private async hasAuthorColumn(queryRunner: QueryRunner): Promise<boolean> {
+ if (queryRunner.connection.options.type === 'postgres') {
+ const rows: unknown[] = await queryRunner.query(
+ `SELECT 1 FROM information_schema.columns
+ WHERE table_schema = current_schema() AND table_name = 'messages' AND column_name = 'author'`,
+ );
+ return rows.length > 0;
+ }
+ const rows: Array<{ name: string }> = await queryRunner.query(`PRAGMA table_info("messages")`);
+ return rows.some(r => r.name === 'author');
+ }
+
public async up(queryRunner: QueryRunner): Promise<void> {
- const table = await queryRunner.getTable('messages');
- const col = table?.findColumnByName('author');
- if (col) return; // already added by synchronize or a previous run
+ if (await this.hasAuthorColumn(queryRunner)) return; // already added by synchronize or a previous run
await queryRunner.query(`ALTER TABLE "messages" ADD COLUMN "author" varchar NULL`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
- const table = await queryRunner.getTable('messages');
- const col = table?.findColumnByName('author');
- if (!col) return;
+ if (!(await this.hasAuthorColumn(queryRunner))) return;
await queryRunner.query(`ALTER TABLE "messages" DROP COLUMN "author"`);
}
}