From dd5e7e5712bb10043d8716145265d8a1a4f1a5f2 Mon Sep 17 00:00:00 2001 From: Rafal Hawrylak Date: Sun, 18 Oct 2026 07:16:50 +0000 Subject: [PATCH] fix(cli): filter out non-existent tables in BigQueryDbAdapter.search --- cli/api/dbadapters/bigquery.ts | 3 +- cli/api/dbadapters/bigquery_test.ts | 59 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/cli/api/dbadapters/bigquery.ts b/cli/api/dbadapters/bigquery.ts index 38ff5079a..256a61dff 100644 --- a/cli/api/dbadapters/bigquery.ts +++ b/cli/api/dbadapters/bigquery.ts @@ -318,7 +318,7 @@ export class BigQueryDbAdapter implements IDbAdapter { rowLimit: options.limit, }, ); - return await Promise.all( + const tables = await Promise.all( results.rows.map((row) => this.table({ database: row.table_catalog, @@ -327,6 +327,7 @@ export class BigQueryDbAdapter implements IDbAdapter { }), ), ); + return tables.filter((table) => Boolean(table)); } public async table(target: dataform.ITarget): Promise { diff --git a/cli/api/dbadapters/bigquery_test.ts b/cli/api/dbadapters/bigquery_test.ts index fdf6dc8f8..ff7425b4c 100644 --- a/cli/api/dbadapters/bigquery_test.ts +++ b/cli/api/dbadapters/bigquery_test.ts @@ -1,5 +1,6 @@ import { Dataset, Table } from "@google-cloud/bigquery"; import { expect } from "chai"; +import { EventEmitter } from "events"; import { AuthClient, gaxios, Impersonated } from "google-auth-library"; import { anything, instance, mock, when } from "ts-mockito"; @@ -319,4 +320,62 @@ suite("BigQueryDbAdapter", () => { await adapter.setMetadata(action); }); + + test("search() filters out tables whose metadata is not found", async () => { + const projectId = "project1"; + const liveSchema = "live_schema"; + const deletedSchema = "deleted_schema"; + const tableName = "table1"; + + const mockBigQuery: any = { + createQueryStream: () => { + const stream = new EventEmitter(); + process.nextTick(() => { + stream.emit("data", { + table_catalog: projectId, + table_schema: liveSchema, + table_name: tableName, + }); + stream.emit("data", { + table_catalog: projectId, + table_schema: deletedSchema, + table_name: tableName, + }); + stream.emit("end"); + }); + return stream; + }, + dataset: (schema: string) => ({ + table: (name: string) => ({ + getMetadata: () => { + if (schema === deletedSchema) { + return Promise.reject({ errors: [{ reason: "notFound" }] }); + } + return Promise.resolve([ + { + type: "TABLE", + tableReference: { projectId, datasetId: schema, tableId: name }, + schema: { fields: [{ name: "col1", type: "STRING" }] }, + lastModifiedTime: "123456789", + }, + ]); + }, + }), + }), + }; + + const credentials = dataform.BigQuery.create({ projectId, location: "US" }); + const adapter = new BigQueryDbAdapter(credentials, { + clientProvider: () => mockBigQuery, + }); + + const result = await adapter.search("schema"); + + expect(result.length).to.equal(1); + expect(result[0].target).to.eql({ + database: projectId, + schema: liveSchema, + name: tableName, + }); + }); });