Skip to content

Commit

Permalink
*: update test, update log
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Oct 9, 2015
1 parent cf05dfe commit 0188010
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 2 additions & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ func updateDefaultValue(ctx context.Context, t *tables.Table, col *column.Col) e
continue
}

// TODO: check and get timestamp/datetime default value.
// refer to getDefaultValue in stmt/stmts/stmt_helper.go.
if err0 = t.SetColValue(txn, k, col.DefaultValue); err0 != nil {
return errors.Trace(err0)
}
Expand Down
31 changes: 25 additions & 6 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
mysql "github.com/pingcap/tidb/mysqldef"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/stmt"
Expand Down Expand Up @@ -81,20 +82,38 @@ func (ts *testSuite) TestT(c *C) {
tb, err := sessionctx.GetDomain(ctx).InfoSchema().TableByName(tbIdent2.Schema, tbIdent2.Name)
c.Assert(err, IsNil)
c.Assert(tb, NotNil)
_, err = tb.AddRecord(ctx, []interface{}{1})
rid0, err := tb.AddRecord(ctx, []interface{}{1})
c.Assert(err, IsNil)
_, err = tb.AddRecord(ctx, []interface{}{2})
rid1, err := tb.AddRecord(ctx, []interface{}{2})
c.Assert(err, IsNil)
alterStmt := statement(`alter table t2 add b enum("bbb") first`).(*stmts.AlterTableStmt)
alterStmt := statement(`alter table t2 add b enum("bb") first`).(*stmts.AlterTableStmt)
sessionctx.GetDomain(ctx).DDL().AlterTable(ctx, tbIdent2, alterStmt.Specs)
c.Assert(alterStmt.Specs[0].String(), Not(Equals), "")
_, err = tb.Row(ctx, 1)
cols, err := tb.Row(ctx, rid0)
c.Assert(err, IsNil)
alterStmt = statement("alter table t2 add c timestamp after b").(*stmts.AlterTableStmt)
c.Assert(len(cols), Equals, 2)
c.Assert(cols[0], Equals, nil)
c.Assert(cols[1], Equals, int64(1))
alterStmt = statement("alter table t2 add c varchar(255) after b").(*stmts.AlterTableStmt)
sessionctx.GetDomain(ctx).DDL().AlterTable(ctx, tbIdent2, alterStmt.Specs)
c.Assert(alterStmt.Specs[0].String(), Not(Equals), "")
_, err = tb.Row(ctx, 2)
tb, err = sessionctx.GetDomain(ctx).InfoSchema().TableByName(tbIdent2.Schema, tbIdent2.Name)
c.Assert(err, IsNil)
c.Assert(tb, NotNil)
cols, err = tb.Row(ctx, rid1)
c.Assert(err, IsNil)
c.Assert(len(cols), Equals, 3)
c.Assert(cols[0], Equals, nil)
c.Assert(cols[1], Equals, nil)
c.Assert(cols[2], Equals, int64(2))
rid3, err := tb.AddRecord(ctx, []interface{}{mysql.Enum{Name: "bb", Value: 1}, "c", 3})
c.Assert(err, IsNil)
cols, err = tb.Row(ctx, rid3)
c.Assert(err, IsNil)
c.Assert(len(cols), Equals, 3)
c.Assert(cols[0], Equals, mysql.Enum{Name: "bb", Value: 1})
c.Assert(cols[1], Equals, "c")
c.Assert(cols[2], Equals, int64(3))

tb, err = sessionctx.GetDomain(ctx).InfoSchema().TableByName(tbIdent.Schema, tbIdent.Name)
c.Assert(err, IsNil)
Expand Down
13 changes: 7 additions & 6 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import (
"github.com/pingcap/tidb/util/errors2"
)

// KeyLastItem is the last item in key.
const KeyLastItem = 3
// The key has tableID, rowID/idx, columnID three parts.

This comment has been minimized.

Copy link
@qiuyesuifeng

qiuyesuifeng Oct 9, 2015

Member

I guess keyLastItem means record key?
Then the key content will be tableID_r, rowID, columnID three parts?

// keyLastItem is the last item in key.
const keyLastItem = 3

// Table implements table.Table interface.
type Table struct {
Expand Down Expand Up @@ -138,11 +139,11 @@ func ColumnID(key []byte) (interface{}, error) {
if err != nil {
return 0, errors.Trace(err)
}
if len(k) != KeyLastItem {
if len(k) != keyLastItem {
return 0, errors.New("column not exist")
}

return k[KeyLastItem-1], nil
return k[keyLastItem-1], nil
}

func (t *Table) unflatten(rec interface{}, col *column.Col) (interface{}, error) {
Expand Down Expand Up @@ -285,10 +286,10 @@ func (t *Table) setOnUpdateData(ctx context.Context, touched []bool, data []inte
func (t *Table) SetColValue(txn kv.Transaction, key []byte, data interface{}) error {
v, err := t.EncodeValue(data)
if err != nil {
return err
return errors.Trace(err)
}
if err := txn.Set(key, v); err != nil {
return err
return errors.Trace(err)
}
return nil
}
Expand Down

0 comments on commit 0188010

Please sign in to comment.