Skip to content

Commit 93f131c

Browse files
authored
Added support for popular column types for resource_sql_table (#3528)
* catalog suppress diff * - * - * - * - * set computed * Update docs * - * - * - * -
1 parent 0908416 commit 93f131c

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

catalog/resource_sql_table.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var MaxSqlExecWaitTimeout = 50
2222

2323
type SqlColumnInfo struct {
2424
Name string `json:"name"`
25-
Type string `json:"type_text,omitempty" tf:"suppress_diff,alias:type"`
25+
Type string `json:"type_text,omitempty" tf:"alias:type,computed"`
2626
Comment string `json:"comment,omitempty"`
2727
Nullable bool `json:"nullable,omitempty" tf:"default:true"`
2828
}
@@ -488,10 +488,29 @@ func columnChangesCustomizeDiff(d *schema.ResourceDiff, newTable *SqlTableInfo)
488488
return nil
489489
}
490490

491+
var columnTypeAliases = map[string]string{
492+
"integer": "int",
493+
"long": "bigint",
494+
"real": "float",
495+
"short": "smallint",
496+
"byte": "tinyint",
497+
"decimal": "decimal(10,0)",
498+
"dec": "decimal(10,0)",
499+
"numeric": "decimal(10,0)",
500+
}
501+
502+
func getColumnType(columnType string) string {
503+
caseInsensitiveColumnType := strings.ToLower(columnType)
504+
if alias, ok := columnTypeAliases[caseInsensitiveColumnType]; ok {
505+
return alias
506+
}
507+
return caseInsensitiveColumnType
508+
}
509+
491510
func assertNoColumnTypeDiff(oldCols []interface{}, newColumnInfos []SqlColumnInfo) error {
492511
for i, oldCol := range oldCols {
493512
oldColMap := oldCol.(map[string]interface{})
494-
if oldColMap["type"] != newColumnInfos[i].Type {
513+
if getColumnType(oldColMap["type"].(string)) != getColumnType(newColumnInfos[i].Type) {
495514
return fmt.Errorf("changing the 'type' of an existing column is not supported")
496515
}
497516
}
@@ -511,7 +530,7 @@ func assertNoColumnMembershipAndFieldValueUpdate(oldCols []interface{}, newColum
511530
}
512531
for name, oldColMap := range oldColsNameToMap {
513532
if newCol, exists := newColsNameToMap[name]; exists {
514-
if oldColMap["type"] != newCol.Type || oldColMap["nullable"] != newCol.Nullable || oldColMap["comment"] != newCol.Comment {
533+
if getColumnType(oldColMap["type"].(string)) != getColumnType(newCol.Type) || oldColMap["nullable"] != newCol.Nullable || oldColMap["comment"] != newCol.Comment {
515534
return fmt.Errorf("detected changes in both number of columns and existing column field values, please do not change number of columns and update column values at the same time")
516535
}
517536
}
@@ -540,6 +559,9 @@ func ResourceSqlTable() common.Resource {
540559

541560
s["partitions"].ConflictsWith = []string{"cluster_keys"}
542561
s["cluster_keys"].ConflictsWith = []string{"partitions"}
562+
common.MustSchemaPath(s, "column", "type").DiffSuppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
563+
return getColumnType(old) == getColumnType(new)
564+
}
543565
return s
544566
})
545567
return common.Resource{

internal/acceptance/sql_table_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"regexp"
7+
"strconv"
78
"testing"
89

910
"github.com/databricks/terraform-provider-databricks/catalog"
@@ -317,6 +318,61 @@ func TestUcAccResourceSqlTable_RenameColumn(t *testing.T) {
317318
})
318319
}
319320

321+
func constructManagedSqlTableTemplateWithColumnTypeUpdates(tableName string, columnName string, step string, columnTypes []string) string {
322+
colInfos := []catalog.SqlColumnInfo{}
323+
for index, colType := range columnTypes {
324+
colInfos = append(colInfos, catalog.SqlColumnInfo{
325+
Name: columnName + strconv.Itoa(index),
326+
Type: colType,
327+
Nullable: true,
328+
Comment: "comment" + strconv.Itoa(index) + step,
329+
})
330+
}
331+
return constructManagedSqlTableTemplate(tableName, colInfos)
332+
}
333+
334+
func TestUcAccResourceSqlTable_ColumnTypeSuppressDiff(t *testing.T) {
335+
if os.Getenv("GOOGLE_CREDENTIALS") != "" {
336+
skipf(t)("databricks_sql_table resource not available on GCP")
337+
}
338+
tableName := RandomName()
339+
columnName := RandomName()
340+
unityWorkspaceLevel(t, step{
341+
Template: constructManagedSqlTableTemplateWithColumnTypeUpdates(tableName, columnName, "0", []string{
342+
"integer",
343+
"long",
344+
"real",
345+
"short",
346+
"byte",
347+
"decimal",
348+
"dec",
349+
"numeric",
350+
}),
351+
}, step{
352+
Template: constructManagedSqlTableTemplateWithColumnTypeUpdates(tableName, columnName, "1", []string{
353+
"INTEGER",
354+
"LONG",
355+
"REAL",
356+
"SHORT",
357+
"BYTE",
358+
"DECIMAL",
359+
"DEC",
360+
"NUMERIC",
361+
}),
362+
}, step{
363+
Template: constructManagedSqlTableTemplateWithColumnTypeUpdates(tableName, columnName, "2", []string{
364+
"int",
365+
"bigint",
366+
"float",
367+
"smallint",
368+
"tinyint",
369+
"decimal(10,0)",
370+
"decimal(10,0)",
371+
"decimal(10,0)",
372+
}),
373+
})
374+
}
375+
320376
func TestUcAccResourceSqlTable_AddColumnComment(t *testing.T) {
321377
if os.Getenv("GOOGLE_CREDENTIALS") != "" {
322378
skipf(t)("databricks_sql_table resource not available on GCP")

0 commit comments

Comments
 (0)