-
Notifications
You must be signed in to change notification settings - Fork 427
/
table.go
968 lines (847 loc) · 30.9 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
package resources
import (
"context"
"fmt"
"log"
"strconv"
"strings"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/previewfeatures"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/datatypes"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
// TODO [SNOW-1348114]: old implementation was quoting every column, SDK is not quoting them, therefore they are quoted here: decide if we quote columns or not
// TODO [SNOW-1031688]: move data manipulation logic to the SDK - SQL generation or builders part (e.g. different default types/identity)
var tableSchema = map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Specifies the identifier for the table; must be unique for the database and schema in which the table is created.",
DiffSuppressFunc: suppressIdentifierQuoting,
},
"schema": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The schema in which to create the table.",
},
"database": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The database in which to create the table.",
},
"cluster_by": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "A list of one or more table columns/expressions to be used as clustering key(s) for the table",
},
"column": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Description: "Definitions of a column to create in the table. Minimum one required.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Column name",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "Column type, e.g. VARIANT. For a full list of column types, see [Summary of Data Types](https://docs.snowflake.com/en/sql-reference/intro-summary-data-types).",
ValidateDiagFunc: IsDataTypeValid,
DiffSuppressFunc: DiffSuppressDataTypes,
},
"nullable": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether this column can contain null values. **Note**: Depending on your Snowflake version, the default value will not suffice if this column is used in a primary key constraint.",
},
"default": {
Type: schema.TypeList,
Optional: true,
Description: "Defines the column default value; note due to limitations of Snowflake's ALTER TABLE ADD/MODIFY COLUMN updates to default will not be applied",
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
// TODO [SNOW-1348114]: there is no such separation on SDK level. Should we keep it in V1?
Schema: map[string]*schema.Schema{
"constant": {
Type: schema.TypeString,
Optional: true,
Description: "The default constant value for the column",
// ConflictsWith: []string{".expression", ".sequence"}, - can't use, nor ExactlyOneOf due to column type being TypeList
},
"expression": {
Type: schema.TypeString,
Optional: true,
Description: "The default expression value for the column",
// ConflictsWith: []string{".constant", ".sequence"}, - can't use, nor ExactlyOneOf due to column type being TypeList
},
"sequence": {
Type: schema.TypeString,
Optional: true,
Description: "The default sequence to use for the column",
DiffSuppressFunc: suppressIdentifierQuoting,
// ConflictsWith: []string{".constant", ".expression"}, - can't use, nor ExactlyOneOf due to column type being TypeList
},
},
},
},
/*Note: Identity and default are mutually exclusive. From what I can tell we can't enforce this here
the snowflake query will error so we can defer enforcement to there.
*/
"identity": {
Type: schema.TypeList,
Optional: true,
Description: "Defines the identity start/step values for a column. **Note** Identity/default are mutually exclusive.",
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start_num": {
Type: schema.TypeInt,
Optional: true,
Description: "The number to start incrementing at.",
Default: 1,
},
"step_num": {
Type: schema.TypeInt,
Optional: true,
Description: "Step size to increment by.",
Default: 1,
},
},
},
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Column comment",
},
"masking_policy": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Masking policy to apply on column. It has to be a fully qualified name.",
},
"collate": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Column collation, e.g. utf8",
},
"schema_evolution_record": {
Type: schema.TypeString,
Computed: true,
Description: "Record of schema evolution.",
},
},
},
},
"comment": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies a comment for the table.",
},
"owner": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the role that owns the table.",
},
"primary_key": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Definitions of primary key constraint to create on table",
Deprecated: "Use snowflake_table_constraint instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of constraint",
},
"keys": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "Columns to use in primary key",
},
},
},
},
"data_retention_time_in_days": {
Type: schema.TypeInt,
Optional: true,
Default: IntDefault,
Description: "Specifies the retention period for the table so that Time Travel actions (SELECT, CLONE, UNDROP) can be performed on historical data in the table. If you wish to inherit the parent schema setting then pass in the schema attribute to this argument or do not fill this parameter at all; the default value for this field is -1, which is a fallback to use Snowflake default - in this case the schema value",
ValidateFunc: validation.IntBetween(-1, 90),
},
"change_tracking": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Specifies whether to enable change tracking on the table. Default false.",
},
"tag": tagReferenceSchema,
FullyQualifiedNameAttributeName: schemas.FullyQualifiedNameSchema,
}
func Table() *schema.Resource {
return &schema.Resource{
CreateContext: PreviewFeatureCreateContextWrapper(string(previewfeatures.TableResource), TrackingCreateWrapper(resources.Table, CreateTable)),
ReadContext: PreviewFeatureReadContextWrapper(string(previewfeatures.TableResource), TrackingReadWrapper(resources.Table, ReadTable)),
UpdateContext: PreviewFeatureUpdateContextWrapper(string(previewfeatures.TableResource), TrackingUpdateWrapper(resources.Table, UpdateTable)),
DeleteContext: PreviewFeatureDeleteContextWrapper(string(previewfeatures.TableResource), TrackingDeleteWrapper(resources.Table, DeleteTable)),
CustomizeDiff: TrackingCustomDiffWrapper(resources.Table, customdiff.All(
ComputedIfAnyAttributeChanged(tableSchema, FullyQualifiedNameAttributeName, "name"),
)),
Schema: tableSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
type columnDefault struct {
constant *string
expression *string
sequence *string
}
func (cd *columnDefault) _type() string {
if cd.constant != nil {
return "constant"
}
if cd.expression != nil {
return "expression"
}
if cd.sequence != nil {
return "sequence"
}
return "unknown"
}
type columnIdentity struct {
startNum int
stepNum int
}
type column struct {
name string
dataType string
nullable bool
_default *columnDefault
identity *columnIdentity
comment string
maskingPolicy string
collate string
}
type columns []column
type changedColumns []changedColumn
type changedColumn struct {
newColumn column // our new column
changedDataType bool
changedNullConstraint bool
dropedDefault bool
changedComment bool
changedMaskingPolicy bool
changedCollate bool
}
func (c columns) getChangedColumnProperties(new columns) (changed changedColumns) {
changed = changedColumns{}
for _, cO := range c {
for _, cN := range new {
changeColumn := changedColumn{cN, false, false, false, false, false, false}
if cO.name == cN.name && cO.dataType != cN.dataType {
changeColumn.changedDataType = true
}
if cO.name == cN.name && cO.nullable != cN.nullable {
changeColumn.changedNullConstraint = true
}
if cO.name == cN.name && cO._default != nil && cN._default == nil {
changeColumn.dropedDefault = true
}
if cO.name == cN.name && cO.comment != cN.comment {
changeColumn.changedComment = true
}
if cO.name == cN.name && cO.maskingPolicy != cN.maskingPolicy {
changeColumn.changedMaskingPolicy = true
}
if cO.name == cN.name && cO.collate != cN.collate {
changeColumn.changedCollate = true
}
changed = append(changed, changeColumn)
}
}
return
}
func (c columns) diffs(new columns) (removed columns, added columns, changed changedColumns) {
return c.getNewIn(new), new.getNewIn(c), c.getChangedColumnProperties(new)
}
func getColumnDefault(def map[string]interface{}) *columnDefault {
if c, ok := def["constant"]; ok {
if constant, ok := c.(string); ok && len(constant) > 0 {
return &columnDefault{
constant: &constant,
}
}
}
if e, ok := def["expression"]; ok {
if expr, ok := e.(string); ok && len(expr) > 0 {
return &columnDefault{
expression: &expr,
}
}
}
if s, ok := def["sequence"]; ok {
if seq := s.(string); ok && len(seq) > 0 {
return &columnDefault{
sequence: &seq,
}
}
}
return nil
}
func getColumnIdentity(identity map[string]interface{}) *columnIdentity {
if len(identity) > 0 {
startNum := identity["start_num"].(int)
stepNum := identity["step_num"].(int)
return &columnIdentity{startNum, stepNum}
}
return nil
}
func getColumn(from interface{}) (to column) {
c := from.(map[string]interface{})
var cd *columnDefault
var id *columnIdentity
_default := c["default"].([]interface{})
identity := c["identity"].([]interface{})
if len(_default) == 1 {
cd = getColumnDefault(_default[0].(map[string]interface{}))
}
if len(identity) == 1 {
id = getColumnIdentity(identity[0].(map[string]interface{}))
}
return column{
name: c["name"].(string),
dataType: c["type"].(string),
nullable: c["nullable"].(bool),
_default: cd,
identity: id,
comment: c["comment"].(string),
collate: c["collate"].(string),
maskingPolicy: c["masking_policy"].(string),
}
}
func getColumns(from interface{}) (to columns) {
cols := from.([]interface{})
to = make(columns, len(cols))
for i, c := range cols {
to[i] = getColumn(c)
}
return to
}
func getTableColumnRequest(from interface{}) (*sdk.TableColumnRequest, error) {
c := from.(map[string]interface{})
_type := c["type"].(string)
dataType, err := datatypes.ParseDataType(_type)
if err != nil {
return nil, err
}
nameInQuotes := fmt.Sprintf(`"%v"`, snowflake.EscapeString(c["name"].(string)))
request := sdk.NewTableColumnRequest(nameInQuotes, sdk.DataType(_type))
_default := c["default"].([]interface{})
var expression string
if len(_default) == 1 {
if c, ok := _default[0].(map[string]interface{})["constant"]; ok {
if constant, ok := c.(string); ok && len(constant) > 0 {
if datatypes.IsTextDataType(dataType) {
expression = snowflake.EscapeSnowflakeString(constant)
} else {
expression = constant
}
}
}
if e, ok := _default[0].(map[string]interface{})["expression"]; ok {
if expr, ok := e.(string); ok && len(expr) > 0 {
expression = expr
}
}
if s, ok := _default[0].(map[string]interface{})["sequence"]; ok {
if seq, ok2 := s.(string); ok2 && len(seq) > 0 {
expression = fmt.Sprintf(`%v.NEXTVAL`, seq)
}
}
request.WithDefaultValue(sdk.NewColumnDefaultValueRequest().WithExpression(sdk.String(expression)))
}
identity := c["identity"].([]interface{})
if len(identity) == 1 {
identityProp := identity[0].(map[string]interface{})
startNum := identityProp["start_num"].(int)
stepNum := identityProp["step_num"].(int)
request.WithDefaultValue(sdk.NewColumnDefaultValueRequest().WithIdentity(sdk.NewColumnIdentityRequest(startNum, stepNum)))
}
maskingPolicy := c["masking_policy"].(string)
if maskingPolicy != "" {
request.WithMaskingPolicy(sdk.NewColumnMaskingPolicyRequest(sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(maskingPolicy)))
}
if datatypes.IsTextDataType(dataType) {
request.WithCollate(sdk.String(c["collate"].(string)))
}
return request.
WithNotNull(sdk.Bool(!c["nullable"].(bool))).
WithComment(sdk.String(c["comment"].(string))), nil
}
func getTableColumnRequests(from interface{}) ([]sdk.TableColumnRequest, error) {
cols := from.([]interface{})
to := make([]sdk.TableColumnRequest, len(cols))
for i, c := range cols {
cReq, err := getTableColumnRequest(c)
if err != nil {
return nil, err
}
to[i] = *cReq
}
return to, nil
}
type primarykey struct {
name string
keys []string
}
func getPrimaryKey(from interface{}) (to primarykey) {
pk := from.([]interface{})
to = primarykey{}
if len(pk) > 0 {
pkDetails := pk[0].(map[string]interface{})
to.name = pkDetails["name"].(string)
to.keys = expandStringList(pkDetails["keys"].([]interface{}))
return to
}
return to
}
func toColumnConfig(descriptions []sdk.TableColumnDetails) []any {
flattened := make([]any, 0)
for _, td := range descriptions {
if td.Kind != "COLUMN" {
continue
}
flat := map[string]any{}
flat["name"] = td.Name
flat["type"] = string(td.Type)
flat["nullable"] = td.IsNullable
if td.Comment != nil {
flat["comment"] = *td.Comment
}
if td.Collation != nil {
flat["collate"] = *td.Collation
}
if td.PolicyName != nil {
// TODO [SNOW-867240]: SHOW TABLE returns last part of id without double quotes... we have to quote it again. Move it to SDK.
flat["masking_policy"] = sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(*td.PolicyName).FullyQualifiedName()
}
identity := toColumnIdentityConfig(td)
if identity != nil {
flat["identity"] = []any{identity}
} else {
def := toColumnDefaultConfig(td)
if def != nil {
flat["default"] = []any{def}
}
}
if td.SchemaEvolutionRecord != nil {
flat["schema_evolution_record"] = *td.SchemaEvolutionRecord
}
flattened = append(flattened, flat)
}
return flattened
}
func toColumnDefaultConfig(td sdk.TableColumnDetails) map[string]any {
if td.Default == nil {
return nil
}
defaultRaw := *td.Default
def := map[string]any{}
if strings.HasSuffix(defaultRaw, ".NEXTVAL") {
// TODO [SNOW-867240]: SHOW TABLE returns last part of id without double quotes... we have to quote it again. Move it to SDK.
sequenceIdRaw := strings.TrimSuffix(defaultRaw, ".NEXTVAL")
def["sequence"] = sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(sequenceIdRaw).FullyQualifiedName()
return def
}
if strings.Contains(defaultRaw, "(") && strings.Contains(defaultRaw, ")") {
def["expression"] = defaultRaw
return def
}
if sdk.IsStringType(string(td.Type)) {
def["constant"] = snowflake.UnescapeSnowflakeString(defaultRaw)
return def
}
def["constant"] = defaultRaw
return def
}
func toColumnIdentityConfig(td sdk.TableColumnDetails) map[string]any {
// if autoincrement is used this is reflected back IDENTITY START 1 INCREMENT 1
if td.Default == nil {
return nil
}
defaultRaw := *td.Default
if strings.Contains(defaultRaw, "IDENTITY") {
identity := map[string]any{}
split := strings.Split(defaultRaw, " ")
start, err := strconv.Atoi(split[2])
if err == nil {
identity["start_num"] = start
}
step, err := strconv.Atoi(split[4])
if err == nil {
identity["step_num"] = step
}
return identity
}
return nil
}
// CreateTable implements schema.CreateFunc.
func CreateTable(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
databaseName := d.Get("database").(string)
schemaName := d.Get("schema").(string)
name := d.Get("name").(string)
id := sdk.NewSchemaObjectIdentifier(databaseName, schemaName, name)
tableColumnRequests, err := getTableColumnRequests(d.Get("column").([]interface{}))
if err != nil {
return diag.FromErr(err)
}
createRequest := sdk.NewCreateTableRequest(id, tableColumnRequests)
if v, ok := d.GetOk("comment"); ok {
createRequest.WithComment(sdk.String(v.(string)))
}
if v, ok := d.GetOk("cluster_by"); ok {
createRequest.WithClusterBy(expandStringList(v.([]interface{})))
}
if v, ok := d.GetOk("primary_key"); ok {
keysList := v.([]any)
if len(keysList) > 0 {
keys := expandStringList(keysList[0].(map[string]any)["keys"].([]interface{}))
constraintRequest := sdk.NewOutOfLineConstraintRequest(sdk.ColumnConstraintTypePrimaryKey).WithColumns(snowflake.QuoteStringList(keys))
keyName, isPresent := keysList[0].(map[string]any)["name"]
if isPresent && keyName != "" {
constraintRequest.WithName(sdk.String(keyName.(string)))
}
}
}
if v := d.Get("data_retention_time_in_days"); v.(int) != IntDefault {
createRequest.WithDataRetentionTimeInDays(sdk.Int(v.(int)))
}
if v, ok := d.GetOk("change_tracking"); ok {
createRequest.WithChangeTracking(sdk.Bool(v.(bool)))
}
var tagAssociationRequests []sdk.TagAssociationRequest
if _, ok := d.GetOk("tag"); ok {
tagAssociations := getPropertyTags(d, "tag")
tagAssociationRequests = make([]sdk.TagAssociationRequest, len(tagAssociations))
for i, t := range tagAssociations {
tagAssociationRequests[i] = *sdk.NewTagAssociationRequest(t.Name, t.Value)
}
createRequest.WithTags(tagAssociationRequests)
}
err = client.Tables.Create(ctx, createRequest)
if err != nil {
return diag.FromErr(fmt.Errorf("error creating table %v err = %w", name, err))
}
d.SetId(helpers.EncodeSnowflakeID(id))
return ReadTable(ctx, d, meta)
}
// ReadTable implements schema.ReadFunc.
func ReadTable(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier)
table, err := client.Tables.ShowByID(ctx, id)
if err != nil {
log.Printf("[DEBUG] table (%s) not found", d.Id())
d.SetId("")
return nil
}
s, err := client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName()))
if err != nil {
log.Printf("[DEBUG] schema (%s) not found", d.Id())
d.SetId("")
return nil
}
if err := d.Set(FullyQualifiedNameAttributeName, id.FullyQualifiedName()); err != nil {
return diag.FromErr(err)
}
var schemaRetentionTime int64
// "retention_time" may sometimes be empty string instead of an integer
{
rt := s.RetentionTime
if rt == "" {
rt = "0"
}
schemaRetentionTime, err = strconv.ParseInt(rt, 10, 64)
if err != nil {
return diag.FromErr(err)
}
}
tableDescription, err := client.Tables.DescribeColumns(ctx, sdk.NewDescribeTableColumnsRequest(id))
if err != nil {
return diag.FromErr(err)
}
// Set the relevant data in the state
toSet := map[string]interface{}{
"name": table.Name,
"owner": table.Owner,
"database": table.DatabaseName,
"schema": table.SchemaName,
"comment": table.Comment,
"column": toColumnConfig(tableDescription),
"cluster_by": table.GetClusterByKeys(),
"change_tracking": table.ChangeTracking,
}
if v := d.Get("data_retention_time_in_days"); v.(int) != IntDefault || int64(table.RetentionTime) != schemaRetentionTime {
toSet["data_retention_time_in_days"] = table.RetentionTime
}
for key, val := range toSet {
if err := d.Set(key, val); err != nil { // lintignore:R001
return diag.FromErr(err)
}
}
return nil
}
// UpdateTable implements schema.UpdateFunc.
func UpdateTable(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier)
if d.HasChange("name") {
newId := sdk.NewSchemaObjectIdentifierInSchema(id.SchemaId(), d.Get("name").(string))
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithNewName(&newId))
if err != nil {
return diag.FromErr(fmt.Errorf("error renaming table %v err = %w", d.Id(), err))
}
d.SetId(helpers.EncodeSnowflakeID(newId))
id = newId
}
var runSetStatement bool
var runUnsetStatement bool
setRequest := sdk.NewTableSetRequest()
unsetRequest := sdk.NewTableUnsetRequest()
if d.HasChange("comment") {
comment := d.Get("comment").(string)
if comment == "" {
runUnsetStatement = true
unsetRequest.WithComment(true)
} else {
runSetStatement = true
setRequest.WithComment(sdk.String(comment))
}
}
if d.HasChange("change_tracking") {
changeTracking := d.Get("change_tracking").(bool)
runSetStatement = true
setRequest.WithChangeTracking(sdk.Bool(changeTracking))
}
if d.HasChange("data_retention_time_in_days") {
if days := d.Get("data_retention_time_in_days"); days.(int) != IntDefault {
runSetStatement = true
setRequest.WithDataRetentionTimeInDays(sdk.Int(days.(int)))
} else {
runUnsetStatement = true
unsetRequest.WithDataRetentionTimeInDays(true)
}
}
if runSetStatement {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithSet(setRequest))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
}
if runUnsetStatement {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithUnset(unsetRequest))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
}
if d.HasChange("cluster_by") {
cb := expandStringList(d.Get("cluster_by").([]interface{}))
if len(cb) != 0 {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithClusteringAction(sdk.NewTableClusteringActionRequest().WithClusterBy(cb)))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
} else {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithClusteringAction(sdk.NewTableClusteringActionRequest().WithDropClusteringKey(sdk.Bool(true))))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
}
}
if d.HasChange("column") {
t, n := d.GetChange("column")
removed, added, changed := getColumns(t).diffs(getColumns(n))
if len(removed) > 0 {
removedColumnNames := make([]string, len(removed))
for i, r := range removed {
removedColumnNames[i] = r.name
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithDropColumns(snowflake.QuoteStringList(removedColumnNames))))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
}
for _, cA := range added {
addRequest := sdk.NewTableColumnAddActionRequest(fmt.Sprintf("\"%s\"", cA.name), sdk.DataType(cA.dataType)).
WithInlineConstraint(sdk.NewTableColumnAddInlineConstraintRequest().WithNotNull(sdk.Bool(!cA.nullable)))
if cA._default != nil {
if cA._default._type() != "constant" {
return diag.FromErr(fmt.Errorf("failed to add column %v => Only adding a column as a constant is supported by Snowflake", cA.name))
}
var expression string
if sdk.IsStringType(cA.dataType) {
expression = snowflake.EscapeSnowflakeString(*cA._default.constant)
} else {
expression = *cA._default.constant
}
addRequest.WithDefaultValue(sdk.NewColumnDefaultValueRequest().WithExpression(sdk.String(expression)))
}
if cA.identity != nil {
addRequest.WithDefaultValue(sdk.NewColumnDefaultValueRequest().WithIdentity(sdk.NewColumnIdentityRequest(cA.identity.startNum, cA.identity.stepNum)))
}
if cA.maskingPolicy != "" {
addRequest.WithMaskingPolicy(sdk.NewColumnMaskingPolicyRequest(sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(cA.maskingPolicy)))
}
if cA.comment != "" {
addRequest.WithComment(sdk.String(cA.comment))
}
if cA.collate != "" && sdk.IsStringType(cA.dataType) {
addRequest.WithCollate(sdk.String(cA.collate))
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAdd(addRequest)))
if err != nil {
return diag.FromErr(fmt.Errorf("error adding column: %w", err))
}
}
for _, cA := range changed {
if cA.changedDataType || cA.changedCollate {
var newCollation *string
if sdk.IsStringType(cA.newColumn.dataType) && cA.newColumn.collate != "" {
newCollation = sdk.String(cA.newColumn.collate)
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAlter([]sdk.TableColumnAlterActionRequest{*sdk.NewTableColumnAlterActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name)).WithType(sdk.Pointer(sdk.DataType(cA.newColumn.dataType))).WithCollate(newCollation)})))
if err != nil {
return diag.FromErr(fmt.Errorf("error changing property on %v: err %w", d.Id(), err))
}
}
if cA.changedNullConstraint {
nullabilityRequest := sdk.NewTableColumnNotNullConstraintRequest()
if !cA.newColumn.nullable {
nullabilityRequest.WithSet(sdk.Bool(true))
} else {
nullabilityRequest.WithDrop(sdk.Bool(true))
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAlter([]sdk.TableColumnAlterActionRequest{*sdk.NewTableColumnAlterActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name)).WithNotNullConstraint(nullabilityRequest)})))
if err != nil {
return diag.FromErr(fmt.Errorf("error changing property on %v: err %w", d.Id(), err))
}
}
if cA.dropedDefault {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAlter([]sdk.TableColumnAlterActionRequest{*sdk.NewTableColumnAlterActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name)).WithDropDefault(sdk.Bool(true))})))
if err != nil {
return diag.FromErr(fmt.Errorf("error changing property on %v: err %w", d.Id(), err))
}
}
if cA.changedComment {
columnAlterActionRequest := sdk.NewTableColumnAlterActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name))
if cA.newColumn.comment == "" {
columnAlterActionRequest.WithUnsetComment(sdk.Bool(true))
} else {
columnAlterActionRequest.WithComment(sdk.String(cA.newColumn.comment))
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(sdk.NewTableColumnActionRequest().WithAlter([]sdk.TableColumnAlterActionRequest{*columnAlterActionRequest})))
if err != nil {
return diag.FromErr(fmt.Errorf("error changing property on %v: err %w", d.Id(), err))
}
}
if cA.changedMaskingPolicy {
columnAction := sdk.NewTableColumnActionRequest()
if strings.TrimSpace(cA.newColumn.maskingPolicy) == "" {
columnAction.WithUnsetMaskingPolicy(sdk.NewTableColumnAlterUnsetMaskingPolicyActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name)))
} else {
columnAction.WithSetMaskingPolicy(sdk.NewTableColumnAlterSetMaskingPolicyActionRequest(fmt.Sprintf("\"%s\"", cA.newColumn.name), sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(cA.newColumn.maskingPolicy), []string{}).WithForce(sdk.Bool(true)))
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithColumnAction(columnAction))
if err != nil {
return diag.FromErr(fmt.Errorf("error changing property on %v: err %w", d.Id(), err))
}
}
}
}
if d.HasChange("primary_key") {
o, n := d.GetChange("primary_key")
newKey := getPrimaryKey(n)
oldKey := getPrimaryKey(o)
if len(oldKey.keys) > 0 || len(newKey.keys) == 0 {
// drop our pk if there was an old primary key, or pk has been removed
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithConstraintAction(
sdk.NewTableConstraintActionRequest().
WithDrop(sdk.NewTableConstraintDropActionRequest().WithPrimaryKey(sdk.Bool(true))),
))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
}
if len(newKey.keys) > 0 {
constraint := sdk.NewOutOfLineConstraintRequest(sdk.ColumnConstraintTypePrimaryKey).WithColumns(snowflake.QuoteStringList(newKey.keys))
if newKey.name != "" {
constraint.WithName(sdk.String(newKey.name))
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithConstraintAction(
sdk.NewTableConstraintActionRequest().WithAdd(constraint),
))
if err != nil {
return diag.FromErr(fmt.Errorf("error updating table: %w", err))
}
}
}
if d.HasChange("tag") {
unsetTags, setTags := GetTagsDiff(d, "tag")
if len(unsetTags) > 0 {
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithUnsetTags(unsetTags))
if err != nil {
return diag.FromErr(fmt.Errorf("error setting tags on %v, err = %w", d.Id(), err))
}
}
if len(setTags) > 0 {
tagAssociationRequests := make([]sdk.TagAssociationRequest, len(setTags))
for i, t := range setTags {
tagAssociationRequests[i] = *sdk.NewTagAssociationRequest(t.Name, t.Value)
}
err := client.Tables.Alter(ctx, sdk.NewAlterTableRequest(id).WithSetTags(tagAssociationRequests))
if err != nil {
return diag.FromErr(fmt.Errorf("error setting tags on %v, err = %w", d.Id(), err))
}
}
}
return ReadTable(ctx, d, meta)
}
// DeleteTable implements schema.DeleteFunc.
func DeleteTable(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
id := helpers.DecodeSnowflakeID(d.Id()).(sdk.SchemaObjectIdentifier)
err := client.Tables.Drop(ctx, sdk.NewDropTableRequest(id))
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}