1
1
import { verifyObjectMatchesProto , VerifyProtoErrorBehaviour } from "df/common/protos" ;
2
2
import {
3
3
ActionBuilder ,
4
- ILegacyTableBigqueryConfig ,
4
+ ILegacyBigQueryOptions ,
5
+ ILegacyTableConfig ,
5
6
ITableContext ,
6
- LegacyConfigConverter
7
+ LegacyConfigConverter ,
8
+ TableType
7
9
} from "df/core/actions" ;
8
10
import { Assertion } from "df/core/actions/assertion" ;
11
+ import { Table } from "df/core/actions/table" ;
12
+ import { View } from "df/core/actions/view" ;
9
13
import { ColumnDescriptors } from "df/core/column_descriptors" ;
10
14
import { Contextable , Resolvable } from "df/core/common" ;
11
15
import * as Path from "df/core/path" ;
@@ -25,25 +29,6 @@ import {
25
29
} from "df/core/utils" ;
26
30
import { dataform } from "df/protos/ts" ;
27
31
28
- /**
29
- * @hidden
30
- * This maintains backwards compatability with older versions.
31
- * TODO(ekrekr): consider breaking backwards compatability of these in v4.
32
- */
33
- export interface ILegacyIncrementalTableConfig
34
- extends dataform . ActionConfig . IncrementalTableConfig {
35
- dependencies : Resolvable [ ] ;
36
- database : string ;
37
- schema : string ;
38
- fileName : string ;
39
- type : string ;
40
- bigquery ?: ILegacyTableBigqueryConfig ;
41
- // Legacy incremental table config's table assertions cannot directly extend the protobuf
42
- // incremental table config definition because of legacy incremental table config's flexible
43
- // types.
44
- assertions : any ;
45
- }
46
-
47
32
/**
48
33
* @hidden
49
34
*/
@@ -71,9 +56,15 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
71
56
private uniqueKeyAssertions : Assertion [ ] = [ ] ;
72
57
private rowConditionsAssertion : Assertion ;
73
58
59
+ private unverifiedConfig : any ;
60
+ private configPath : string | undefined ;
61
+
74
62
constructor ( session ?: Session , unverifiedConfig ?: any , configPath ?: string ) {
75
63
super ( session ) ;
76
64
this . session = session ;
65
+ this . configPath = configPath ;
66
+ // A copy is used here to prevent manipulation of the original.
67
+ this . unverifiedConfig = Object . assign ( { } , unverifiedConfig ) ;
77
68
78
69
if ( ! unverifiedConfig ) {
79
70
return ;
@@ -158,11 +149,48 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
158
149
if ( config . filename ) {
159
150
this . proto . fileName = config . filename ;
160
151
}
161
- this . proto . onSchemaChange = this . mapOnSchemaChange ( config . onSchemaChange )
152
+ this . proto . onSchemaChange = this . mapOnSchemaChange ( config . onSchemaChange ) ;
162
153
163
154
return this ;
164
155
}
165
156
157
+ /**
158
+ * @hidden
159
+ * @deprecated
160
+ * Deprecated in favor of action type can being set in the configs passed to action constructor
161
+ * functions.
162
+ */
163
+ public type ( type : TableType ) {
164
+ let newAction : View | Table ;
165
+ switch ( type ) {
166
+ case "table" :
167
+ newAction = new Table (
168
+ this . session ,
169
+ { ...this . unverifiedConfig , type : "table" } ,
170
+ this . configPath
171
+ ) ;
172
+ break ;
173
+ case "incremental" :
174
+ return this ;
175
+ case "view" :
176
+ newAction = new View (
177
+ this . session ,
178
+ { ...this . unverifiedConfig , type : "view" } ,
179
+ this . configPath
180
+ ) ;
181
+ break ;
182
+ default :
183
+ throw new Error ( `Unexpected table type: ${ type } ` ) ;
184
+ }
185
+ const existingAction = this . session . actions . indexOf ( this ) ;
186
+ if ( existingAction === - 1 ) {
187
+ throw Error (
188
+ "Expected pre-existing action, but none found. Please report this to the Dataform team."
189
+ ) ;
190
+ }
191
+ this . session . actions [ existingAction ] = newAction ;
192
+ }
193
+
166
194
public query ( query : Contextable < ITableContext , string > ) {
167
195
this . contextableQuery = query ;
168
196
return this ;
@@ -398,8 +426,15 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
398
426
return protoOps ;
399
427
}
400
428
429
+ /**
430
+ * Verify config checks that the constructor provided config matches the expected proto structure,
431
+ * or the previously accepted legacy structure. If the legacy structure is used, it is converted
432
+ * to the new structure.
433
+ */
401
434
private verifyConfig (
402
- unverifiedConfig : ILegacyIncrementalTableConfig
435
+ // `any` is used here to facilitate the type merging of the legacy table config, which is very
436
+ // different to the new structure.
437
+ unverifiedConfig : dataform . ActionConfig . IncrementalTableConfig | ILegacyTableConfig | any
403
438
) : dataform . ActionConfig . IncrementalTableConfig {
404
439
// The "type" field only exists on legacy incremental table configs. Here we convert them to the
405
440
// new format.
@@ -441,7 +476,7 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
441
476
throw e ;
442
477
} ,
443
478
unverifiedConfig . bigquery ,
444
- strictKeysOf < ILegacyTableBigqueryConfig > ( ) ( [
479
+ strictKeysOf < ILegacyBigQueryOptions > ( ) ( [
445
480
"partitionBy" ,
446
481
"clusterBy" ,
447
482
"updatePartitionFilter" ,
@@ -471,27 +506,37 @@ export class IncrementalTable extends ActionBuilder<dataform.Table> {
471
506
// - for sqlx it will have type "string"
472
507
// - for action.yaml it will be converted to enum which is represented
473
508
// in TypeScript as a "number".
474
- private mapOnSchemaChange ( onSchemaChange ?: string | number ) : dataform . OnSchemaChange {
509
+ private mapOnSchemaChange ( onSchemaChange ?: string | number ) : dataform . OnSchemaChange {
475
510
if ( ! onSchemaChange ) {
476
511
return dataform . OnSchemaChange . IGNORE ;
477
512
}
478
513
479
514
if ( typeof onSchemaChange === "number" ) {
480
515
switch ( onSchemaChange ) {
481
- case dataform . ActionConfig . OnSchemaChange . IGNORE : return dataform . OnSchemaChange . IGNORE ;
482
- case dataform . ActionConfig . OnSchemaChange . FAIL : return dataform . OnSchemaChange . FAIL ;
483
- case dataform . ActionConfig . OnSchemaChange . EXTEND : return dataform . OnSchemaChange . EXTEND ;
484
- case dataform . ActionConfig . OnSchemaChange . SYNCHRONIZE : return dataform . OnSchemaChange . SYNCHRONIZE ;
485
- default : throw new Error ( `OnSchemaChange value "${ onSchemaChange } " is not supported` ) ;
516
+ case dataform . ActionConfig . OnSchemaChange . IGNORE :
517
+ return dataform . OnSchemaChange . IGNORE ;
518
+ case dataform . ActionConfig . OnSchemaChange . FAIL :
519
+ return dataform . OnSchemaChange . FAIL ;
520
+ case dataform . ActionConfig . OnSchemaChange . EXTEND :
521
+ return dataform . OnSchemaChange . EXTEND ;
522
+ case dataform . ActionConfig . OnSchemaChange . SYNCHRONIZE :
523
+ return dataform . OnSchemaChange . SYNCHRONIZE ;
524
+ default :
525
+ throw new Error ( `OnSchemaChange value "${ onSchemaChange } " is not supported` ) ;
486
526
}
487
527
}
488
528
489
529
switch ( onSchemaChange . toString ( ) . toUpperCase ( ) ) {
490
- case "IGNORE" : return dataform . OnSchemaChange . IGNORE ;
491
- case "FAIL" : return dataform . OnSchemaChange . FAIL ;
492
- case "EXTEND" : return dataform . OnSchemaChange . EXTEND ;
493
- case "SYNCHRONIZE" : return dataform . OnSchemaChange . SYNCHRONIZE ;
494
- default : throw new Error ( `OnSchemaChange value "${ onSchemaChange } " is not supported` ) ;
530
+ case "IGNORE" :
531
+ return dataform . OnSchemaChange . IGNORE ;
532
+ case "FAIL" :
533
+ return dataform . OnSchemaChange . FAIL ;
534
+ case "EXTEND" :
535
+ return dataform . OnSchemaChange . EXTEND ;
536
+ case "SYNCHRONIZE" :
537
+ return dataform . OnSchemaChange . SYNCHRONIZE ;
538
+ default :
539
+ throw new Error ( `OnSchemaChange value "${ onSchemaChange } " is not supported` ) ;
495
540
}
496
541
}
497
542
}
0 commit comments