Fix NULL default bug when merging branches with different schemas

This commit is contained in:
Neil Macneale IV
2025-04-28 15:42:56 -07:00
parent bdf9caf02e
commit 82fc5c94af
2 changed files with 36 additions and 1 deletions
@@ -644,6 +644,41 @@ var SchemaChangeTestsBasicCases = []MergeScriptTest{
},
},
},
{
// One branch adds a new column with NULL as default
// Other branch has new rows which need to be migrated.
// Created values are NULL, not "NULL".
Name: "creating new column to replace ancestor column",
AncSetUpScript: []string{
"CREATE table t (pk int primary key);",
"INSERT into t values (1), (2);",
},
RightSetUpScript: []string{
"ALTER TABLE t ADD new_col LONGTEXT DEFAULT NULL",
"INSERT INTO t VALUES (3, 'three'), (4, 'four');",
},
LeftSetUpScript: []string{
// Put rows on main which are not in the right branch.
"INSERT INTO t VALUES (5), (6)",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "call dolt_merge('right');",
Expected: []sql.Row{{doltCommit, 0, 0, "merge successful"}},
},
{
Query: "select * from t;",
Expected: []sql.Row{
{1, nil},
{2, nil},
{3, "three"},
{4, "four"},
{5, nil},
{6, nil},
},
},
},
},
}
var SchemaChangeTestsCollations = []MergeScriptTest{
@@ -193,7 +193,7 @@ func GenerateCreateTableIndentedColumnDefinition(col schema.Column, tableCollati
var defaultVal, genVal, onUpdateVal *sql.ColumnDefaultValue
if col.Default != "" {
// hacky way to determine if column default is an expression
if col.Default[0] != '(' && col.Default[len(col.Default)-1] != ')' && col.Default[0] != '\'' && col.Default[len(col.Default)-1] != '\'' {
if col.Default[0] != '(' && col.Default[len(col.Default)-1] != ')' && col.Default[0] != '\'' && col.Default[len(col.Default)-1] != '\'' && col.Default != "NULL" {
col.Default = fmt.Sprintf("'%s'", col.Default)
}
defaultVal = sql.NewUnresolvedColumnDefaultValue(col.Default)