@@ -4,7 +4,7 @@ import * as firebase from "firebase-admin";
4
4
5
5
import * as logs from "../logs" ;
6
6
import * as bigquery from "@google-cloud/bigquery" ;
7
-
7
+ import * as functions from "firebase-functions" ;
8
8
import { getNewPartitionField } from "./schema" ;
9
9
import { BigQuery , TableMetadata } from "@google-cloud/bigquery" ;
10
10
@@ -135,18 +135,22 @@ export class Partitioning {
135
135
}
136
136
137
137
private async isTablePartitioned ( ) {
138
+ const [ tableExists ] = await this . table . exists ( ) ;
139
+
140
+ if ( ! this . table || ! tableExists ) return false ;
141
+
138
142
/* Return true if partition metadata already exists */
139
143
const [ metadata ] = await this . table . getMetadata ( ) ;
140
- if ( ! ! metadata . timePartitioning ) {
144
+ if ( metadata . timePartitioning ) {
141
145
logs . cannotPartitionExistingTable ( this . table ) ;
142
- return Promise . resolve ( true ) ;
146
+ return true ;
143
147
}
144
148
145
149
/** Find schema fields **/
146
150
const schemaFields = await this . metaDataSchemaFields ( ) ;
147
151
148
152
/** Return false if no schema exists */
149
- if ( ! schemaFields ) return Promise . resolve ( false ) ;
153
+ if ( ! schemaFields ) return false ;
150
154
151
155
/* Return false if time partition field not found */
152
156
return schemaFields . some (
@@ -156,11 +160,11 @@ export class Partitioning {
156
160
157
161
async isValidPartitionForExistingTable ( ) : Promise < boolean > {
158
162
/** Return false if partition type option has not been set */
159
- if ( ! this . isPartitioningEnabled ( ) ) return Promise . resolve ( false ) ;
163
+ if ( ! this . isPartitioningEnabled ( ) ) return false ;
160
164
161
165
/* Return false if table is already partitioned */
162
166
const isPartitioned = await this . isTablePartitioned ( ) ;
163
- if ( isPartitioned ) return Promise . resolve ( false ) ;
167
+ if ( isPartitioned ) return false ;
164
168
165
169
return this . hasValidCustomPartitionConfig ( ) ;
166
170
}
@@ -242,44 +246,54 @@ export class Partitioning {
242
246
return fields . map ( ( $ ) => $ . name ) . includes ( timePartitioningField ) ;
243
247
}
244
248
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
+ }
272
285
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
274
293
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 } `
280
296
) ;
281
-
282
- return ;
283
297
}
284
298
285
299
async updateTableMetadata ( options : bigquery . TableMetadata ) : Promise < void > {
0 commit comments