From ee67f37189e15857890c27f19253cda2f24c81b6 Mon Sep 17 00:00:00 2001 From: Jason Fulghum Date: Fri, 28 Mar 2025 13:56:40 -0700 Subject: [PATCH] Changing how a constraint addition is structured, to match what Dolt/GMS does --- server/ast/alter_table.go | 14 ++------------ testing/go/alter_table_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/server/ast/alter_table.go b/server/ast/alter_table.go index f52706c4c6..96349ccd10 100644 --- a/server/ast/alter_table.go +++ b/server/ast/alter_table.go @@ -99,19 +99,9 @@ func nodeAlterTableCmds( } vitessDdlCmds = append(vitessDdlCmds, statement) - // Postgres (unlike MySQL) allows an inline FK constraint - // when altering a table to add a column. GMS doesn't support - // this directly in the ALTER TABLE ADD COLUMN DDL command, - // so we break this out into a separate DDL command. + // If inline constraints have been specified, set the ConstraintAction so that they get processed if len(statement.TableSpec.Constraints) > 0 { - vitessDdlCmds = append(vitessDdlCmds, - &vitess.DDL{ - Action: "alter", - ConstraintAction: "add", - Table: tableName, - IfExists: ifExists, - TableSpec: statement.TableSpec, - }) + statement.ConstraintAction = vitess.AddStr } case *tree.AlterTableDropColumn: diff --git a/testing/go/alter_table_test.go b/testing/go/alter_table_test.go index 3fc653a375..c4dc145727 100644 --- a/testing/go/alter_table_test.go +++ b/testing/go/alter_table_test.go @@ -303,6 +303,22 @@ func TestAlterTable(t *testing.T) { }, }, }, + { + Name: "Add column with inline check constraint", + SetUpScript: []string{ + "CREATE TABLE test1 (a INT, b INT);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "ALTER TABLE test1 ADD COLUMN c INT NOT NULL DEFAULT 42 CONSTRAINT chk1 CHECK (c > 0);", + Expected: []sql.Row{}, + }, + { + Query: "INSERT INTO test1 VALUES (2, 2, -2);", + ExpectedErr: `Check constraint "chk1" violated`, + }, + }, + }, { Name: "Drop Column", SetUpScript: []string{