From dab974cd5bfd3be82be5e82326fe74bb9aec6843 Mon Sep 17 00:00:00 2001 From: Nick Tobey Date: Wed, 10 May 2023 15:19:07 -0700 Subject: [PATCH 1/4] Generate per-table names for constraint checks. --- go/libraries/doltcore/sqle/tables.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/libraries/doltcore/sqle/tables.go b/go/libraries/doltcore/sqle/tables.go index 8e24beb2dd..5b71a957a8 100644 --- a/go/libraries/doltcore/sqle/tables.go +++ b/go/libraries/doltcore/sqle/tables.go @@ -2541,7 +2541,7 @@ func (t *AlterableDoltTable) generateCheckName(ctx *sql.Context, check *sql.Chec bb.Write([]byte(check.CheckExpression)) hash := hash.Of(bb.Bytes()) - hashedName := fmt.Sprintf("chk_%s", hash.String()[:8]) + hashedName := fmt.Sprintf("%s_chk_%s", t.tableName, hash.String()[:8]) name := hashedName var i int From 630a53b68ff88af1f9214bf4ef7ae28fb2753bf3 Mon Sep 17 00:00:00 2001 From: Nick Tobey Date: Wed, 10 May 2023 15:19:21 -0700 Subject: [PATCH 2/4] Add bats test for per-table constraint check names. --- integration-tests/bats/sql-create-tables.bats | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/integration-tests/bats/sql-create-tables.bats b/integration-tests/bats/sql-create-tables.bats index e86918c8ef..e607201c6f 100644 --- a/integration-tests/bats/sql-create-tables.bats +++ b/integration-tests/bats/sql-create-tables.bats @@ -776,3 +776,18 @@ SQL dolt sql -q "INSERT INTO budgets VALUES (UUID());" dolt sql -q "INSERT INTO budgets2 VALUES (UUID());" } + +@test "sql-create-tables: tables should not reuse constraint names" { + run dolt sql -r csv < 0) +); +CREATE TABLE t2 LIKE t1; +SELECT count(CONSTRAINT_NAME), count(distinct CONSTRAINT_NAME) FROM information_schema.table_constraints WHERE CONSTRAINT_TYPE="CHECK"; +SQL + + [ "$status" -eq 0 ] + [[ "$output" =~ "2,2" ]] || false + +} \ No newline at end of file From 2953275da9d26e5c4bdbe76f775a0d5e67034e84 Mon Sep 17 00:00:00 2001 From: Nick Tobey Date: Thu, 11 May 2023 16:47:48 -0700 Subject: [PATCH 3/4] Update tests. --- go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go | 2 +- integration-tests/bats/sql-check-constraints.bats | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go b/go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go index bf5ec972e0..186a84c956 100644 --- a/go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go +++ b/go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go @@ -3393,7 +3393,7 @@ var PatchTableFunctionScriptTests = []queries.ScriptTest{ Assertions: []queries.ScriptTestAssertion{ { Query: "SELECT statement_order, table_name, diff_type, statement FROM dolt_patch('HEAD', 'WORKING')", - Expected: []sql.Row{{1, "foo", "schema", "CREATE TABLE `foo` (\n `pk` int NOT NULL,\n `c1` int,\n PRIMARY KEY (`pk`),\n CONSTRAINT `chk_eq3jn5ra` CHECK ((c1 > 3))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;"}}, + Expected: []sql.Row{{1, "foo", "schema", "CREATE TABLE `foo` (\n `pk` int NOT NULL,\n `c1` int,\n PRIMARY KEY (`pk`),\n CONSTRAINT `foo_chk_eq3jn5ra` CHECK ((c1 > 3))\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;"}}, }, }, }, diff --git a/integration-tests/bats/sql-check-constraints.bats b/integration-tests/bats/sql-check-constraints.bats index 425b7e9f35..e9758887ed 100644 --- a/integration-tests/bats/sql-check-constraints.bats +++ b/integration-tests/bats/sql-check-constraints.bats @@ -81,7 +81,7 @@ SQL # check information_schema.CHECK_CONSTRAINTS table run dolt sql -q "select constraint_catalog, constraint_name, check_clause from information_schema.CHECK_CONSTRAINTS;" -r csv - [[ "$output" =~ "def,chk_eq3jn5ra,(c1 > 3)" ]] || false + [[ "$output" =~ "def,foo_chk_eq3jn5ra,(c1 > 3)" ]] || false } @test "sql-check-constraints: check constraints survive renaming a column" { From 725b148a78f899673da23efddc23a9dea75ac3be Mon Sep 17 00:00:00 2001 From: Nick Tobey Date: Fri, 12 May 2023 12:18:19 -0700 Subject: [PATCH 4/4] Update `drop-create.bats` to use new generated constraint name. --- integration-tests/bats/drop-create.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/bats/drop-create.bats b/integration-tests/bats/drop-create.bats index f8cc09657e..3923465cca 100755 --- a/integration-tests/bats/drop-create.bats +++ b/integration-tests/bats/drop-create.bats @@ -332,7 +332,7 @@ SQL [[ "$output" =~ "+ \`b\` tinyint NOT NULL," ]] || false [[ "$output" =~ "+ \`c\` varchar(10)," ]] || false [[ "$output" =~ "+ PRIMARY KEY (\`a\`)," ]] || false - [[ "$output" =~ "+ CONSTRAINT \`chk_vk8cbuqc\` CHECK ((\`b\` > 0))" ]] || false + [[ "$output" =~ "+ CONSTRAINT \`test_chk_vk8cbuqc\` CHECK ((\`b\` > 0))" ]] || false } @test "drop-create: default changes" {