Skip to content

Commit d8eb24f

Browse files
committed
fix(firestore-bigquery-export): fixed timestamp as a fieldname partitioning
1 parent b3b844d commit d8eb24f

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

firestore-bigquery-export/firestore-bigquery-change-tracker/src/__tests__/bigquery/e2e.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,41 @@ describe("e2e", () => {
267267
);
268268
});
269269

270+
test("successfully partitions with a valid Firebase Timestamp value with a timestamp field and Timestamp type", async () => {
271+
const created = firestore.Timestamp.now();
272+
const expectedDate = created.toDate().toISOString().substring(0, 22);
273+
274+
const event: FirestoreDocumentChangeEvent = changeTrackerEvent({
275+
data: { created },
276+
});
277+
278+
await changeTracker({
279+
datasetId,
280+
tableId,
281+
timePartitioning: "DAY",
282+
timePartitioningField: "timestamp",
283+
timePartitioningFieldType: "TIMESTAMP",
284+
timePartitioningFirestoreField: "created",
285+
}).record([event]);
286+
287+
const [metadata] = await dataset.table(`${tableId_raw}`).getMetadata();
288+
289+
const [changeLogRows] = await getBigQueryTableData(
290+
process.env.PROJECT_ID,
291+
datasetId,
292+
tableId
293+
);
294+
295+
expect(metadata.timePartitioning).toBeDefined();
296+
expect(metadata.timePartitioning.type).toEqual("DAY");
297+
expect(metadata.timePartitioning.field).toEqual("timestamp");
298+
299+
//TODO: check data has been added successfully
300+
expect(changeLogRows[0].timestamp.value).toBe(
301+
BigQuery.timestamp(created.toDate()).value
302+
);
303+
});
304+
270305
test("old_data is null if is not provided", async () => {
271306
const event: FirestoreDocumentChangeEvent = changeTrackerEvent({
272307
data: { foo: "foo" },

firestore-bigquery-export/firestore-bigquery-change-tracker/src/bigquery/partitioning.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,41 +239,48 @@ export class Partitioning {
239239
}
240240

241241
customFieldExists(fields = []) {
242-
if (!fields.length) return false;
243-
242+
/** Extract the time partioning field name */
244243
const { timePartitioningField } = this.config;
245244

245+
/** Return based the field already exist */
246246
return fields.map(($) => $.name).includes(timePartitioningField);
247247
}
248248

249-
private async shouldAddPartitioningToSchema(): Promise<{
249+
private async shouldAddPartitioningToSchema(fields: string[]): Promise<{
250250
proceed: boolean;
251251
message: string;
252252
}> {
253253
if (!this.isPartitioningEnabled()) {
254254
return { proceed: false, message: "Partitioning not enabled" };
255255
}
256+
256257
if (!this.hasValidTableReference()) {
257258
return { proceed: false, message: "Invalid table reference" };
258259
}
260+
259261
if (!this.hasValidCustomPartitionConfig()) {
260262
return { proceed: false, message: "Invalid partition config" };
261263
}
264+
262265
if (!this.hasValidTimePartitionType()) {
263266
return { proceed: false, message: "Invalid partition type" };
264267
}
268+
265269
if (!this.hasValidTimePartitionOption()) {
266270
return { proceed: false, message: "Invalid partition option" };
267271
}
272+
268273
if (this.hasHourAndDatePartitionConfig()) {
269274
return {
270275
proceed: false,
271276
message: "Invalid partitioning and field type combination",
272277
};
273278
}
274-
if (this.customFieldExists()) {
279+
280+
if (this.customFieldExists(fields)) {
275281
return { proceed: false, message: "Field already exists on schema" };
276282
}
283+
277284
if (await this.isTablePartitioned()) {
278285
return { proceed: false, message: "Table is already partitioned" };
279286
}
@@ -284,7 +291,10 @@ export class Partitioning {
284291
}
285292

286293
async addPartitioningToSchema(fields = []): Promise<void> {
287-
const { proceed, message } = await this.shouldAddPartitioningToSchema();
294+
const { proceed, message } = await this.shouldAddPartitioningToSchema(
295+
fields
296+
);
297+
288298
if (!proceed) {
289299
functions.logger.warn(`Did not add partitioning to schema: ${message}`);
290300
return;

0 commit comments

Comments
 (0)