*: check database exists when create/drop table.

This commit is contained in:
siddontang
2015-10-27 20:11:09 +08:00
parent 214a90e048
commit 7d7d77f571
4 changed files with 18 additions and 7 deletions

View File

@ -396,7 +396,7 @@ func (d *ddl) AlterTable(ctx context.Context, ident table.Ident, specs []*AlterS
tbl, err := is.TableByName(ident.Schema, ident.Name)
if err != nil {
return errors.Trace(err)
return errors.Trace(ErrNotExists)
}
for _, spec := range specs {
switch spec.Action {
@ -528,7 +528,7 @@ func (d *ddl) DropTable(ctx context.Context, ti table.Ident) (err error) {
tb, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(err)
return errors.Trace(ErrNotExists)
}
job := &model.Job{
@ -573,7 +573,7 @@ func (d *ddl) CreateIndex(ctx context.Context, ti table.Ident, unique bool, inde
t, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(err)
return errors.Trace(ErrNotExists)
}
if _, ok := is.IndexByName(ti.Schema, ti.Name, indexName); ok {
return errors.Errorf("CREATE INDEX: index already exist %s", indexName)

View File

@ -19,6 +19,7 @@ import (
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/table"
qerror "github.com/pingcap/tidb/util/errors"
"github.com/pingcap/tidb/util/errors2"
)
@ -34,7 +35,10 @@ func (d *ddl) onTableCreate(t *meta.TMeta, job *model.Job) error {
tbInfo.State = model.StateNone
tables, err := t.ListTables(schemaID)
if err != nil {
if errors2.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(qerror.ErrDatabaseNotExist)
} else if err != nil {
return errors.Trace(err)
}
@ -87,9 +91,14 @@ func (d *ddl) onTableDrop(t *meta.TMeta, job *model.Job) error {
tableID := job.TableID
tblInfo, err := t.GetTable(schemaID, tableID)
if err != nil {
if errors2.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(qerror.ErrDatabaseNotExist)
} else if err != nil {
return errors.Trace(err)
} else if tblInfo == nil {
}
if tblInfo == nil {
job.State = model.JobCancelled
return errors.Trace(ErrNotExists)
}

View File

@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/stmt"
"github.com/pingcap/tidb/table"
qerror "github.com/pingcap/tidb/util/errors"
"github.com/pingcap/tidb/util/errors2"
"github.com/pingcap/tidb/util/format"
)
@ -113,7 +114,7 @@ func (s *DropTableStmt) Exec(ctx context.Context) (rset.Recordset, error) {
err := sessionctx.GetDomain(ctx).DDL().DropTable(ctx, ti.Full(ctx))
// TODO: we should return special error for table not exist, checking "not exist" is not enough,
// because some other errors may contain this error string too.
if err != nil && strings.HasSuffix(err.Error(), "not exist") {
if errors2.ErrorEqual(err, ddl.ErrNotExists) || errors2.ErrorEqual(err, qerror.ErrDatabaseNotExist) {
notExistTables = append(notExistTables, ti.String())
} else if err != nil {
return nil, errors.Trace(err)

View File

@ -82,6 +82,7 @@ func NewTable(tableID int64, tableName string, dbName string, cols []*column.Col
indexPrefix: fmt.Sprintf("%d_i", tableID),
alloc: alloc,
Columns: cols,
state: model.StatePublic,
}
return t
}