Skip to content

Commit

Permalink
fix(firestore-bigquery-export): fixed timestamp as a field-name parti…
Browse files Browse the repository at this point in the history
…tioning
  • Loading branch information
dackers86 committed May 15, 2024
2 parents 79ae81b + a57fc8d commit b452cfb
Show file tree
Hide file tree
Showing 14 changed files with 5,898 additions and 24 deletions.
5,835 changes: 5,832 additions & 3 deletions firestore-bigquery-export/firestore-bigquery-change-tracker/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "github.com/firebase/extensions.git",
"directory": "firestore-bigquery-export/firestore-bigquery-change-tracker"
},
"version": "1.1.33",
"version": "1.1.34",
"description": "Core change-tracker library for Cloud Firestore Collection BigQuery Exports",
"main": "./lib/index.js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("Using an alternative bigquery project", () => {
randomID = (Math.random() + 1).toString(36).substring(7);
datasetId = `dataset_${randomID}`;
tableId = `table_${randomID}`;
bq = new BigQuery();
bq = new BigQuery({ projectId: process.env.PROJECT_ID });
});
describe("has a valid alternative project id", () => {
beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

process.env.PROJECT_ID = "extensions-testing";

const bq: BigQuery = new BigQuery();
const bq: BigQuery = new BigQuery({ projectId: process.env.PROJECT_ID });
const event: FirestoreDocumentChangeEvent = changeTrackerEvent({});
let randomID: string;
let datasetId: string;
Expand Down Expand Up @@ -736,6 +736,7 @@ describe("Checking updates", () => {
}).record([event]);

const raw_changelog_table = bq.dataset(datasetId).table(tableId_raw);

const [metadata] = await raw_changelog_table.getMetadata();

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deleteTable } from "../fixtures/clearTables";

process.env.PROJECT_ID = "extensions-testing";

const bq: BigQuery = new BigQuery();
const bq: BigQuery = new BigQuery({ projectId: process.env.PROJECT_ID });
const event: FirestoreDocumentChangeEvent = changeTrackerEvent({});
let randomID: string;
let datasetId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ process.env.PROJECT_ID = "extensions-testing";
const consoleLogSpy = jest.spyOn(logger, "log").mockImplementation();
const consoleLogSpyWarn = jest.spyOn(logger, "warn").mockImplementation();

const bq: BigQuery = new BigQuery();
const bq: BigQuery = new BigQuery({ projectId: process.env.PROJECT_ID });
const event: FirestoreDocumentChangeEvent = changeTrackerEvent({});
let randomID: string;
let datasetId: string;
Expand Down Expand Up @@ -267,6 +267,41 @@ describe("e2e", () => {
);
});

test("successfully partitions with a valid Firebase Timestamp value with `timestamp` as field name and Timestamp type", async () => {
const created = firestore.Timestamp.now();
const expectedDate = created.toDate().toISOString().substring(0, 22);

const event: FirestoreDocumentChangeEvent = changeTrackerEvent({
data: { created },
});

await changeTracker({
datasetId,
tableId,
timePartitioning: "DAY",
timePartitioningField: "timestamp",
timePartitioningFieldType: "TIMESTAMP",
timePartitioningFirestoreField: "created",
}).record([event]);

const [metadata] = await dataset.table(`${tableId_raw}`).getMetadata();

const [changeLogRows] = await getBigQueryTableData(
process.env.PROJECT_ID,
datasetId,
tableId
);

expect(metadata.timePartitioning).toBeDefined();
expect(metadata.timePartitioning.type).toEqual("DAY");
expect(metadata.timePartitioning.field).toEqual("timestamp");

//TODO: check data has been added successfully
expect(changeLogRows[0].timestamp.value).toBe(
BigQuery.timestamp(created.toDate()).value
);
});

test("old_data is null if is not provided", async () => {
const event: FirestoreDocumentChangeEvent = changeTrackerEvent({
data: { foo: "foo" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ChangeType, FirestoreDocumentChangeEvent } from "../..";
import { FirestoreBigQueryEventHistoryTrackerConfig } from "../../bigquery";
import { Partitioning } from "../../bigquery/partitioning";
import { deleteTable } from "../fixtures/clearTables";
import { changeTracker } from "../fixtures/changeTracker";

let bq: BigQuery;
let dataset: Dataset;
Expand All @@ -16,7 +15,7 @@ let datasetId: string;

describe("processing partitions on a new table", () => {
beforeAll(async () => {
bq = new BigQuery();
bq = new BigQuery({ projectId: process.env.PROJECT_ID });
randomID = (Math.random() + 1).toString(36).substring(7);
datasetId = `bq_${randomID}`;
[dataset] = await bq.createDataset(datasetId, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ChangeType } from "../..";
import { buildLatestSnapshotViewQuery } from "../../bigquery/snapshot";
process.env.PROJECT_ID = "extensions-testing";

const bq = new BigQuery();
const bq = new BigQuery({ projectId: process.env.PROJECT_ID });
let event: FirestoreDocumentChangeEvent;
let randomID: string;
let datasetId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { deleteTable } from "../fixtures/clearTables";

process.env.PROJECT_ID = "extensions-testing";

const bq = new BigQuery();
const bq = new BigQuery({ projectId: process.env.PROJECT_ID });
let event: FirestoreDocumentChangeEvent;
let randomID: string;
let datasetId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const defaultQuery = (
LIMIT 1`;

export const getBigQueryTableData = async (bqProjectId, datasetId, tableId) => {
const bq = new BigQuery();
const bq = new BigQuery({ projectId: bqProjectId });

// Setup queries
const [changeLogQuery] = await bq.createQueryJob({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,41 +239,48 @@ export class Partitioning {
}

customFieldExists(fields = []) {
if (!fields.length) return false;

/** Extract the time partioning field name */
const { timePartitioningField } = this.config;

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

private async shouldAddPartitioningToSchema(): Promise<{
private async shouldAddPartitioningToSchema(fields: string[]): Promise<{
proceed: boolean;
message: string;
}> {
if (!this.isPartitioningEnabled()) {
return { proceed: false, message: "Partitioning not enabled" };
}

if (!this.hasValidTableReference()) {
return { proceed: false, message: "Invalid table reference" };
}

if (!this.hasValidCustomPartitionConfig()) {
return { proceed: false, message: "Invalid partition config" };
}

if (!this.hasValidTimePartitionType()) {
return { proceed: false, message: "Invalid partition type" };
}

if (!this.hasValidTimePartitionOption()) {
return { proceed: false, message: "Invalid partition option" };
}

if (this.hasHourAndDatePartitionConfig()) {
return {
proceed: false,
message: "Invalid partitioning and field type combination",
};
}
if (this.customFieldExists()) {

if (this.customFieldExists(fields)) {
return { proceed: false, message: "Field already exists on schema" };
}

if (await this.isTablePartitioned()) {
return { proceed: false, message: "Table is already partitioned" };
}
Expand All @@ -284,7 +291,10 @@ export class Partitioning {
}

async addPartitioningToSchema(fields = []): Promise<void> {
const { proceed, message } = await this.shouldAddPartitioningToSchema();
const { proceed, message } = await this.shouldAddPartitioningToSchema(
fields
);

if (!proceed) {
functions.logger.warn(`Did not add partitioning to schema: ${message}`);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"include": ["src/**/*.ts", "src/**/*.test.ts"]
}
"types": ["node", "jest"]
},
"include": ["src/**/*.ts", "src/**/*.test.ts"]
}
4 changes: 2 additions & 2 deletions firestore-bigquery-export/functions/__tests__/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * as admin from "firebase-admin";
import { BigQuery } from "@google-cloud/bigquery";

/** Set defaults */
const bqProjectId = "dev-extensions-testing";
const bqProjectId = "extensions-testing";
const datasetId = "firestore_export";
const tableId = "bq_e2e_test_raw_changelog";

/** Init resources */
admin.initializeApp({ projectId: bqProjectId });
const bq = new BigQuery({ projectId: "dev-extensions-testing" });
const bq = new BigQuery({ projectId: "extensions-testing" });
import { documentData } from "./fixtures/documentData";

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BigQuery } from "@google-cloud/bigquery";
import { getQueryResult, randomId, setupBQ } from "./helpers/setup";
import executeScript from "./helpers/executeScript";

const bq = new BigQuery({ projectId: "dev-extensions-testing" });
const bq = new BigQuery({ projectId: "extensions-testing" });

const datasetPrefix = "e2e_test_";

Expand Down

0 comments on commit b452cfb

Please sign in to comment.