Skip to content

Commit

Permalink
Make new column nullable by default (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelewski authored Aug 21, 2024
1 parent 8a0ce2c commit 6649665
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions quesma/clickhouse/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func TestAlterTable(t *testing.T) {
"{\"attributes_string_key\":[],\"attributes_string_type\":[],\"attributes_string_value\":[],\"Test1\":1,\"Test2\":2}",
}
alters := []string{
"ALTER TABLE \"\" ADD COLUMN IF NOT EXISTS \"Test1\" Int64",
"ALTER TABLE \"\" ADD COLUMN IF NOT EXISTS \"Test2\" Int64",
"ALTER TABLE \"\" ADD COLUMN IF NOT EXISTS \"Test1\" Nullable(Int64)",
"ALTER TABLE \"\" ADD COLUMN IF NOT EXISTS \"Test2\" Nullable(Int64)",
}
columns := []string{"Test1", "Test2"}
table := &Table{
Cols: map[string]*Column{},
}
Expand All @@ -49,6 +50,15 @@ func TestAlterTable(t *testing.T) {
assert.Equal(t, alters[i], alter[0])
// Table will grow with each iteration
assert.Equal(t, i+1, len(table.Cols))
for _, col := range columns[:i+1] {
_, ok := table.Cols[col]
assert.True(t, ok)
}
for k, col := range table.Cols {
assert.Equal(t, k, col.Name)
assert.Equal(t, "Nullable", col.Modifiers)
}

assert.NoError(t, err)
}
}
4 changes: 2 additions & 2 deletions quesma/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ func (lm *LogManager) generateNewColumns(
attrTypes := getAttributesByArrayName(AttributesValueType, attrsMap)
var deleteIndexes []int
for i := 0; i < len(attrKeys); i++ {
alterTable := fmt.Sprintf("ALTER TABLE \"%s\" ADD COLUMN IF NOT EXISTS \"%s\" %s", table.Name, attrKeys[i], attrTypes[i])
table.Cols[attrKeys[i]] = &Column{Name: attrKeys[i], Type: NewBaseType(attrTypes[i])}
alterTable := fmt.Sprintf("ALTER TABLE \"%s\" ADD COLUMN IF NOT EXISTS \"%s\" Nullable(%s)", table.Name, attrKeys[i], attrTypes[i])
table.Cols[attrKeys[i]] = &Column{Name: attrKeys[i], Type: NewBaseType(attrTypes[i]), Modifiers: "Nullable"}
alterCmd = append(alterCmd, alterTable)
deleteIndexes = append(deleteIndexes, i)
}
Expand Down

0 comments on commit 6649665

Please sign in to comment.