Skip to content

Commit

Permalink
fix(firestore-bigquery-export): partition if only timestamp is provid…
Browse files Browse the repository at this point in the history
…ed (#1854)
  • Loading branch information
cabljac authored Dec 1, 2023
1 parent 7da6e53 commit 3dc59f2
Show file tree
Hide file tree
Showing 3 changed files with 989 additions and 9,550 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,53 @@ describe("processing partitions on a new table", () => {
});
});
});

describe("updateTableMetadata", () => {
test("updates the table metadata with the timestamp field", async () => {
const config: FirestoreBigQueryEventHistoryTrackerConfig = {
datasetId: "",
tableId: "",
datasetLocation: "",
timePartitioning: "MONTH",
timePartitioningField: "timestamp",
timePartitioningFieldType: undefined,
timePartitioningFirestoreField: undefined,
transformFunction: "",
clustering: [],
bqProjectId: null,
};
const options = {};

const partitioning = new Partitioning(config, table);

await partitioning.updateTableMetadata(options);

expect(options).toEqual({
timePartitioning: {
field: "timestamp",
type: "MONTH",
},
});
});
test("Should not update if there is a custom option with the timestamp option", async () => {
const config: FirestoreBigQueryEventHistoryTrackerConfig = {
datasetId: "",
tableId: "",
datasetLocation: "",
timePartitioning: "MONTH",
timePartitioningField: "timestamp",
timePartitioningFieldType: "DATETIME",
timePartitioningFirestoreField: undefined,
transformFunction: "",
clustering: [],
bqProjectId: null,
};
const options = {};

const partitioning = new Partitioning(config, table);

await partitioning.updateTableMetadata(options);

expect(options).toEqual({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,19 @@ export class Partitioning {
!timePartitioningField &&
!timePartitioningFieldType &&
!timePartitioningFirestoreField;

/* No custom config has been set, use partition value option only */
if (hasNoCustomOptions) return true;

/* check if all options have been provided to be */
/* check if all valid combinations have been provided*/
const hasOnlyTimestamp =
timePartitioningField === "timestamp" &&
!timePartitioningFieldType &&
!timePartitioningFirestoreField;
return (
!!timePartitioningField &&
!!timePartitioningFieldType &&
!!timePartitioningFirestoreField
hasOnlyTimestamp ||
(!!timePartitioningField &&
!!timePartitioningFieldType &&
!!timePartitioningFirestoreField)
);
}

Expand Down Expand Up @@ -248,33 +252,31 @@ export class Partitioning {

async addPartitioningToSchema(fields = []): Promise<void> {
/** check if class has valid table reference */
if (!this.hasValidTableReference()) return Promise.resolve();
if (!this.hasValidTableReference()) return;

/** return if table is already partitioned **/
if (await this.isTablePartitioned()) return Promise.resolve();
if (await this.isTablePartitioned()) return;

/** return if an invalid partition type has been requested**/
if (!this.hasValidTimePartitionType()) return Promise.resolve();

if (!this.hasValidTimePartitionType()) return;
/** Return if invalid partitioning and field type combination */
if (this.hasHourAndDatePartitionConfig()) return Promise.resolve();
if (this.hasHourAndDatePartitionConfig()) return;

/** return if an invalid partition type has been requested**/
if (!this.hasValidCustomPartitionConfig()) return Promise.resolve();
if (!this.hasValidCustomPartitionConfig()) return;

/** return if an invalid partition type has been requested**/
if (!this.hasValidCustomPartitionConfig()) return Promise.resolve();
if (!this.hasValidCustomPartitionConfig()) return;

/** update fields with new schema option ** */
if (!this.hasValidTimePartitionOption()) return Promise.resolve();

if (!this.hasValidTimePartitionOption()) return;
/* Check if partition field has been provided */
if (!this.config.timePartitioningField) return Promise.resolve();
if (!this.config.timePartitioningField) return;

// if (await !this.hasExistingSchema) return Promise.resolve();

// Field already exists on schema, skip
if (this.customFieldExists(fields)) return Promise.resolve();
if (this.customFieldExists(fields)) return;

fields.push(getNewPartitionField(this.config));

Expand All @@ -284,24 +286,24 @@ export class Partitioning {
this.config.timePartitioningField
);

return Promise.resolve();
return;
}

async updateTableMetadata(options: bigquery.TableMetadata): Promise<void> {
/** return if table is already partitioned **/
if (await this.isTablePartitioned()) return Promise.resolve();
if (await this.isTablePartitioned()) return;

/** return if an invalid partition type has been requested**/
if (!this.hasValidTimePartitionType()) return Promise.resolve();
if (!this.hasValidTimePartitionType()) return;

/** update fields with new schema option ** */
if (!this.hasValidTimePartitionOption()) return Promise.resolve();
if (!this.hasValidTimePartitionOption()) return;

/** Return if invalid partitioning and field type combination */
if (this.hasHourAndDatePartitionConfig()) return Promise.resolve();
if (this.hasHourAndDatePartitionConfig()) return;

/** return if an invalid partition type has been requested**/
if (!this.hasValidCustomPartitionConfig()) return Promise.resolve();
if (!this.hasValidCustomPartitionConfig()) return;

// if (await !this.hasExistingSchema) return Promise.resolve();

Expand Down
Loading

0 comments on commit 3dc59f2

Please sign in to comment.