From b4c0e6ca2439562c9b07da5e41fbb04236af4bbb Mon Sep 17 00:00:00 2001 From: Du Chuan Date: Sun, 20 May 2018 16:42:09 +0800 Subject: [PATCH] DDL: Refine error message about "Invalid default value" (#6333) --- ddl/ddl_api.go | 5 +-- ddl/integration_test.go | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 ddl/integration_test.go diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 11a598087a16..a1616573a18c 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -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. diff --git a/ddl/integration_test.go b/ddl/integration_test.go new file mode 100644 index 000000000000..17e4ab14cb2f --- /dev/null +++ b/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) +}