Retain not null constraint on primary key columns when dropping primary key (#2745)

This commit is contained in:
jennifersp
2022-02-04 15:20:21 -08:00
committed by GitHub
parent 508fe36325
commit 94ad6bb78e
13 changed files with 68 additions and 13 deletions
+1 -1
View File
@@ -18,7 +18,6 @@ require (
github.com/denisbrodbeck/machineid v1.0.1
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20201005193433-3ee972b1d078
github.com/dolthub/fslock v0.0.3
github.com/dolthub/go-mysql-server v0.11.1-0.20220202231901-96fc17bbd0af
github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371
github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
@@ -78,6 +77,7 @@ require (
)
require (
github.com/dolthub/go-mysql-server v0.11.1-0.20220203192057-279acfb0711e
github.com/google/flatbuffers v2.0.5+incompatible
github.com/kch42/buzhash v0.0.0-20160816060738-9bdec3dec7c6
github.com/prometheus/client_golang v1.11.0
+2 -2
View File
@@ -172,8 +172,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-mysql-server v0.11.1-0.20220202231901-96fc17bbd0af h1:CjddjDhxHzMT/3O+/NiLu1DcJVEhBgEcm79yCS+Yh98=
github.com/dolthub/go-mysql-server v0.11.1-0.20220202231901-96fc17bbd0af/go.mod h1:46f9AwcXU81JvYIl1nnq0iuS18D5WTcWHKJbXaK7Stw=
github.com/dolthub/go-mysql-server v0.11.1-0.20220203192057-279acfb0711e h1:JUfLg8iWscJ2tbxi71QX6r6vNR6iQqtGSBAX4mnvD1c=
github.com/dolthub/go-mysql-server v0.11.1-0.20220203192057-279acfb0711e/go.mod h1:46f9AwcXU81JvYIl1nnq0iuS18D5WTcWHKJbXaK7Stw=
github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371 h1:oyPHJlzumKta1vnOQqUnfdz+pk3EmnHS3Nd0cCT0I2g=
github.com/dolthub/ishell v0.0.0-20220112232610-14e753f0f371/go.mod h1:dhGBqcCEfK5kuFmeO5+WOx3hqc1k3M29c1oS/R7N4ms=
github.com/dolthub/jsonpath v0.0.0-20210609232853-d49537a30474 h1:xTrR+l5l+1Lfq0NvhiEsctylXinUMFhhsqaEcl414p8=
@@ -50,12 +50,12 @@ func AddPrimaryKeyToTable(ctx context.Context, table *doltdb.Table, tableName st
newColl := make([]schema.Column, sch.GetAllCols().Size())
pkOrdinals := make([]int, len(columns))
for ord, col := range sch.GetAllCols().GetColumns() {
if col.IsNullable() {
col.Constraints = append(col.Constraints, schema.NotNullConstraint{})
}
if i, ok := pkColOrdering[col.Name]; ok {
pkOrdinals[i] = ord
col.IsPartOfPK = true
if col.IsNullable() {
col.Constraints = append(col.Constraints, schema.NotNullConstraint{})
}
}
newColl[ord] = col
}
@@ -38,6 +38,9 @@ func DropPrimaryKeyFromTable(ctx context.Context, table *doltdb.Table, nbf *type
// Modify the schema to convert the primary key cols into non primary key cols
newCollection := schema.MapColCollection(sch.GetAllCols(), func(col schema.Column) schema.Column {
if col.IsPartOfPK {
col.Constraints = append(col.Constraints, schema.NotNullConstraint{})
}
col.IsPartOfPK = false
return col
})
+1 -1
View File
@@ -603,7 +603,7 @@ SQL
dolt sql -q "alter table t add primary key (pk, val)"
run dolt diff -r sql
[ $status -eq 0 ]
[ "${lines[0]}" = 'ALTER TABLE `t` ADD `pk2` INT NOT NULL;' ]
[ "${lines[0]}" = 'ALTER TABLE `t` ADD `pk2` INT;' ]
[ "${lines[1]}" = 'ALTER TABLE `t` DROP PRIMARY KEY;' ]
[ "${lines[2]}" = 'ALTER TABLE `t` ADD PRIMARY KEY (pk,val);' ]
[ "${lines[3]}" = 'warning: skipping data diff due to primary key set change' ]
@@ -3,6 +3,7 @@ load $BATS_TEST_DIRNAME/helper/common.bash
remotesrv_pid=
setup() {
skiponwindows "tests are flaky on Windows"
setup_common
cd $BATS_TMPDIR
mkdir remotes-$$
+1 -1
View File
@@ -5,6 +5,7 @@ REMOTE=http://localhost:50051/test-org/test-repo
remotesrv_pid=
setup() {
skiponwindows "tests are flaky on Windows"
setup_common
cd $BATS_TMPDIR
mkdir remotes-$$
@@ -15,7 +16,6 @@ setup() {
dolt remote add test-remote $REMOTE
dolt push test-remote main
export DOLT_HEAD_COMMIT=`get_head_commit`
skiponwindows "git-dolt tests are flaky on Windows"
}
teardown() {
@@ -612,13 +612,19 @@ SQL
}
@test "primary-key-changes: dropping primary key retains not null constraint" {
dolt sql -q "create table t (pk int)"
dolt sql -q "alter table t add primary key (pk)"
dolt sql -q "create table t (pk1 int, pk2 int, c1 int)"
dolt sql -q "alter table t add primary key (pk1, pk2)"
dolt sql -q "alter table t drop primary key"
run dolt sql -q "show create table t"
[ $status -eq 0 ]
[[ "$output" =~ "NOT NULL" ]] || false
[[ "$output" =~ "`pk1` int NOT NULL" ]] || false
[[ "$output" =~ "`pk2` int NOT NULL" ]] || false
[[ ! "$output" =~ "PRIMARY KEY" ]] || false
dolt sql -q "show create table t" > res.txt
run grep 'NOT NULL' res.txt
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
}
@test "primary-key-changes: creating table with null and primary key column throws error" {
@@ -649,8 +655,47 @@ create database mydb;
create table mydb.test(pk int, c1 int);
alter table mydb.test add primary key(pk);
SQL
run dolt sql -q "show create table mydb.test";
run dolt sql -q "show create table mydb.test"
[ $status -eq 0 ]
skip "Dolt does not accept most alters in the db.table form"
[[ "$output" =~ "PRIMARY KEY" ]]
}
@test "primary-key-changes: can add and drop primary keys on keyless db.table named tables" {
dolt sql -q "CREATE DATABASE mydb"
dolt sql -q "CREATE TABLE mydb.test(pk INT, c1 LONGTEXT, c2 BIGINT, c3 INT)"
dolt sql -q "ALTER TABLE mydb.test ADD PRIMARY KEY(pk, c1)"
run dolt sql -q "SHOW CREATE TABLE mydb.test"
[ $status -eq 0 ]
[[ "$output" =~ "PRIMARY KEY (\`pk\`,\`c1\`)" ]] || false
dolt sql -q "ALTER TABLE mydb.test DROP PRIMARY KEY"
run dolt sql -q "SHOW CREATE TABLE mydb.test"
[ $status -eq 0 ]
[[ ! "$output" =~ "PRIMARY KEY" ]]
dolt sql -q "SHOW CREATE TABLE mydb.test" > output.txt
run grep 'NOT NULL' output.txt
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
}
@test "primary-key-changes: can drop and add multiple primary keys on db.table named tables" {
dolt sql -q "CREATE DATABASE mydb"
dolt sql -q "CREATE TABLE mydb.test(pk INT PRIMARY KEY, c1 INT, c2 BIGINT, c3 INT)"
dolt sql -q "ALTER TABLE mydb.test DROP PRIMARY KEY"
dolt sql -q "SHOW CREATE TABLE mydb.test" > output.txt
run grep 'NOT NULL' output.txt
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 1 ]
dolt sql -q "ALTER TABLE mydb.test ADD PRIMARY KEY(c1, c2)"
run dolt sql -q "SHOW CREATE TABLE mydb.test"
[ $status -eq 0 ]
[[ "$output" =~ "PRIMARY KEY (\`c1\`,\`c2\`)" ]] || false
dolt sql -q "SHOW CREATE TABLE mydb.test" > output.txt
run grep 'NOT NULL' output.txt
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
}
+1
View File
@@ -3,6 +3,7 @@ load $BATS_TEST_DIRNAME/helper/common.bash
remotesrv_pid=
setup() {
skiponwindows "tests are flaky on Windows"
setup_common
cd $BATS_TMPDIR
mkdir remotes-$$
@@ -10,6 +10,7 @@ make_repo() {
}
setup() {
skiponwindows "tests are flaky on Windows"
setup_no_dolt_init
make_repo repo1
make_repo repo2
@@ -10,6 +10,7 @@ make_repo() {
}
setup() {
skiponwindows "tests are flaky on Windows"
setup_no_dolt_init
make_repo repo1
make_repo repo2
+1
View File
@@ -10,6 +10,7 @@ make_repo() {
}
setup() {
skiponwindows "tests are flaky on Windows"
setup_no_dolt_init
make_repo repo1
make_repo repo2
@@ -2,6 +2,7 @@
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {
skiponwindows "tests are flaky on Windows"
setup_common
# Needed for dolt_branches test
@@ -16,6 +17,7 @@ setup() {
}
teardown() {
skiponwindows "tests are flaky on Windows"
assert_feature_version
teardown_common
kill $remotesrv_pid