Created
May 6, 2019 19:05
-
-
Save adsr/8e3bc67a71bbf66281ce3c94768fde82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/go/vt/vttablet/tabletserver/schema/load_table.go b/go/vt/vttablet/tabletserver/schema/load_table.go | |
| index 95d8a739c..97548a27b 100644 | |
| --- a/go/vt/vttablet/tabletserver/schema/load_table.go | |
| +++ b/go/vt/vttablet/tabletserver/schema/load_table.go | |
| @@ -10,34 +10,39 @@ You may obtain a copy of the License at | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| */ | |
| package schema | |
| import ( | |
| + "flag" | |
| "fmt" | |
| "strconv" | |
| "strings" | |
| "time" | |
| "vitess.io/vitess/go/sqltypes" | |
| "vitess.io/vitess/go/vt/log" | |
| "vitess.io/vitess/go/vt/sqlparser" | |
| "vitess.io/vitess/go/vt/vttablet/tabletserver/connpool" | |
| "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" | |
| querypb "vitess.io/vitess/go/vt/proto/query" | |
| ) | |
| +var ( | |
| + schemaEngineHack = flag.Bool("schema_engine_hack", false, "avoid looking up schema info; probably breaks vindexes") | |
| +) | |
| + | |
| // LoadTable creates a Table from the schema info in the database. | |
| func LoadTable(conn *connpool.DBConn, tableName string, tableType string, comment string) (*Table, error) { | |
| ta := NewTable(tableName) | |
| sqlTableName := sqlparser.String(ta.Name) | |
| if err := fetchColumns(ta, conn, sqlTableName); err != nil { | |
| return nil, err | |
| } | |
| if err := fetchIndexes(ta, conn, sqlTableName); err != nil { | |
| return nil, err | |
| } | |
| @@ -57,20 +62,28 @@ func LoadTable(conn *connpool.DBConn, tableName string, tableType string, commen | |
| func fetchColumns(ta *Table, conn *connpool.DBConn, sqlTableName string) error { | |
| qr, err := conn.Exec(tabletenv.LocalContext(), fmt.Sprintf("select * from %s where 1 != 1", sqlTableName), 0, true) | |
| if err != nil { | |
| return err | |
| } | |
| fieldTypes := make(map[string]querypb.Type, len(qr.Fields)) | |
| // TODO(sougou): Store the full field info in the schema. | |
| for _, field := range qr.Fields { | |
| fieldTypes[field.Name] = field.Type | |
| } | |
| + if *schemaEngineHack { | |
| + for fieldName, fieldType := range fieldTypes { | |
| + if fieldDefault, err := sqltypes.NewValue(fieldType, []byte("")); err != nil { | |
| + ta.AddColumn(fieldName, fieldType, fieldDefault, "") | |
| + } | |
| + } | |
| + return nil | |
| + } | |
| columns, err := conn.Exec(tabletenv.LocalContext(), fmt.Sprintf("describe %s", sqlTableName), 10000, false) | |
| if err != nil { | |
| return err | |
| } | |
| for _, row := range columns.Rows { | |
| name := row[0].ToString() | |
| columnType, ok := fieldTypes[name] | |
| if !ok { | |
| // This code is unreachable. | |
| log.Warningf("Table: %s, column %s not found in select list, skipping.", ta.Name, name) | |
| @@ -91,20 +104,24 @@ func fetchColumns(ta *Table, conn *connpool.DBConn, sqlTableName string) error { | |
| } | |
| // overwrite the original value with the new one. | |
| row[4] = r.Rows[0][0] | |
| } | |
| ta.AddColumn(name, columnType, row[4], row[5].ToString()) | |
| } | |
| return nil | |
| } | |
| func fetchIndexes(ta *Table, conn *connpool.DBConn, sqlTableName string) error { | |
| + if *schemaEngineHack { | |
| + ta.Done() | |
| + return nil | |
| + } | |
| indexes, err := conn.Exec(tabletenv.LocalContext(), fmt.Sprintf("show index from %s", sqlTableName), 10000, false) | |
| if err != nil { | |
| return err | |
| } | |
| var currentIndex *Index | |
| currentName := "" | |
| for _, row := range indexes.Rows { | |
| indexName := row[2].ToString() | |
| if currentName != indexName { | |
| currentIndex = ta.AddIndex(indexName, row[1].ToString() == "0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment