From 6649665e2091b38c4df8714f23a5e180c3ecef73 Mon Sep 17 00:00:00 2001 From: Przemyslaw Delewski <102958445+pdelewski@users.noreply.github.com> Date: Wed, 21 Aug 2024 07:18:50 +0200 Subject: [PATCH] Make new column nullable by default (#655) --- quesma/clickhouse/alter_table_test.go | 14 ++++++++++++-- quesma/clickhouse/clickhouse.go | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/quesma/clickhouse/alter_table_test.go b/quesma/clickhouse/alter_table_test.go index e87567a36..fab9c58d4 100644 --- a/quesma/clickhouse/alter_table_test.go +++ b/quesma/clickhouse/alter_table_test.go @@ -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{}, } @@ -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) } } diff --git a/quesma/clickhouse/clickhouse.go b/quesma/clickhouse/clickhouse.go index b0ef40051..719248c4f 100644 --- a/quesma/clickhouse/clickhouse.go +++ b/quesma/clickhouse/clickhouse.go @@ -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) }