Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
fix: support "sql" prefix exists in table name (#1256) (#1259)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <[email protected]>

Co-authored-by: Xuecheng Zhang <[email protected]>
  • Loading branch information
ti-srebot and csuzhangxc authored Nov 2, 2020
1 parent e6ca256 commit dd8c11a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func (l *Loader) prepareDbFiles(files map[string]struct{}) error {
continue
}

idx := strings.Index(file, "-schema-create.sql")
idx := strings.LastIndex(file, "-schema-create.sql")
if idx > 0 {
schemaFileCount++
db := file[:idx]
Expand Down Expand Up @@ -900,7 +900,7 @@ func (l *Loader) prepareTableFiles(files map[string]struct{}) error {
continue
}

idx := strings.Index(file, "-schema.sql")
idx := strings.LastIndex(file, "-schema.sql")
name := file[:idx]
fields := strings.Split(name, ".")
if len(fields) != 2 {
Expand Down
2 changes: 1 addition & 1 deletion loader/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func escapeName(name string) string {

// input filename is like `all_mode.t1.0.sql` or `all_mode.t1.sql`
func getDBAndTableFromFilename(filename string) (string, string, error) {
idx := strings.Index(filename, ".sql")
idx := strings.LastIndex(filename, ".sql")
if idx < 0 {
return "", "", fmt.Errorf("%s doesn't have a `.sql` suffix", filename)
}
Expand Down
31 changes: 31 additions & 0 deletions loader/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,34 @@ func (t *testUtilSuite) TestGenerateSchemaCreateFile(c *C) {
c.Assert(string(data), Equals, testCase.createSQL)
}
}

func (t *testUtilSuite) TestGetDBAndTableFromFilename(c *C) {
cases := []struct {
filename string
schema string
table string
errMsg string
}{
{"db.tbl.sql", "db", "tbl", ""},
{"db.tbl.0.sql", "db", "tbl", ""},
{"db.sqltbl.sql", "db", "sqltbl", ""},
{"db.sqltbl.0.sql", "db", "sqltbl", ""},
{"sqldb.tbl.sql", "sqldb", "tbl", ""},
{"sqldb.tbl.0.sql", "sqldb", "tbl", ""},
{"db.tbl.sql0.sql", "db", "tbl", ""},
{"db.tbl.0", "", "", ".*doesn't have a `.sql` suffix.*"},
{"db.sql", "", "", ".*doesn't have correct `.` seperator.*"},
{"db.0.sql", "db", "0", ""}, // treat `0` as the table name.
}

for _, cs := range cases {
schema, table, err := getDBAndTableFromFilename(cs.filename)
if cs.errMsg != "" {
c.Assert(err, ErrorMatches, cs.errMsg)
} else {
c.Assert(err, IsNil)
c.Assert(schema, Equals, cs.schema)
c.Assert(table, Equals, cs.table)
}
}
}

0 comments on commit dd8c11a

Please sign in to comment.