From 6ca3acf630362f57d31a474cee8701b736d40dea Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 2 Nov 2015 12:00:40 +0800 Subject: [PATCH] ddl: update DropIndex and support index in constraint. --- ddl/ddl.go | 35 +++++++++++++++++++++++++++-------- ddl/ddl_test.go | 2 +- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index cab1bee8c6..407562a88c 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -55,7 +55,7 @@ type DDL interface { CreateTable(ctx context.Context, ident table.Ident, cols []*coldef.ColumnDef, constrs []*coldef.TableConstraint) error DropTable(ctx context.Context, tableIdent table.Ident) (err error) CreateIndex(ctx context.Context, tableIdent table.Ident, unique bool, indexName model.CIStr, columnNames []*coldef.IndexColName) error - DropIndex(ctx context.Context, schema, tableName, indexName model.CIStr) error + DropIndex(ctx context.Context, tableIdent table.Ident, indexName model.CIStr) error GetInformationSchema() infoschema.InfoSchema AlterTable(ctx context.Context, tableIdent table.Ident, spec []*AlterSpecification) error } @@ -446,15 +446,34 @@ func (d *ddl) AlterTable(ctx context.Context, ident table.Ident, specs []*AlterS if err != nil { return errors.Trace(ErrNotExists) } + + // now we only allow one schema changes at the same time. + if len(specs) != 1 { + return errors.New("can't run multi schema changes in one DDL") + } + for _, spec := range specs { switch spec.Action { case AlterAddColumn: - if err := d.addColumn(ctx, schema, tbl, spec, is.SchemaMetaVersion()); err != nil { - return errors.Trace(err) + err = d.addColumn(ctx, schema, tbl, spec, is.SchemaMetaVersion()) + case AlterDropIndex: + err = d.DropIndex(ctx, ident, model.NewCIStr(spec.Name)) + case AlterAddConstr: + constr := spec.Constraint + switch spec.Constraint.Tp { + case coldef.ConstrKey, coldef.ConstrIndex: + err = d.CreateIndex(ctx, ident, false, model.NewCIStr(constr.ConstrName), spec.Constraint.Keys) + case coldef.ConstrUniq, coldef.ConstrUniqIndex, coldef.ConstrUniqKey: + err = d.CreateIndex(ctx, ident, true, model.NewCIStr(constr.ConstrName), spec.Constraint.Keys) + default: + // nothing to do now. } default: - // TODO: process more actions - continue + // nothing to do now. + } + + if err != nil { + return errors.Trace(err) } } return nil @@ -637,14 +656,14 @@ func (d *ddl) CreateIndex(ctx context.Context, ti table.Ident, unique bool, inde return errors.Trace(err) } -func (d *ddl) DropIndex(ctx context.Context, schemaName, tableName, indexName model.CIStr) error { +func (d *ddl) DropIndex(ctx context.Context, ti table.Ident, indexName model.CIStr) error { is := d.infoHandle.Get() - schema, ok := is.SchemaByName(schemaName) + schema, ok := is.SchemaByName(ti.Schema) if !ok { return errors.Trace(qerror.ErrDatabaseNotExist) } - t, err := is.TableByName(schemaName, tableName) + t, err := is.TableByName(ti.Schema, ti.Name) if err != nil { return errors.Trace(ErrNotExists) } diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 864567a29f..854b2626da 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -154,7 +154,7 @@ func (ts *testSuite) TestT(c *C) { c.Assert(err, IsNil) tbs := sessionctx.GetDomain(ctx).InfoSchema().SchemaTables(tbIdent.Schema) c.Assert(len(tbs), Equals, 2) - err = sessionctx.GetDomain(ctx).DDL().DropIndex(ctx, tbIdent.Schema, tbIdent.Name, idxName) + err = sessionctx.GetDomain(ctx).DDL().DropIndex(ctx, tbIdent, idxName) c.Assert(err, IsNil) err = sessionctx.GetDomain(ctx).DDL().DropTable(ctx, tbIdent) c.Assert(err, IsNil)