Skip to content

Commit 87060b2

Browse files
authored
feat(firestore-bigquery-export): add bigquery-project-id param to gen-schema-view script (#1999)
1 parent ca9ca2d commit 87060b2

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

firestore-bigquery-export/scripts/gen-schema-view/src/index.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { readSchemas } from "./schema-loader-utils";
2626

2727
const BIGQUERY_VALID_CHARACTERS = /^[a-zA-Z0-9_]+$/;
2828
const FIRESTORE_VALID_CHARACTERS = /^[^\/]+$/;
29+
const GCP_PROJECT_VALID_CHARACTERS = /^[a-z][a-z0-9-]{0,29}$/;
2930

3031
const validateInput = (value: any, name: string, regex: RegExp) => {
3132
if (!value || value === "" || value.trim() === "") {
@@ -58,7 +59,7 @@ program
5859
)
5960
.option(
6061
"-B, --big-query-project <big-query-project>",
61-
"Google Cloud Project ID for BigQuery."
62+
"Google Cloud Project ID for BigQuery (can be the same as the Firebase project ID)."
6263
)
6364
.option(
6465
"-d, --dataset <dataset>",
@@ -79,10 +80,20 @@ const questions = [
7980
{
8081
message: "What is your Firebase project ID?",
8182
name: "project",
83+
default: process.env.PROJECT_ID,
8284
type: "input",
8385
validate: (value) =>
8486
validateInput(value, "project ID", FIRESTORE_VALID_CHARACTERS),
8587
},
88+
{
89+
message:
90+
"What is your Google Cloud Project ID for BigQuery? (can be the same as the Firebase project ID)",
91+
name: "bigQueryProject",
92+
default: process.env.PROJECT_ID,
93+
type: "input",
94+
validate: (value) =>
95+
validateInput(value, "BigQuery project ID", GCP_PROJECT_VALID_CHARACTERS),
96+
},
8697
{
8798
message:
8899
"What is the ID of the BigQuery dataset the raw changelog lives in? (The dataset and the raw changelog must already exist!)",
@@ -109,6 +120,7 @@ const questions = [
109120

110121
interface CliConfig {
111122
projectId: string;
123+
bigQueryProjectId: string;
112124
datasetId: string;
113125
tableNamePrefix: string;
114126
schemas: { [schemaName: string]: FirestoreSchema };
@@ -121,7 +133,7 @@ async function run(): Promise<number> {
121133
// Set project ID so it can be used in BigQuery intialization
122134
process.env.PROJECT_ID = config.projectId;
123135
// BigQuery actually requires this variable to set the project correctly.
124-
process.env.GOOGLE_CLOUD_PROJECT = config.projectId;
136+
process.env.GOOGLE_CLOUD_PROJECT = config.bigQueryProjectId;
125137

126138
// Initialize Firebase
127139
if (!firebase.apps.length) {
@@ -135,7 +147,9 @@ async function run(): Promise<number> {
135147
if (Object.keys(config.schemas).length === 0) {
136148
console.log(`No schema files found!`);
137149
}
138-
const viewFactory = new FirestoreBigQuerySchemaViewFactory();
150+
const viewFactory = new FirestoreBigQuerySchemaViewFactory(
151+
config.bigQueryProjectId
152+
);
139153
for (const schemaName in config.schemas) {
140154
await viewFactory.initializeSchemaViewResources(
141155
config.datasetId,
@@ -152,6 +166,7 @@ async function parseConfig(): Promise<CliConfig> {
152166
if (program.nonInteractive) {
153167
if (
154168
program.project === undefined ||
169+
program.bigQueryProject === undefined ||
155170
program.dataset === undefined ||
156171
program.tableNamePrefix === undefined ||
157172
program.schemaFiles.length === 0
@@ -161,15 +176,18 @@ async function parseConfig(): Promise<CliConfig> {
161176
}
162177
return {
163178
projectId: program.project,
179+
bigQueryProjectId: program.bigQueryProject,
164180
datasetId: program.dataset,
165181
tableNamePrefix: program.tableNamePrefix,
166182
schemas: readSchemas(program.schemaFiles),
167183
};
168184
}
169-
const { project, dataset, tableNamePrefix, schemaFiles } =
185+
const { project, bigQueryProject, dataset, tableNamePrefix, schemaFiles } =
170186
await inquirer.prompt(questions);
187+
171188
return {
172189
projectId: project,
190+
bigQueryProjectId: bigQueryProject,
173191
datasetId: dataset,
174192
tableNamePrefix: tableNamePrefix,
175193
schemas: readSchemas(

firestore-bigquery-export/scripts/gen-schema-view/src/schema/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ export const firestoreToBigQueryFieldType: {
8787
export class FirestoreBigQuerySchemaViewFactory {
8888
bq: bigquery.BigQuery;
8989

90-
constructor() {
91-
this.bq = new bigquery.BigQuery();
90+
constructor(bqProjectId: string) {
91+
this.bq = new bigquery.BigQuery({ projectId: bqProjectId });
9292
}
9393

9494
/**

0 commit comments

Comments
 (0)