Skip to content

Commit 505f8af

Browse files
authored
Merge pull request #331 from FirebasePrivate/fz/big-query-timestamp-micro-precision
Change timestamp to a string to preserve microsecond precision
2 parents e895637 + 69d0fc4 commit 505f8af

File tree

7 files changed

+11
-47
lines changed

7 files changed

+11
-47
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ export class FirestoreBigQueryEventHistoryTracker implements FirestoreEventHisto
8787
buildDataRow(
8888
eventId: string,
8989
changeType: ChangeType,
90-
timestamp: Date,
90+
timestamp: string,
9191
document_name: string,
9292
data?: Object
9393
): bigquery.RowMetadata {
9494
// This must match firestoreToBQTable().
9595
return {
96-
timestamp: timestamp.toISOString(),
96+
timestamp: timestamp,
9797
event_id: eventId,
9898
document_name: document_name,
9999
operation: ChangeType[changeType],

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ export enum ChangeType {
2525
}
2626

2727
export interface FirestoreDocumentChangeEvent {
28-
timestamp: Date;
28+
// The timestamp represented in ISO format.
29+
// Date is not appropriate because it only has millisecond precision.
30+
// Cloud Firestore timestamps have microsecond precision.
31+
timestamp: string;
2932
operation: ChangeType;
3033
documentName: string;
3134
eventId: string;

firestore-bigquery-export/functions/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exports.fsexportbigquery = functions.handler.firestore.document.onWrite((change,
3636
try {
3737
const changeType = util_1.getChangeType(change);
3838
yield eventTracker.record([{
39-
timestamp: util_1.getTimestamp(context, change),
39+
timestamp: context.timestamp,
4040
operation: changeType,
4141
documentName: context.resource.name,
4242
eventId: context.eventId,

firestore-bigquery-export/functions/lib/util.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,3 @@ function getChangeType(change) {
2626
return firestore_bigquery_change_tracker_1.ChangeType.UPDATE;
2727
}
2828
exports.getChangeType = getChangeType;
29-
function getTimestamp(context, change) {
30-
const changeType = getChangeType((change));
31-
switch (changeType) {
32-
case firestore_bigquery_change_tracker_1.ChangeType.CREATE:
33-
return change.after.updateTime.toDate();
34-
case firestore_bigquery_change_tracker_1.ChangeType.DELETE:
35-
// Due to an internal bug (129264426), before.update_time is actually the commit timestamp.
36-
return new Date(change.before.updateTime.toDate().getTime() + 1);
37-
case firestore_bigquery_change_tracker_1.ChangeType.UPDATE:
38-
return change.after.updateTime.toDate();
39-
default: {
40-
throw new Error(`Invalid change type: ${changeType}`);
41-
}
42-
}
43-
}
44-
exports.getTimestamp = getTimestamp;

firestore-bigquery-export/functions/src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@
1515
*/
1616

1717
import config from "./config";
18-
import * as firebase from "firebase-admin";
1918
import * as functions from "firebase-functions";
2019
import {
2120
ChangeType,
2221
FirestoreBigQueryEventHistoryTracker,
2322
FirestoreEventHistoryTracker,
24-
FirestoreSchema,
2523
} from "@firebaseextensions/firestore-bigquery-change-tracker";
26-
import * as _ from "lodash";
2724
import * as logs from "./logs";
28-
import {
29-
getChangeType,
30-
getTimestamp,
31-
} from "./util";
25+
import { getChangeType } from "./util";
3226

3327
const eventTracker: FirestoreEventHistoryTracker = new FirestoreBigQueryEventHistoryTracker(config);
3428

@@ -40,7 +34,7 @@ exports.fsexportbigquery = functions.handler.firestore.document.onWrite(
4034
try {
4135
const changeType = getChangeType(change);
4236
await eventTracker.record([{
43-
timestamp: getTimestamp(context, change),
37+
timestamp: context.timestamp, // This is a Cloud Firestore commit timestamp with microsecond precision.
4438
operation: changeType,
4539
documentName: context.resource.name,
4640
eventId: context.eventId,

firestore-bigquery-export/functions/src/util.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as firebase from "firebase-admin";
1817
import { DocumentSnapshot } from "firebase-functions/lib/providers/firestore";
19-
import { Change, EventContext } from "firebase-functions";
18+
import { Change } from "firebase-functions";
2019

2120
import {
2221
ChangeType
@@ -31,19 +30,3 @@ export function getChangeType(change: Change<DocumentSnapshot>): ChangeType {
3130
}
3231
return ChangeType.UPDATE;
3332
}
34-
35-
export function getTimestamp(context: EventContext, change: Change<DocumentSnapshot>): Date {
36-
const changeType = getChangeType((change));
37-
switch (changeType) {
38-
case ChangeType.CREATE:
39-
return change.after.updateTime.toDate();
40-
case ChangeType.DELETE:
41-
// Due to an internal bug (129264426), before.update_time is actually the commit timestamp.
42-
return new Date(change.before.updateTime.toDate().getTime() + 1);
43-
case ChangeType.UPDATE:
44-
return change.after.updateTime.toDate();
45-
default: {
46-
throw new Error(`Invalid change type: ${changeType}`);
47-
}
48-
}
49-
}

firestore-bigquery-export/scripts/import/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ const run = async (): Promise<number> => {
177177
const rows: FirestoreDocumentChangeEvent[] = docs.map(
178178
(snapshot) => {
179179
return {
180-
timestamp: new Date(0), // epoch
180+
timestamp: new Date(0).toISOString(), // epoch
181181
operation: ChangeType.IMPORT,
182182
documentName: snapshot.ref.path,
183183
eventId: "",

0 commit comments

Comments
 (0)