Skip to content

Commit

Permalink
DDL: Refine error message about "Invalid default value" (#6333)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and tiancaiamao committed May 20, 2018
1 parent 65131a4 commit b4c0e6c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
5 changes: 2 additions & 3 deletions ddl/ddl_api.go
Expand Up @@ -467,11 +467,10 @@ func checkDefaultValue(ctx sessionctx.Context, c *table.Column, hasDefaultValue
}

if c.DefaultValue != nil {
_, err := table.GetColDefaultValue(ctx, c.ToInfo())
if types.ErrTruncated.Equal(err) {
if _, err := table.GetColDefaultValue(ctx, c.ToInfo()); err != nil {
return types.ErrInvalidDefault.GenByArgs(c.Name)
}
return errors.Trace(err)
return nil
}

// Set not null but default null is invalid.
Expand Down
88 changes: 88 additions & 0 deletions ddl/integration_test.go
@@ -0,0 +1,88 @@
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ddl_test

import (
"fmt"

"github.com/juju/errors"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)

var _ = Suite(&testIntegrationSuite{})

type testIntegrationSuite struct {
store kv.Storage
dom *domain.Domain
ctx sessionctx.Context
}

func (s *testIntegrationSuite) TearDownTest(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
r := tk.MustQuery("show tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
}

func (s *testIntegrationSuite) SetUpSuite(c *C) {
var err error
testleak.BeforeTest()
s.store, s.dom, err = newStoreWithBootstrap()
c.Assert(err, IsNil)
s.ctx = mock.NewContext()
}

func (s *testIntegrationSuite) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
}

func (s *testIntegrationSuite) TestInvalidDefault(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("USE test;")

_, err := tk.Exec("create table t(c1 decimal default 1.7976931348623157E308)")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, types.ErrInvalidDefault), IsTrue)

_, err = tk.Exec("create table t( c1 varchar(2) default 'TiDB');")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, types.ErrInvalidDefault), IsTrue)
}

func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) {
store, err := mockstore.NewMockTikvStore()
if err != nil {
return nil, nil, errors.Trace(err)
}
session.SetSchemaLease(0)
session.SetStatsLease(0)
dom, err := session.BootstrapSession(store)
return store, dom, errors.Trace(err)
}

0 comments on commit b4c0e6c

Please sign in to comment.