@@ -4,7 +4,7 @@ import * as firebase from "firebase-admin";
44
55import * as logs from "../logs" ;
66import * as bigquery from "@google-cloud/bigquery" ;
7-
7+ import * as functions from "firebase-functions" ;
88import { getNewPartitionField } from "./schema" ;
99import { BigQuery , TableMetadata } from "@google-cloud/bigquery" ;
1010
@@ -135,18 +135,22 @@ export class Partitioning {
135135 }
136136
137137 private async isTablePartitioned ( ) {
138+ const [ tableExists ] = await this . table . exists ( ) ;
139+
140+ if ( ! this . table || ! tableExists ) return false ;
141+
138142 /* Return true if partition metadata already exists */
139143 const [ metadata ] = await this . table . getMetadata ( ) ;
140- if ( ! ! metadata . timePartitioning ) {
144+ if ( metadata . timePartitioning ) {
141145 logs . cannotPartitionExistingTable ( this . table ) ;
142- return Promise . resolve ( true ) ;
146+ return true ;
143147 }
144148
145149 /** Find schema fields **/
146150 const schemaFields = await this . metaDataSchemaFields ( ) ;
147151
148152 /** Return false if no schema exists */
149- if ( ! schemaFields ) return Promise . resolve ( false ) ;
153+ if ( ! schemaFields ) return false ;
150154
151155 /* Return false if time partition field not found */
152156 return schemaFields . some (
@@ -156,11 +160,11 @@ export class Partitioning {
156160
157161 async isValidPartitionForExistingTable ( ) : Promise < boolean > {
158162 /** Return false if partition type option has not been set */
159- if ( ! this . isPartitioningEnabled ( ) ) return Promise . resolve ( false ) ;
163+ if ( ! this . isPartitioningEnabled ( ) ) return false ;
160164
161165 /* Return false if table is already partitioned */
162166 const isPartitioned = await this . isTablePartitioned ( ) ;
163- if ( isPartitioned ) return Promise . resolve ( false ) ;
167+ if ( isPartitioned ) return false ;
164168
165169 return this . hasValidCustomPartitionConfig ( ) ;
166170 }
@@ -242,44 +246,54 @@ export class Partitioning {
242246 return fields . map ( ( $ ) => $ . name ) . includes ( timePartitioningField ) ;
243247 }
244248
245- async addPartitioningToSchema ( fields = [ ] ) : Promise < void > {
246- /** Return if partition type option has not been set */
247- if ( ! this . isPartitioningEnabled ( ) ) return ;
248-
249- /** Return if class has invalid table reference */
250- if ( ! this . hasValidTableReference ( ) ) return ;
251-
252- /** Return if table is already partitioned **/
253- if ( await this . isTablePartitioned ( ) ) return ;
254-
255- /** Return if partition config is invalid */
256- if ( ! this . hasValidCustomPartitionConfig ( ) ) return ;
257-
258- /** Return if an invalid partition type has been requested */
259- if ( ! this . hasValidTimePartitionType ( ) ) return ;
260-
261- /** Return if an invalid partition option has been requested */
262- if ( ! this . hasValidTimePartitionOption ( ) ) return ;
263-
264- /** Return if invalid partitioning and field type combination */
265- if ( this . hasHourAndDatePartitionConfig ( ) ) return ;
266-
267- /** Return if partition field has not been provided */
268- if ( ! this . config . timePartitioningField ) return ;
269-
270- /** Return if field already exists on schema */
271- if ( this . customFieldExists ( fields ) ) return ;
249+ private async shouldAddPartitioningToSchema ( ) : Promise < {
250+ proceed : boolean ;
251+ message : string ;
252+ } > {
253+ if ( ! this . isPartitioningEnabled ( ) ) {
254+ return { proceed : false , message : "Partitioning not enabled" } ;
255+ }
256+ if ( ! this . hasValidTableReference ( ) ) {
257+ return { proceed : false , message : "Invalid table reference" } ;
258+ }
259+ if ( ! this . hasValidCustomPartitionConfig ( ) ) {
260+ return { proceed : false , message : "Invalid partition config" } ;
261+ }
262+ if ( ! this . hasValidTimePartitionType ( ) ) {
263+ return { proceed : false , message : "Invalid partition type" } ;
264+ }
265+ if ( ! this . hasValidTimePartitionOption ( ) ) {
266+ return { proceed : false , message : "Invalid partition option" } ;
267+ }
268+ if ( this . hasHourAndDatePartitionConfig ( ) ) {
269+ return {
270+ proceed : false ,
271+ message : "Invalid partitioning and field type combination" ,
272+ } ;
273+ }
274+ if ( this . customFieldExists ( ) ) {
275+ return { proceed : false , message : "Field already exists on schema" } ;
276+ }
277+ if ( await this . isTablePartitioned ( ) ) {
278+ return { proceed : false , message : "Table is already partitioned" } ;
279+ }
280+ if ( ! this . config . timePartitioningField ) {
281+ return { proceed : false , message : "Partition field not provided" } ;
282+ }
283+ return { proceed : true , message : "" } ;
284+ }
272285
273- /** Add new partitioning field **/
286+ async addPartitioningToSchema ( fields = [ ] ) : Promise < void > {
287+ const { proceed, message } = await this . shouldAddPartitioningToSchema ( ) ;
288+ if ( ! proceed ) {
289+ functions . logger . warn ( `Did not add partitioning to schema: ${ message } ` ) ;
290+ return ;
291+ }
292+ // Add new partitioning field
274293 fields . push ( getNewPartitionField ( this . config ) ) ;
275-
276- /** Log successful addition of partition column */
277- logs . addPartitionFieldColumn (
278- this . table . id ,
279- this . config . timePartitioningField
294+ functions . logger . log (
295+ `Added new partition field: ${ this . config . timePartitioningField } to table ID: ${ this . table . id } `
280296 ) ;
281-
282- return ;
283297 }
284298
285299 async updateTableMetadata ( options : bigquery . TableMetadata ) : Promise < void > {
0 commit comments