Skip to content

Commit

Permalink
ddl: disallow alter table on view
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewDi committed Dec 31, 2018
1 parent 61d2a1f commit afa59db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ var (
ErrCoalesceOnlyOnHashPartition = terror.ClassDDL.New(codeCoalesceOnlyOnHashPartition, mysql.MySQLErrName[mysql.ErrCoalesceOnlyOnHashPartition])
// ErrViewWrongList returns create view must include all columns in the select clause
ErrViewWrongList = terror.ClassDDL.New(codeViewWrongList, mysql.MySQLErrName[mysql.ErrViewWrongList])
// ErrTableIsNotBaseTable returns for table is not base table.
ErrTableIsNotBaseTable = terror.ClassDDL.New(codeErrWrongObject, "'%s.%s' is not BASE TABLE")
)

// DDL is responsible for updating schema in data store and maintaining in-memory InfoSchema cache.
Expand Down Expand Up @@ -648,6 +650,7 @@ const (
codeWrongKeyColumn = 1167
codeBlobKeyWithoutLength = 1170
codeInvalidOnUpdate = 1294
codeErrWrongObject = terror.ErrCode(mysql.ErrWrongObject)
codeViewWrongList = 1353
codeUnsupportedOnGeneratedColumn = 3106
codeGeneratedColumnNonPrior = 3107
Expand Down Expand Up @@ -727,6 +730,7 @@ func init() {
codeWarnDataTruncated: mysql.WarnDataTruncated,
codeCoalesceOnlyOnHashPartition: mysql.ErrCoalesceOnlyOnHashPartition,
codeUnknownPartition: mysql.ErrUnknownPartition,
codeErrWrongObject: mysql.ErrWrongObject,
}
terror.ErrClassToMySQLCodes[terror.ClassDDL] = ddlMySQLErrCodes
}
13 changes: 13 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,19 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
return errRunMultiSchemaChanges
}

is := d.GetInformationSchema(ctx)
_, ok := is.SchemaByName(ident.Schema)
if !ok {
return errors.Trace(infoschema.ErrDatabaseNotExists)
}
t, err := is.TableByName(ident.Schema, ident.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ident.Schema, ident.Name))
}
if t.Meta().IsView() {
return ErrTableIsNotBaseTable.GenWithStackByArgs(ident.Schema, ident.Name)
}

for _, spec := range validSpecs {
var handledCharsetOrCollate bool
switch spec.Tp {
Expand Down
8 changes: 8 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func (s *testSuite3) TestAlterTableAddColumn(c *C) {
r.Close()
tk.MustExec("alter table alter_test add column c3 varchar(50) default 'CURRENT_TIMESTAMP'")
tk.MustQuery("select c3 from alter_test").Check(testkit.Rows("CURRENT_TIMESTAMP"))
tk.MustExec("create or replace view alter_view as select c1,c2 from alter_test")
_, err = tk.Exec("alter table alter_view add column c4 varchar(50)")
c.Assert(err.Error(), Equals, ddl.ErrTableIsNotBaseTable.GenWithStackByArgs("test", "alter_view").Error())
tk.MustExec("drop view alter_view")
}

func (s *testSuite3) TestAddNotNullColumnNoDefault(c *C) {
Expand Down Expand Up @@ -283,6 +287,10 @@ func (s *testSuite3) TestAlterTableModifyColumn(c *C) {
createSQL := result.Rows()[0][1]
expected := "CREATE TABLE `mc` (\n `c1` bigint(20) DEFAULT NULL,\n `c2` text DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
c.Assert(createSQL, Equals, expected)
tk.MustExec("create or replace view alter_view as select c1,c2 from mc")
_, err = tk.Exec("alter table alter_view modify column c2 text")
c.Assert(err.Error(), Equals, ddl.ErrTableIsNotBaseTable.GenWithStackByArgs("test", "alter_view").Error())
tk.MustExec("drop view alter_view")
}

func (s *testSuite3) TestDefaultDBAfterDropCurDB(c *C) {
Expand Down

0 comments on commit afa59db

Please sign in to comment.