Skip to content

Commit

Permalink
ddl: make sequence cache-opt mutually exclusive to each other (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
AilinKid authored Jan 6, 2022
1 parent 249903c commit bbe4c8e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ddl/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ func handleSequenceOptions(seqOptions []*ast.SequenceOption, sequenceInfo *model
maxSetFlag = true
case ast.SequenceCache:
sequenceInfo.CacheValue = op.IntValue
sequenceInfo.Cache = true
case ast.SequenceNoCache:
sequenceInfo.CacheValue = 0
sequenceInfo.Cache = false
case ast.SequenceCycle:
sequenceInfo.Cycle = true
Expand Down Expand Up @@ -140,13 +142,13 @@ func handleSequenceOptions(seqOptions []*ast.SequenceOption, sequenceInfo *model
}

func validateSequenceOptions(seqInfo *model.SequenceInfo) bool {
// To ensure that cache * increment will never overflows.
// To ensure that cache * increment will never overflow.
var maxIncrement int64
if seqInfo.Increment == 0 {
// Increment shouldn't be set as 0.
return false
}
if seqInfo.CacheValue <= 0 {
if seqInfo.Cache && seqInfo.CacheValue <= 0 {
// Cache value should be bigger than 0.
return false
}
Expand Down Expand Up @@ -205,7 +207,9 @@ func alterSequenceOptions(sequenceOptions []*ast.SequenceOption, ident ast.Ident
oldSequence.MaxValue = op.IntValue
case ast.SequenceCache:
oldSequence.CacheValue = op.IntValue
oldSequence.Cache = true
case ast.SequenceNoCache:
oldSequence.CacheValue = 0
oldSequence.Cache = false
case ast.SequenceCycle:
oldSequence.Cycle = true
Expand Down
20 changes: 20 additions & 0 deletions ddl/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ddl_test

import (
"strconv"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/ddl"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/table/tables"
testkit2 "github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/testkit"
)

Expand Down Expand Up @@ -1088,3 +1090,21 @@ func (s *testSequenceSuite) TestAlterSequencePrivilege(c *C) {
c.Assert(err.Error(), Equals, "[planner:1142]ALTER command denied to user 'myuser'@'localhost' for table 'my_seq'")
tk.MustExec("drop sequence if exists my_seq")
}

func TestDdl_AlterSequenceIssue31265(t *testing.T) {
store, clean := testkit2.CreateMockStore(t)
defer clean()

tk := testkit2.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create sequence seq cache=1 nocache")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB"))

tk.MustExec("create sequence cache_to_nocache_seq;")
tk.MustExec("alter sequence cache_to_nocache_seq nocache;")
tk.MustQuery("show create sequence cache_to_nocache_seq;").Check(testkit.Rows("cache_to_nocache_seq CREATE SEQUENCE `cache_to_nocache_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB"))

tk.MustExec("create sequence nocache_to_cache_seq nocache;")
tk.MustExec("alter sequence nocache_to_cache_seq cache 10;")
tk.MustQuery("show create sequence nocache_to_cache_seq;").Check(testkit.Rows("nocache_to_cache_seq CREATE SEQUENCE `nocache_to_cache_seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 10 nocycle ENGINE=InnoDB"))
}

0 comments on commit bbe4c8e

Please sign in to comment.