Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validation for dropping an auto_increment primary key column #7462

Merged
merged 11 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/dolthub/fslock v0.0.3
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20240205203605-9e6c6d650813
github.com/dolthub/vitess v0.0.0-20240206204925-6acf16fa777c
github.com/dustin/go-humanize v1.0.1
github.com/fatih/color v1.13.0
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568
Expand Down Expand Up @@ -57,7 +57,7 @@ require (
github.com/cespare/xxhash v1.1.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.17.1-0.20240206012309-33cd2ef75cf6
github.com/dolthub/go-mysql-server v0.17.1-0.20240206210445-80b0f5eeba85
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
github.com/google/go-github/v57 v57.0.0
Expand Down
4 changes: 4 additions & 0 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e h1:kPsT4a47cw
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.17.1-0.20240206012309-33cd2ef75cf6 h1:s1pJKeZ8xdsRYC8k2ONsG8x9e4lhLgUdZXGGnSkxWJs=
github.com/dolthub/go-mysql-server v0.17.1-0.20240206012309-33cd2ef75cf6/go.mod h1:ic+L5GOnDgwPaNHqvegDMofJvB7Jg2bVVqPrFXVOUoM=
github.com/dolthub/go-mysql-server v0.17.1-0.20240206210445-80b0f5eeba85 h1:rfuC9DgfK5RwW/JTGqI6XXZF/83qMVM0ztsG95aqutY=
github.com/dolthub/go-mysql-server v0.17.1-0.20240206210445-80b0f5eeba85/go.mod h1:Kjfapc/1feJ3/WgTgiY0LoR5x/3ax5PJ26brPnvYUO8=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488 h1:0HHu0GWJH0N6a6keStrHhUAK5/o9LVfkh44pvsV4514=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488/go.mod h1:ehexgi1mPxRTk0Mok/pADALuHbvATulTh6gzr7NzZto=
github.com/dolthub/jsonpath v0.0.2-0.20240201003050-392940944c15 h1:sfTETOpsrNJPDn2KydiCtDgVu6Xopq8k3JP8PjFT22s=
Expand All @@ -197,6 +199,8 @@ github.com/dolthub/swiss v0.1.0 h1:EaGQct3AqeP/MjASHLiH6i4TAmgbG/c4rA6a1bzCOPc=
github.com/dolthub/swiss v0.1.0/go.mod h1:BeucyB08Vb1G9tumVN3Vp/pyY4AMUnr9p7Rz7wJ7kAQ=
github.com/dolthub/vitess v0.0.0-20240205203605-9e6c6d650813 h1:tGwsoLAMFQ+7FDEyIWOIJ1Vc/nptbFi0Fh7SQahB8ro=
github.com/dolthub/vitess v0.0.0-20240205203605-9e6c6d650813/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
github.com/dolthub/vitess v0.0.0-20240206204925-6acf16fa777c h1:Zt23BHsxvPHGfpHV9k/FcsHqWZjfybyQQux2OLpRni8=
github.com/dolthub/vitess v0.0.0-20240206204925-6acf16fa777c/go.mod h1:IwjNXSQPymrja5pVqmfnYdcy7Uv7eNJNBPK/MEh9OOw=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
Expand Down
76 changes: 76 additions & 0 deletions go/libraries/doltcore/sqle/enginetest/dolt_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5204,6 +5204,82 @@ var DoltAutoIncrementTests = []queries.ScriptTest{
},
},
},
{
// Dropping the primary key constraint from a table implicitly truncates the table, which resets the
// auto_increment value for the table to 0. These tests assert that the correct auto_increment value is
// restored after the drop pk operation.
Name: "drop auto_increment primary key",
SetUpScript: []string{
"create table t (a int primary key auto_increment, b int, key (a))",
"call dolt_commit('-Am', 'empty table')",
"call dolt_branch('branch1')",
"call dolt_branch('branch2')",
"insert into t (b) values (1), (2)",
"call dolt_commit('-am', 'two values on main')",
"call dolt_checkout('branch1')",
"insert into t (b) values (3), (4)",
"call dolt_commit('-am', 'two values on branch1')",
"call dolt_checkout('branch2')",
"insert into t (b) values (5), (6)",
"call dolt_checkout('main')",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "alter table t drop primary key",
Expected: []sql.Row{{types.NewOkResult(0)}},
},
{
// highest value in any branch is 6
Query: "insert into t (b) values (7), (8)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 7}}},
},
{
Query: "select * from t order by a",
Expected: []sql.Row{
{1, 1},
{2, 2},
{7, 7},
{8, 8},
},
},
{
Query: "call dolt_checkout('branch2')",
SkipResultsCheck: true,
},
{
Query: "insert into t (b) values (9), (10)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 9}}},
},
{
Query: "select * from t order by a",
Expected: []sql.Row{
{5, 5},
{6, 6},
{9, 9},
{10, 10},
},
},
{
Query: "alter table t drop primary key",
Expected: []sql.Row{{types.NewOkResult(0)}},
},
{
Query: "insert into t (b) values (11), (12)",
Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 11}}},
},
{
Query: "select * from t order by a",
Expected: []sql.Row{
{5, 5},
{6, 6},
{9, 9},
{10, 10},
{11, 11},
{12, 12},
},
},
},
},
}

var DoltCherryPickTests = []queries.ScriptTest{
Expand Down
23 changes: 21 additions & 2 deletions go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,16 @@ func (t *AlterableDoltTable) RewriteInserter(
})
}

// Grab the next auto_increment value before we call truncate, since truncate will delete the table
// and clear out the auto_increment tracking for this table.
var nextAutoIncValue uint64
if t.autoIncCol.AutoIncrement {
nextAutoIncValue, err = t.PeekNextAutoIncrementValue(ctx)
if err != nil {
return nil, err
}
}

// TODO: test for this when the table is auto increment and exists on another branch
dt, err = t.truncate(ctx, dt, newSch, sess)
if err != nil {
Expand Down Expand Up @@ -1590,6 +1600,14 @@ func (t *AlterableDoltTable) RewriteInserter(

newWs := ws.WithWorkingRoot(newRoot)

// Restore the next auto increment value, since it was cleared when we truncated the table
if t.autoIncCol.AutoIncrement {
err = t.AutoIncrementSetter(ctx).SetAutoIncrementValue(ctx, nextAutoIncValue)
if err != nil {
return nil, err
}
}

opts := dbState.WriteSession().GetOptions()
opts.ForeignKeyChecksDisabled = true
writeSession := writer.NewWriteSession(dt.Format(), newWs, ait, opts)
Expand Down Expand Up @@ -1945,8 +1963,9 @@ func validateSchemaChange(
}

func (t *AlterableDoltTable) adjustForeignKeysForDroppedPk(ctx *sql.Context, tbl string, root *doltdb.RootValue) (*doltdb.RootValue, error) {
if t.autoIncCol.AutoIncrement {
return nil, sql.ErrWrongAutoKey.New()
err := sql.ValidatePrimaryKeyDrop(ctx, t, t.PrimaryKeySchema())
if err != nil {
return nil, err
}

fkc, err := root.GetForeignKeyCollection(ctx)
Expand Down
Loading