Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { resolveGcpProjectIdForBigQuery } from "../../bigquery/gcpProject";

const ENV_VARS = ["PROJECT_ID", "GOOGLE_CLOUD_PROJECT"];

function clearEnv() {
for (const v of ENV_VARS) delete process.env[v];
}

beforeEach(clearEnv);
afterEach(clearEnv);

describe("resolveGcpProjectIdForBigQuery", () => {
it("returns the explicit preferred value first", () => {
process.env.GOOGLE_CLOUD_PROJECT = "env-project";
expect(resolveGcpProjectIdForBigQuery("explicit-project")).toBe(
"explicit-project"
);
});

it("prefers GOOGLE_CLOUD_PROJECT over PROJECT_ID (Gen2 / Cloud Run)", () => {
process.env.GOOGLE_CLOUD_PROJECT = "gen2-project";
process.env.PROJECT_ID = "gen1-project";
expect(resolveGcpProjectIdForBigQuery(undefined)).toBe("gen2-project");
});

it("falls back to PROJECT_ID when GOOGLE_CLOUD_PROJECT is absent", () => {
process.env.PROJECT_ID = "gen1-project";
expect(resolveGcpProjectIdForBigQuery(undefined)).toBe("gen1-project");
});

it("returns undefined when no source is available", () => {
expect(resolveGcpProjectIdForBigQuery(undefined)).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export function resolveGcpProjectIdForBigQuery(
preferred?: string
): string | undefined {
return (
preferred ||
process.env.GOOGLE_CLOUD_PROJECT ||
process.env.PROJECT_ID
);
Comment thread
davestimpert marked this conversation as resolved.
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { tableRequiresUpdate } from "./checkUpdates";
import { parseErrorMessage, waitForInitialization } from "./utils";
import { initializeLatestView } from "./initializeLatestView";
import { logger, LogLevel } from "../logger";
import { resolveGcpProjectIdForBigQuery } from "./gcpProject";

export { RawChangelogSchema, RawChangelogViewSchema } from "./schema";
import type { ChangeTrackerConfig } from "./types";
Expand All @@ -68,7 +69,7 @@ export class FirestoreBigQueryEventHistoryTracker
constructor(public config: ChangeTrackerConfig) {
this.bq = new bigquery.BigQuery();

this.bq.projectId = config.bqProjectId || process.env.PROJECT_ID;
this.bq.projectId = resolveGcpProjectIdForBigQuery(config.bqProjectId);

this.partitioningConfig = new PartitioningConfig(this.config.partitioning);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as sqlFormatter from "sql-formatter";
import { timestampField } from "./schema";
import { resolveGcpProjectIdForBigQuery } from "./gcpProject";

const excludeFields: string[] = ["document_name", "document_id"];
const nonGroupFields = ["event_id", "data", "old_data"];
Expand Down Expand Up @@ -65,7 +66,12 @@ export function buildLatestSnapshotViewQuery({
}: BuildLatestSnapshotViewQueryOptions): string {
validateInputs({ datasetId, tableName, timestampColumnName, groupByColumns });

const projectId = bqProjectId || process.env.PROJECT_ID;
const projectId = resolveGcpProjectIdForBigQuery(bqProjectId);
Comment thread
davestimpert marked this conversation as resolved.
if (!projectId) {
throw new Error(
"Could not determine BigQuery project ID. Provide it via the bqProjectId config option or set the GOOGLE_CLOUD_PROJECT / PROJECT_ID environment variable."
);
}

return useLegacyQuery
? buildLegacyQuery(
Expand Down