From 5d9a6248c7c421c5a13ebd9ac73b4805b1d3dc9f Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 15 Oct 2024 20:06:34 -0700 Subject: [PATCH] Clean up. --- lib/typing/columns/columns.go | 13 +++++ lib/typing/columns/columns_test.go | 79 ++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/lib/typing/columns/columns.go b/lib/typing/columns/columns.go index 7818c8004..4d29ae82b 100644 --- a/lib/typing/columns/columns.go +++ b/lib/typing/columns/columns.go @@ -129,6 +129,15 @@ func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) error { } if arg.StringPrecision != nil { + var currentPrecision int32 + if col.KindDetails.OptionalStringPrecision != nil { + currentPrecision = *col.KindDetails.OptionalStringPrecision + } + + if currentPrecision > *arg.StringPrecision { + return fmt.Errorf("cannot decrease string precision from %d to %d", currentPrecision, *arg.StringPrecision) + } + col.KindDetails.OptionalStringPrecision = arg.StringPrecision } @@ -151,6 +160,10 @@ func (c *Columns) UpsertColumn(colName string, arg UpsertColumnArg) error { _col.backfilled = *arg.Backfilled } + if arg.StringPrecision != nil { + _col.KindDetails.OptionalStringPrecision = arg.StringPrecision + } + c.AddColumn(_col) } diff --git a/lib/typing/columns/columns_test.go b/lib/typing/columns/columns_test.go index 32bd0cb5e..33a65830c 100644 --- a/lib/typing/columns/columns_test.go +++ b/lib/typing/columns/columns_test.go @@ -192,9 +192,9 @@ func TestColumns_UpsertColumns(t *testing.T) { { // Now update a and b to be toast columns for _, key := range []string{"a", "b"} { - cols.UpsertColumn(key, UpsertColumnArg{ + assert.NoError(t, cols.UpsertColumn(key, UpsertColumnArg{ ToastCol: typing.ToPtr(true), - }) + })) // Now inspect. col, _ := cols.GetColumn(key) @@ -202,19 +202,70 @@ func TestColumns_UpsertColumns(t *testing.T) { } } { - // Play with string precision - cols.UpsertColumn("string_precision_a", UpsertColumnArg{}) - - colA, _ := cols.GetColumn("string_precision_a") - assert.Nil(t, colA.KindDetails.OptionalStringPrecision) - - cols.UpsertColumn("string_precision_a", UpsertColumnArg{StringPrecision: typing.ToPtr(int32(55))}) - colA, _ = cols.GetColumn("string_precision_a") - assert.Equal(t, int32(55), *colA.KindDetails.OptionalStringPrecision) + // Increase string precision + { + // Valid - Current column does not have string precision set + assert.NoError(t, cols.UpsertColumn("string_precision_a", UpsertColumnArg{})) + + colA, _ := cols.GetColumn("string_precision_a") + assert.Nil(t, colA.KindDetails.OptionalStringPrecision) + + assert.NoError(t, + cols.UpsertColumn("string_precision_a", + UpsertColumnArg{ + StringPrecision: typing.ToPtr(int32(55)), + }, + ), + ) + colA, _ = cols.GetColumn("string_precision_a") + assert.Equal(t, int32(55), *colA.KindDetails.OptionalStringPrecision) + } + { + // Valid - Current column does have string precision set (but it's less) + assert.NoError(t, + cols.UpsertColumn("string_precision_b", + UpsertColumnArg{ + StringPrecision: typing.ToPtr(int32(5)), + }, + ), + ) + + colB, _ := cols.GetColumn("string_precision_b") + assert.Equal(t, int32(5), *colB.KindDetails.OptionalStringPrecision) + assert.NoError(t, + cols.UpsertColumn("string_precision_b", + UpsertColumnArg{ + StringPrecision: typing.ToPtr(int32(100)), + }, + ), + ) + + colB, _ = cols.GetColumn("string_precision_b") + assert.Equal(t, int32(100), *colB.KindDetails.OptionalStringPrecision) + } + { + // Invalid - Cannot decrease string precision + assert.NoError(t, + cols.UpsertColumn("string_precision_b", + UpsertColumnArg{ + StringPrecision: typing.ToPtr(int32(500)), + }, + ), + ) + + assert.ErrorContains(t, + cols.UpsertColumn("string_precision_b", + UpsertColumnArg{ + StringPrecision: typing.ToPtr(int32(100)), + }, + ), + "cannot decrease string precision from 500 to 100", + ) + } } { // Create a new column zzz - cols.UpsertColumn("zzz", UpsertColumnArg{}) + assert.NoError(t, cols.UpsertColumn("zzz", UpsertColumnArg{})) zzzCol, _ := cols.GetColumn("zzz") assert.False(t, zzzCol.ToastColumn) assert.False(t, zzzCol.primaryKey) @@ -222,10 +273,10 @@ func TestColumns_UpsertColumns(t *testing.T) { } { // Create a new column aaa - cols.UpsertColumn("aaa", UpsertColumnArg{ + assert.NoError(t, cols.UpsertColumn("aaa", UpsertColumnArg{ ToastCol: typing.ToPtr(true), PrimaryKey: typing.ToPtr(true), - }) + })) aaaCol, _ := cols.GetColumn("aaa") assert.True(t, aaaCol.ToastColumn) assert.True(t, aaaCol.primaryKey)