Skip to content

Commit

Permalink
*: add context.Context to NewTxn(), tiny refactor (#8530)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored and jackysp committed Dec 6, 2018
1 parent 98c72c6 commit 93acd10
Show file tree
Hide file tree
Showing 33 changed files with 153 additions and 126 deletions.
31 changes: 16 additions & 15 deletions ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
// create table t (c1 int, c2 int);
tblInfo := testTableInfo(c, d, "t", 2)
ctx := testNewContext(d)
err := ctx.NewTxn()
err := ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
// insert t values (1, 2);
Expand Down Expand Up @@ -96,7 +96,7 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
hookCtx.Store = s.store
prevState = job.SchemaState
var err error
err = hookCtx.NewTxn()
err = hookCtx.NewTxn(context.Background())
if err != nil {
checkErr = errors.Trace(err)
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *testColumnChangeSuite) testAddColumnNoDefault(c *C, ctx sessionctx.Cont
hookCtx.Store = s.store
prevState = job.SchemaState
var err error
err = hookCtx.NewTxn()
err = hookCtx.NewTxn(context.Background())
if err != nil {
checkErr = errors.Trace(err)
}
Expand Down Expand Up @@ -221,15 +221,15 @@ func (s *testColumnChangeSuite) testColumnDrop(c *C, ctx sessionctx.Context, d *

func (s *testColumnChangeSuite) checkAddWriteOnly(ctx sessionctx.Context, d *ddl, deleteOnlyTable, writeOnlyTable table.Table, h int64) error {
// WriteOnlyTable: insert t values (2, 3)
err := ctx.NewTxn()
err := ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
_, err = writeOnlyTable.AddRecord(ctx, types.MakeDatums(2, 3), false)
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (s *testColumnChangeSuite) checkAddWriteOnly(ctx sessionctx.Context, d *ddl
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -276,7 +276,7 @@ func (s *testColumnChangeSuite) checkAddWriteOnly(ctx sessionctx.Context, d *ddl
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -293,39 +293,40 @@ func touchedSlice(t table.Table) []bool {
return touched
}

func (s *testColumnChangeSuite) checkAddPublic(ctx sessionctx.Context, d *ddl, writeOnlyTable, publicTable table.Table) error {
func (s *testColumnChangeSuite) checkAddPublic(sctx sessionctx.Context, d *ddl, writeOnlyTable, publicTable table.Table) error {
ctx := context.TODO()
// publicTable Insert t values (4, 4, 4)
err := ctx.NewTxn()
err := sctx.NewTxn(ctx)
if err != nil {
return errors.Trace(err)
}
h, err := publicTable.AddRecord(ctx, types.MakeDatums(4, 4, 4), false)
h, err := publicTable.AddRecord(sctx, types.MakeDatums(4, 4, 4), false)
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = sctx.NewTxn(ctx)
if err != nil {
return errors.Trace(err)
}
// writeOnlyTable update t set c1 = 3 where c1 = 4
oldRow, err := writeOnlyTable.RowWithCols(ctx, h, writeOnlyTable.WritableCols())
oldRow, err := writeOnlyTable.RowWithCols(sctx, h, writeOnlyTable.WritableCols())
if err != nil {
return errors.Trace(err)
}
if len(oldRow) != 3 {
return errors.Errorf("%v", oldRow)
}
newRow := types.MakeDatums(3, 4, oldRow[2].GetValue())
err = writeOnlyTable.UpdateRecord(ctx, h, oldRow, newRow, touchedSlice(writeOnlyTable))
err = writeOnlyTable.UpdateRecord(sctx, h, oldRow, newRow, touchedSlice(writeOnlyTable))
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = sctx.NewTxn(ctx)
if err != nil {
return errors.Trace(err)
}
// publicTable select * from t, make sure the new c3 value 4 is not overwritten to default value 3.
err = checkResult(ctx, publicTable, publicTable.WritableCols(), testutil.RowsWithSep(" ", "2 3 3", "3 4 4"))
err = checkResult(sctx, publicTable, publicTable.WritableCols(), testutil.RowsWithSep(" ", "2 3 3", "3 4 4"))
if err != nil {
return errors.Trace(err)
}
Expand Down
54 changes: 27 additions & 27 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *testColumnSuite) TestColumn(c *C) {
c.Assert(err, IsNil)
}

err := ctx.NewTxn()
err := ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

i := int64(0)
Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *testColumnSuite) TestColumn(c *C) {

h, err := t.AddRecord(ctx, types.MakeDatums(11, 12, 13, 14), false)
c.Assert(err, IsNil)
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
values, err := t.RowWithCols(ctx, h, t.Cols())
c.Assert(err, IsNil)
Expand Down Expand Up @@ -267,7 +267,7 @@ func (s *testColumnSuite) TestColumn(c *C) {
}

func (s *testColumnSuite) checkColumnKVExist(ctx sessionctx.Context, t table.Table, handle int64, col *table.Column, columnValue interface{}, isExist bool) error {
err := ctx.NewTxn()
err := ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -322,7 +322,7 @@ func (s *testColumnSuite) checkDeleteOnlyColumn(ctx sessionctx.Context, d *ddl,
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -345,7 +345,7 @@ func (s *testColumnSuite) checkDeleteOnlyColumn(ctx sessionctx.Context, d *ddl,
return errors.Trace(err)
}
// Test add a new row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -355,7 +355,7 @@ func (s *testColumnSuite) checkDeleteOnlyColumn(ctx sessionctx.Context, d *ddl,
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -382,7 +382,7 @@ func (s *testColumnSuite) checkDeleteOnlyColumn(ctx sessionctx.Context, d *ddl,
return errors.Trace(err)
}
// Test remove a row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -391,7 +391,7 @@ func (s *testColumnSuite) checkDeleteOnlyColumn(ctx sessionctx.Context, d *ddl,
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func (s *testColumnSuite) checkWriteOnlyColumn(ctx sessionctx.Context, d *ddl, t
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -449,7 +449,7 @@ func (s *testColumnSuite) checkWriteOnlyColumn(ctx sessionctx.Context, d *ddl, t
}

// Test add a new row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -459,7 +459,7 @@ func (s *testColumnSuite) checkWriteOnlyColumn(ctx sessionctx.Context, d *ddl, t
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -486,7 +486,7 @@ func (s *testColumnSuite) checkWriteOnlyColumn(ctx sessionctx.Context, d *ddl, t
return errors.Trace(err)
}
// Test remove a row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -495,7 +495,7 @@ func (s *testColumnSuite) checkWriteOnlyColumn(ctx sessionctx.Context, d *ddl, t
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -528,7 +528,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -549,7 +549,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d
}

// Test add a new row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -559,7 +559,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -587,7 +587,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d
}

// Test remove a row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -596,7 +596,7 @@ func (s *testColumnSuite) checkReorganizationColumn(ctx sessionctx.Context, d *d
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -624,7 +624,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -646,7 +646,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI
}

// Test add a new row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -656,7 +656,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI
if err != nil {
return errors.Trace(err)
}
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -676,7 +676,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI
}

// Test remove a row.
err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand All @@ -686,7 +686,7 @@ func (s *testColumnSuite) checkPublicColumn(ctx sessionctx.Context, d *ddl, tblI
return errors.Trace(err)
}

err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -750,7 +750,7 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
tblInfo := testTableInfo(c, d, "t", 3)
ctx := testNewContext(d)

err := ctx.NewTxn()
err := ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -817,7 +817,7 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
c.Assert(errors.ErrorStack(hErr), Equals, "")
c.Assert(ok, IsTrue)

err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
Expand All @@ -835,7 +835,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
tblInfo := testTableInfo(c, d, "t", 4)
ctx := testNewContext(d)

err := ctx.NewTxn()
err := ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -890,7 +890,7 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
c.Assert(hErr, IsNil)
c.Assert(ok, IsTrue)

err = ctx.NewTxn()
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)

job = testDropTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down
Loading

0 comments on commit 93acd10

Please sign in to comment.