Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Empty lines or tabs inside SQL query of view definition will trigger redeployment of databricks_sql_table resource #3865 #4003

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions catalog/resource_sql_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -369,6 +370,7 @@ func (ti *SqlTableInfo) diff(oldti *SqlTableInfo) ([]string, error) {

if ti.TableType == "VIEW" {
// View only attributes
formatViewDefinition(ti)
if ti.ViewDefinition != oldti.ViewDefinition {
statements = append(statements, fmt.Sprintf("ALTER VIEW %s AS %s", ti.SQLFullName(), ti.ViewDefinition))
}
Expand Down Expand Up @@ -408,6 +410,19 @@ func (ti *SqlTableInfo) diff(oldti *SqlTableInfo) ([]string, error) {
return statements, nil
}

// formatViewDefinition removes empty lines and changes tabs to 4 spaces
// in order to compare view definitions correctly
func formatViewDefinition(ti *SqlTableInfo) {

// remove empty lines
// 1\n\n\n2 => 1\n2
ti.ViewDefinition = regexp.MustCompile(`[\r\n]+`).ReplaceAllString(ti.ViewDefinition, "\n")

// change tab to 4 spaces
// 1\t2 => 1 2
ti.ViewDefinition = strings.ReplaceAll(ti.ViewDefinition, "\t", " ")
}

func (ti *SqlTableInfo) updateTable(oldti *SqlTableInfo) error {
statements, err := ti.diff(oldti)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions catalog/resource_sql_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1710,3 +1710,17 @@ func TestSqlTablesAPI_getTable_OptionsAndParametersProcessedCorrectly(t *testing
})
}
}

func TestFormatViewDefinition(t *testing.T) {
ti := &SqlTableInfo{
ViewDefinition: "SELECT\n\n\n\t*\nFROM\n\tmy_table\nWHERE\n\tcolumn = 'value'",
}

expected := "SELECT\n *\nFROM\n my_table\nWHERE\n column = 'value'"

formatViewDefinition(ti)

if ti.ViewDefinition != expected {
t.Errorf("Expected view definition: %s, but got: %s", expected, ti.ViewDefinition)
}
}
Loading