Skip to content

Commit b91f2aa

Browse files
Add is_primary_key to ConnectorSchemaConfigColumn (#380)
* Add is_primary_key to ConnectorSchemaConfigColumn * Fix mapping * Fix override * Fix typo * Fix tests * Fix tests * Fix typo
1 parent b0c8ed2 commit b91f2aa

10 files changed

+180
-61
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased](https://github.com/fivetran/terraform-provider-fivetran/compare/v1.4.1...HEAD)
8+
## [Unreleased](https://github.com/fivetran/terraform-provider-fivetran/compare/v1.4.2...HEAD)
9+
10+
## [1.4.1](https://github.com/fivetran/terraform-provider-fivetran/compare/v1.4.1...v1.4.2)
11+
12+
## Added
13+
- Add `is_primary_key` to connector schema config
914

1015
## [1.4.1](https://github.com/fivetran/terraform-provider-fivetran/compare/v1.4.0...v1.4.1)
1116

fivetran/framework/core/model/connector_schema_config.go

+36-10
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ func (d *ConnectorSchemaResourceModel) ReadFromResponse(response connectors.Conn
104104

105105
func (d *ConnectorSchemaResourceModel) getNullSchema() basetypes.SetValue {
106106
columnAttrTypes := map[string]attr.Type{
107-
"name": types.StringType,
108-
"enabled": types.BoolType,
109-
"hashed": types.BoolType,
107+
"name": types.StringType,
108+
"enabled": types.BoolType,
109+
"hashed": types.BoolType,
110+
"is_primary_key": types.BoolType,
110111
}
111112
tableAttrTypes := map[string]attr.Type{
112113
"name": types.StringType,
@@ -132,8 +133,9 @@ func (d *ConnectorSchemaResourceModel) getNullSchema() basetypes.SetValue {
132133

133134
func (d *ConnectorSchemaResourceModel) getNullSchemas() basetypes.MapValue {
134135
columnsAttrTypes := map[string]attr.Type{
135-
"enabled": types.BoolType,
136-
"hashed": types.BoolType,
136+
"enabled": types.BoolType,
137+
"hashed": types.BoolType,
138+
"is_primary_key": types.BoolType,
137139
}
138140

139141
tablesAttrTypes := map[string]attr.Type{
@@ -181,8 +183,9 @@ func (d *ConnectorSchemaResourceModel) getSchemasRawValue(schemas []interface{})
181183

182184
func (d *ConnectorSchemaResourceModel) getSchemasMap(schemas []interface{}) basetypes.MapValue {
183185
columnsAttrTypes := map[string]attr.Type{
184-
"enabled": types.BoolType,
185-
"hashed": types.BoolType,
186+
"enabled": types.BoolType,
187+
"hashed": types.BoolType,
188+
"is_primary_key": types.BoolType,
186189
}
187190

188191
tablesAttrTypes := map[string]attr.Type{
@@ -259,6 +262,12 @@ func (d *ConnectorSchemaResourceModel) getSchemasMap(schemas []interface{}) base
259262
} else {
260263
columnElements["hashed"] = types.BoolNull()
261264
}
265+
266+
if _, ok := localColumn["is_primary_key"]; ok && columnMap["is_primary_key"] != nil {
267+
columnElements["is_primary_key"] = types.BoolValue(helpers.StrToBool(columnMap["is_primary_key"].(string)))
268+
} else {
269+
columnElements["is_primary_key"] = types.BoolNull()
270+
}
262271
columnValue, _ := types.ObjectValue(columnsAttrTypes, columnElements)
263272
columns[columnName] = columnValue
264273
}
@@ -310,9 +319,10 @@ func (d *ConnectorSchemaResourceModel) getLegacySchemaItems(schemas []interface{
310319
schemaItems := []attr.Value{}
311320
localSchemas := d.mapLocalSchemas()
312321
columnAttrTypes := map[string]attr.Type{
313-
"name": types.StringType,
314-
"enabled": types.BoolType,
315-
"hashed": types.BoolType,
322+
"name": types.StringType,
323+
"enabled": types.BoolType,
324+
"hashed": types.BoolType,
325+
"is_primary_key": types.BoolType,
316326
}
317327

318328
tableAttrTypes := map[string]attr.Type{
@@ -371,6 +381,11 @@ func (d *ConnectorSchemaResourceModel) getLegacySchemaItems(schemas []interface{
371381
} else {
372382
columnElements["hashed"] = types.BoolNull()
373383
}
384+
if _, ok := localColumn["is_primary_key"]; ok && columnMap["is_primary_key"] != nil {
385+
columnElements["is_primary_key"] = types.BoolValue(helpers.StrToBool(columnMap["is_primary_key"].(string)))
386+
} else {
387+
columnElements["is_primary_key"] = types.BoolNull()
388+
}
374389
columnValue, _ := types.ObjectValue(columnAttrTypes, columnElements)
375390
columns = append(columns, columnValue)
376391
}
@@ -436,6 +451,11 @@ func (d *ConnectorSchemaResourceModel) getLegacySchemas() []interface{} {
436451
if !hashedValue.IsUnknown() && !hashedValue.IsNull() {
437452
column["hashed"] = columnElement.Attributes()["hashed"].(basetypes.BoolValue).ValueBool()
438453
}
454+
455+
isPrimaryKey := columnElement.Attributes()["is_primary_key"].(basetypes.BoolValue)
456+
if !isPrimaryKey.IsUnknown() && !isPrimaryKey.IsNull() {
457+
column["is_primary_key"] = columnElement.Attributes()["is_primary_key"].(basetypes.BoolValue).ValueBool()
458+
}
439459
}
440460
columns = append(columns, column)
441461
}
@@ -509,6 +529,9 @@ func (d *ConnectorSchemaResourceModel) getSchemasRaw() []interface{} {
509529
if h, ok := cMap["hashed"].(bool); ok {
510530
column["hashed"] = h
511531
}
532+
if p, ok := cMap["is_primary_key"].(bool); ok {
533+
column["is_primary_key"] = p
534+
}
512535
columns = append(columns, column)
513536
}
514537
}
@@ -565,6 +588,9 @@ func (d *ConnectorSchemaResourceModel) getSchemas() []interface{} {
565588
if !columnElement.Attributes()["hashed"].(basetypes.BoolValue).IsUnknown() {
566589
column["hashed"] = columnElement.Attributes()["hashed"].(basetypes.BoolValue).ValueBool()
567590
}
591+
if !columnElement.Attributes()["is_primary_key"].(basetypes.BoolValue).IsUnknown() {
592+
column["is_primary_key"] = columnElement.Attributes()["is_primary_key"].(basetypes.BoolValue).ValueBool()
593+
}
568594
columns = append(columns, column)
569595
}
570596
}

fivetran/framework/core/schema/connector_schema_config.go

+9
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ The value defines validation method.
8989
Computed: true,
9090
Description: "The boolean value specifying whether a column should be hashed.",
9191
},
92+
"is_primary_key": schema.BoolAttribute{
93+
Optional: true,
94+
Computed: true,
95+
Description: "",
96+
},
9297
},
9398
},
9499
},
@@ -184,6 +189,10 @@ func getColumnBlock() schema.SetNestedBlock {
184189
Computed: true,
185190
Description: "The boolean value specifying whether a column should be hashed.",
186191
},
192+
"is_primary_key": schema.BoolAttribute{
193+
Optional: true,
194+
Description: "",
195+
},
187196
},
188197
},
189198
}

fivetran/tests/mock/resource_connector_schema_config_logic_test.go

+41-41
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ func TestUpstreamSchemaWithoutColumns(t *testing.T) {
3838
schema_1response.newTable("table_1", false, nil)
3939
// as table_2 stays enabled on switch to BLOCK_ALL - column settings are fetched from source and saved in config
4040
schema_1response.newTable("table_2", true, nil).
41-
newColumn("column_1", true, boolPtr(false)).
42-
newColumn("column_2", true, boolPtr(false))
41+
newColumn("column_1", true, boolPtr(false), true).
42+
newColumn("column_2", true, boolPtr(false), false)
4343
schema_1response.newTable("table_3", false, nil)
4444

4545
schema_2response := responseConfig.newSchema("schema_2", true)
4646
schema_2response.newTable("table_1", false, nil)
4747
// as table_2 stays enabled on switch to BLOCK_ALL - column settings are fetched from source and saved in config
4848
schema_2response.newTable("table_2", true, nil).
49-
newColumn("column_1", true, boolPtr(false)).
50-
newColumn("column_2", true, boolPtr(false))
49+
newColumn("column_1", true, boolPtr(false), true).
50+
newColumn("column_2", true, boolPtr(false), false)
5151
schema_2response.newTable("table_3", false, nil)
5252

5353
body := setupOneStepTest(t, upstreamConfig, tfConfig, responseConfig)
@@ -92,25 +92,25 @@ func TestUpstreamSchemaWithoutColumnsColumnConfigured(t *testing.T) {
9292
// Enable only two tables
9393
tfConfig.newSchema("schema_1", true).
9494
newTable("table_1", true, nil).
95-
newColumn("column_1", true, nil)
95+
newColumn("column_1", true, nil, true)
9696

9797
responseConfig := schemaConfigTestData{
9898
schemaChangeHandling: "BLOCK_ALL",
9999
}
100100

101101
responseConfig.newSchema("schema_1", true).
102102
newTable("table_1", true, nil).
103-
newColumn("column_1", true, boolPtr(false)). // column user configured in tf
104-
newColumn("column_2", true, boolPtr(false)) // column present in source, but not saved to standard config before switch to BA mode
103+
newColumn("column_1", true, boolPtr(false), false). // column user configured in tf
104+
newColumn("column_2", true, boolPtr(false), false) // column present in source, but not saved to standard config before switch to BA mode
105105

106106
response2Config := schemaConfigTestData{
107107
schemaChangeHandling: "BLOCK_ALL",
108108
}
109109

110110
response2Config.newSchema("schema_1", true).
111111
newTable("table_1", true, nil).
112-
newColumn("column_1", true, boolPtr(false)). // column user configured in tf
113-
newColumn("column_2", false, boolPtr(false)) // column set disbled after second patch
112+
newColumn("column_1", true, boolPtr(false), false). // column user configured in tf
113+
newColumn("column_2", false, boolPtr(false), false) // column set disbled after second patch
114114

115115
bodies := setupComplexTestWithColumnsReload(
116116
t, upstreamConfig,
@@ -119,7 +119,7 @@ func TestUpstreamSchemaWithoutColumnsColumnConfigured(t *testing.T) {
119119
map[string]map[string][]columnsConfigTestData{
120120
"schema_1": map[string][]columnsConfigTestData{
121121
"table_1": []columnsConfigTestData{
122-
newColumnConfigTestData().newColumn("column_1", false, boolPtr(false)).newColumn("column_2", false, boolPtr(false)),
122+
newColumnConfigTestData().newColumn("column_1", false, boolPtr(false), true).newColumn("column_2", false, boolPtr(false), false),
123123
},
124124
},
125125
})
@@ -142,7 +142,7 @@ func TestUpstreamSchemaWithoutColumnsColumnConfigured(t *testing.T) {
142142

143143
assertEqual(t, len(columns), 1)
144144
column11 := AssertKeyExists(t, columns, "column_1").(map[string]interface{})
145-
assertEqual(t, len(column11), 1)
145+
assertEqual(t, len(column11), 2)
146146
assertKeyExistsAndHasValue(t, column11, "enabled", true)
147147

148148
body2 := bodies[1]
@@ -160,7 +160,7 @@ func TestUpstreamSchemaWithoutColumnsColumnConfigured(t *testing.T) {
160160
columns = AssertKeyExists(t, table11, "columns").(map[string]interface{})
161161
assertEqual(t, len(columns), 1)
162162
column12 := AssertKeyExists(t, columns, "column_2").(map[string]interface{})
163-
assertEqual(t, len(column12), 1)
163+
assertEqual(t, len(column12), 2)
164164
assertKeyExistsAndHasValue(t, column12, "enabled", false)
165165
}
166166

@@ -173,14 +173,14 @@ func TestSchemaDoesntTouchColumnsInBlockAllIfNoColumnSettingsMock(t *testing.T)
173173
schema_1 := upstreamConfig.newSchema("schema_1", true)
174174

175175
schema_1.newTable("table_1", true, nil).
176-
newColumnLocked("column_1", true, boolPtr(false)).
177-
newColumn("column_2", true, boolPtr(false)).
178-
newColumn("column_3", true, boolPtr(true))
176+
newColumnLocked("column_1", true, boolPtr(false), true).
177+
newColumn("column_2", true, boolPtr(false), false).
178+
newColumn("column_3", true, boolPtr(true), false)
179179

180180
schema_1.newTable("table_2", true, nil).
181-
newColumnLocked("column_1", true, boolPtr(false)).
182-
newColumn("column_2", true, boolPtr(false)).
183-
newColumn("column_3", true, boolPtr(true))
181+
newColumnLocked("column_1", true, boolPtr(false), true).
182+
newColumn("column_2", true, boolPtr(false), false).
183+
newColumn("column_3", true, boolPtr(true), false)
184184

185185
// only schema_1.table_1 will stay enabled
186186
// table_2 will be disabled
@@ -199,15 +199,15 @@ func TestSchemaDoesntTouchColumnsInBlockAllIfNoColumnSettingsMock(t *testing.T)
199199
schema_1response := responseConfig.newSchema("schema_1", true)
200200
// table_1 enabled, existing columns saved settings
201201
schema_1response.newTable("table_1", true, nil).
202-
newColumnLocked("column_1", true, boolPtr(false)).
203-
newColumn("column_2", true, boolPtr(false)).
204-
newColumn("column_3", true, boolPtr(true))
202+
newColumnLocked("column_1", true, boolPtr(false), true).
203+
newColumn("column_2", true, boolPtr(false), false).
204+
newColumn("column_3", true, boolPtr(true), false)
205205

206206
// table_2 enabled, existing columns saved settings
207207
schema_1response.newTable("table_2", false, nil).
208-
newColumnLocked("column_1", true, boolPtr(false)).
209-
newColumn("column_2", true, boolPtr(false)).
210-
newColumn("column_3", true, boolPtr(true))
208+
newColumnLocked("column_1", true, boolPtr(false), true).
209+
newColumn("column_2", true, boolPtr(false), false).
210+
newColumn("column_3", true, boolPtr(true), false)
211211

212212
// act
213213
body := setupOneStepTest(t, upstreamConfig, tfConfig, responseConfig)
@@ -231,9 +231,9 @@ func TestSetupSchemaBlockAllMock(t *testing.T) {
231231
schema_1 := upstreamConfig.newSchema("schema_1", true)
232232

233233
schema_1.newTable("table_1", true, nil).
234-
newColumnLocked("column_1", true, boolPtr(false)).
235-
newColumn("column_2", true, boolPtr(false)).
236-
newColumn("column_3", true, boolPtr(true))
234+
newColumnLocked("column_1", true, boolPtr(false), true).
235+
newColumn("column_2", true, boolPtr(false), false).
236+
newColumn("column_3", true, boolPtr(true), false)
237237

238238
schema_1.newTableLocked("table_locked", true, nil)
239239

@@ -243,17 +243,17 @@ func TestSetupSchemaBlockAllMock(t *testing.T) {
243243

244244
tfConfig.newSchema("schema_1", true).
245245
newTable("table_1", true, nil).
246-
newColumn("column_2", true, nil)
246+
newColumn("column_2", true, nil, true)
247247

248248
responseConfig := schemaConfigTestData{
249249
schemaChangeHandling: "BLOCK_ALL",
250250
}
251251

252252
schema_1response := responseConfig.newSchema("schema_1", true)
253253
schema_1response.newTable("table_1", true, nil).
254-
newColumnLocked("column_1", true, boolPtr(false)).
255-
newColumn("column_2", true, boolPtr(false)).
256-
newColumn("column_3", false, boolPtr(true))
254+
newColumnLocked("column_1", true, boolPtr(false), true).
255+
newColumn("column_2", true, boolPtr(false), false).
256+
newColumn("column_3", false, boolPtr(true), false)
257257
schema_1response.newTableLocked("table_locked", true, nil)
258258

259259
// act
@@ -272,7 +272,7 @@ func TestSetupSchemaBlockAllMock(t *testing.T) {
272272

273273
assertEqual(t, len(columns), 1)
274274
column3 := assertKeyExists(t, columns, "column_3").(map[string]interface{})
275-
assertEqual(t, len(column3), 1)
275+
assertEqual(t, len(column3), 2)
276276
assertKeyExistsAndHasValue(t, column3, "enabled", false)
277277
}
278278

@@ -282,26 +282,26 @@ func TestIgnoreNoPatchAllowedColumnsMock(t *testing.T) {
282282
}
283283
upstreamConfig.newSchema("schema_1", true).
284284
newTable("table_1", true, nil).
285-
newColumn("column_1", true, boolPtr(false)).
286-
newColumn("column_2", true, boolPtr(false)).
287-
newColumn("column_3", true, boolPtr(true))
285+
newColumn("column_1", true, boolPtr(false), true).
286+
newColumn("column_2", true, boolPtr(false), false).
287+
newColumn("column_3", true, boolPtr(true), false)
288288

289289
tfConfig := schemaConfigTestData{
290290
schemaChangeHandling: "BLOCK_ALL",
291291
}
292292

293293
tfConfig.newSchema("schema_1", true).
294294
newTable("table_1", true, nil).
295-
newColumn("column_2", true, nil)
295+
newColumn("column_2", true, nil, true)
296296

297297
responseConfig := schemaConfigTestData{
298298
schemaChangeHandling: "BLOCK_ALL",
299299
}
300300
responseConfig.newSchema("schema_1", true).
301301
newTable("table_1", true, nil).
302-
newColumn("column_1", false, boolPtr(false)).
303-
newColumn("column_2", true, boolPtr(false)).
304-
newColumn("column_3", false, boolPtr(true))
302+
newColumn("column_1", false, boolPtr(false), true).
303+
newColumn("column_2", true, boolPtr(false), false).
304+
newColumn("column_3", false, boolPtr(true), false)
305305

306306
// act
307307
body := setupOneStepTest(t, upstreamConfig, tfConfig, responseConfig)
@@ -317,10 +317,10 @@ func TestIgnoreNoPatchAllowedColumnsMock(t *testing.T) {
317317
columns := assertKeyExists(t, table_1, "columns").(map[string]interface{})
318318
assertEqual(t, len(columns), 2)
319319
column_1 := assertKeyExists(t, columns, "column_1").(map[string]interface{})
320-
assertEqual(t, len(column_1), 1)
320+
assertEqual(t, len(column_1), 2)
321321
assertKeyExistsAndHasValue(t, column_1, "enabled", false)
322322
column_3 := assertKeyExists(t, columns, "column_3").(map[string]interface{})
323-
assertEqual(t, len(column_3), 1)
323+
assertEqual(t, len(column_3), 2)
324324
assertKeyExistsAndHasValue(t, column_3, "enabled", false)
325325
}
326326

0 commit comments

Comments
 (0)