Skip to content

Commit

Permalink
Merge branch 'master' into disksing/refactor-lasetinsertid
Browse files Browse the repository at this point in the history
Conflicts:
	ddl/ddl_test.go
	plan/plans/from_test.go
	session_test.go
	stmt/stmts/insert.go
	stmt/stmts/replace.go
  • Loading branch information
disksing committed Nov 27, 2015
2 parents 0abfff4 + 58b0496 commit 75352d8
Show file tree
Hide file tree
Showing 75 changed files with 6,711 additions and 1,391 deletions.
34 changes: 16 additions & 18 deletions bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ const (
CreateGloablVariablesTable = `CREATE TABLE if not exists mysql.GLOBAL_VARIABLES(
VARIABLE_NAME VARCHAR(64) Not Null PRIMARY KEY,
VARIABLE_VALUE VARCHAR(1024) DEFAULT Null);`
// CreateGloablStatusTable is the SQL statement creates global status variable table in system db.
// TODO: MySQL puts GLOBAL_STATUS table in INFORMATION_SCHEMA db.
CreateGloablStatusTable = `CREATE TABLE if not exists mysql.GLOBAL_STATUS(
VARIABLE_NAME VARCHAR(64) Not Null PRIMARY KEY,
VARIABLE_VALUE VARCHAR(1024) DEFAULT Null);`
// CreateTiDBTable is the SQL statement creates a table in system db.
// This table is a key-value struct contains some information used by TiDB.
// Currently we only put bootstrapped in it which indicates if the system is already bootstrapped.
Expand Down Expand Up @@ -148,6 +143,7 @@ func checkBootstrappedVar(s Session) (bool, error) {
}
return false, errors.Trace(err)
}

if len(rs) != 1 {
return false, errors.New("Wrong number of Recordset")
}
Expand All @@ -156,7 +152,17 @@ func checkBootstrappedVar(s Session) (bool, error) {
if err != nil || row == nil {
return false, errors.Trace(err)
}
return row.Data[0].(string) == bootstrappedVarTrue, nil

isBootstrapped := row.Data[0].(string) == bootstrappedVarTrue
if isBootstrapped {
// Make sure that doesn't affect the following operations.

if err = s.FinishTxn(false); err != nil {
return false, errors.Trace(err)
}
}

return isBootstrapped, nil
}

// Execute DDL statements in bootstrap stage.
Expand All @@ -173,8 +179,6 @@ func doDDLWorks(s Session) {
mustExecute(s, CreateColumnPrivTable)
// Create global systemt variable table.
mustExecute(s, CreateGloablVariablesTable)
// Create global status variable table.
mustExecute(s, CreateGloablStatusTable)
// Create TiDB table.
mustExecute(s, CreateTiDBTable)
}
Expand All @@ -183,12 +187,14 @@ func doDDLWorks(s Session) {
// All the statements run in a single transaction.
func doDMLWorks(s Session) {
mustExecute(s, "BEGIN")

// Insert a default user with empty password.
mustExecute(s, `INSERT INTO mysql.user VALUES
("localhost", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
("127.0.0.1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"),
("::1", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y");`)
// Init global system variable table.

// Init global system variables table.
values := make([]string, 0, len(variable.SysVars))
for k, v := range variable.SysVars {
value := fmt.Sprintf(`("%s", "%s")`, strings.ToLower(k), v.Value)
Expand All @@ -197,15 +203,7 @@ func doDMLWorks(s Session) {
sql := fmt.Sprintf("INSERT INTO %s.%s VALUES %s;", mysql.SystemDB, mysql.GlobalVariablesTable,
strings.Join(values, ", "))
mustExecute(s, sql)
// Init global status variable table.
values = make([]string, 0, len(variable.StatusVars))
for k, v := range variable.StatusVars {
value := fmt.Sprintf(`("%s", "%s")`, strings.ToLower(k), v.Value)
values = append(values, value)
}
sql = fmt.Sprintf("INSERT INTO %s.%s VALUES %s;", mysql.SystemDB, mysql.GlobalStatusTable,
strings.Join(values, ", "))
mustExecute(s, sql)

sql = fmt.Sprintf(`INSERT INTO %s.%s VALUES("%s", "%s", "Bootstrap flag. Do not delete.")
ON DUPLICATE KEY UPDATE VARIABLE_VALUE="%s"`,
mysql.SystemDB, mysql.TiDBTable, bootstrappedVar, bootstrappedVarTrue, bootstrappedVarTrue)
Expand Down
16 changes: 8 additions & 8 deletions column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (c *Col) String() string {

// FindCol finds column in cols by name.
func FindCol(cols []*Col, name string) *Col {
for _, c := range cols {
if strings.EqualFold(c.Name.O, name) {
return c
for _, col := range cols {
if strings.EqualFold(col.Name.O, name) {
return col
}
}
return nil
Expand All @@ -82,9 +82,9 @@ func FindCols(cols []*Col, names []string) ([]*Col, error) {
// FindOnUpdateCols finds columns which have OnUpdateNow flag.
func FindOnUpdateCols(cols []*Col) []*Col {
var rcols []*Col
for _, c := range cols {
if mysql.HasOnUpdateNowFlag(c.Flag) {
rcols = append(rcols, c)
for _, col := range cols {
if mysql.HasOnUpdateNowFlag(col.Flag) {
rcols = append(rcols, col)
}
}

Expand Down Expand Up @@ -181,8 +181,8 @@ func ColDescFieldNames(full bool) []string {
// CheckOnce checks if there are duplicated column names in cols.
func CheckOnce(cols []*Col) error {
m := map[string]struct{}{}
for _, v := range cols {
name := v.Name
for _, col := range cols {
name := col.Name
_, ok := m[name.L]
if ok {
return errors.Errorf("column specified twice - %s", name)
Expand Down
4 changes: 3 additions & 1 deletion column/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (s *testColumnSuite) TestString(c *C) {
col := &Col{
model.ColumnInfo{
FieldType: *types.NewFieldType(mysql.TypeTiny),
State: model.StatePublic,
},
}
col.Flen = 2
Expand Down Expand Up @@ -109,7 +110,8 @@ func (s *testColumnSuite) TestDesc(c *C) {
func newCol(name string) *Col {
return &Col{
model.ColumnInfo{
Name: model.NewCIStr(name),
Name: model.NewCIStr(name),
State: model.StatePublic,
},
}
}
76 changes: 76 additions & 0 deletions ddl/alter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ddl

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/parser/coldef"
"github.com/pingcap/tidb/util/types"
)

func (s *testDDLSuite) TestAlterSpecification(c *C) {
tbl := []*AlterSpecification{
{
Action: AlterTableOpt,
},
{
Action: AlterDropColumn,
Name: "c1",
},
{Action: AlterDropPrimaryKey},
{Action: AlterDropForeignKey,
Name: "c"},
{Action: AlterDropIndex,
Name: "index_c"},
{Action: AlterAddConstr,
Constraint: nil},
{Action: AlterAddConstr,
Constraint: &coldef.TableConstraint{
Tp: coldef.ConstrPrimaryKey,
Keys: []*coldef.IndexColName{
{
ColumnName: "a",
Length: 10,
},
},
}},
{Action: AlterAddColumn,
Column: &coldef.ColumnDef{
Name: "c",
Tp: types.NewFieldType(mysql.TypeLong),
},
Position: &ColumnPosition{}},
{Action: AlterAddColumn,
Column: &coldef.ColumnDef{
Name: "c",
Tp: types.NewFieldType(mysql.TypeLong),
},
Position: &ColumnPosition{Type: ColumnPositionFirst}},
{Action: AlterAddColumn,
Column: &coldef.ColumnDef{
Name: "c",
Tp: types.NewFieldType(mysql.TypeLong),
},
Position: &ColumnPosition{Type: ColumnPositionAfter,
RelativeColumn: "c"}},

// Invalid action returns empty string
{Action: -1},
}

for _, spec := range tbl {
c.Assert(len(spec.String()), GreaterEqual, 0)
}
}
45 changes: 45 additions & 0 deletions ddl/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ddl

import "github.com/pingcap/tidb/model"

// Callback is the interface supporting callback function when DDL changed.
type Callback interface {
// OnChanged is called after schema is changed.
OnChanged(err error) error
// OnJobRunBefore is called before running job.
OnJobRunBefore(job *model.Job)
// OnJobUpdated is called after the running job is updated.
OnJobUpdated(job *model.Job)
}

// BaseCallback implements Callback.OnChanged interface.
type BaseCallback struct {
}

// OnChanged implements Callback interface.
func (c *BaseCallback) OnChanged(err error) error {
return err
}

// OnJobRunBefore implements Callback.OnJobRunBefore interface.
func (c *BaseCallback) OnJobRunBefore(job *model.Job) {
// Nothing to do.
}

// OnJobUpdated implements Callback.OnJobUpdated interface.
func (c *BaseCallback) OnJobUpdated(job *model.Job) {
// Nothing to do.
}
51 changes: 51 additions & 0 deletions ddl/callback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package ddl

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/model"
)

type testDDLCallback struct {
*BaseCallback

onJobRunBefore func(*model.Job)
onJobUpdated func(*model.Job)
}

func (tc *testDDLCallback) OnJobRunBefore(job *model.Job) {
if tc.onJobRunBefore != nil {
tc.onJobRunBefore(job)
return
}

tc.BaseCallback.OnJobRunBefore(job)
}

func (tc *testDDLCallback) OnJobUpdated(job *model.Job) {
if tc.onJobUpdated != nil {
tc.onJobUpdated(job)
return
}

tc.BaseCallback.OnJobUpdated(job)
}

func (s *testDDLSuite) TestCallback(c *C) {
cb := &BaseCallback{}
c.Assert(cb.OnChanged(nil), IsNil)
cb.OnJobRunBefore(nil)
cb.OnJobUpdated(nil)
}
Loading

0 comments on commit 75352d8

Please sign in to comment.