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

loader: generate db-schema-create.sql file if not exists #186

Merged
merged 6 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,10 @@ func (l *Loader) prepareDbFiles(files map[string]struct{}) error {
}

if schemaFileCount == 0 {
return errors.New("invalid mydumper files for there are no `-schema-create.sql` files found")
log.Warn("invalid mydumper files for there are no `-schema-create.sql` files found, and will generate later")
}
if len(l.db2Tables) == 0 {
return errors.New("no available `-schema-create.sql` files, check mydumper parameter matches black-white-list in task config")
log.Warn("no available `-schema-create.sql` files, check mydumper parameter matches black-white-list in task config, will generate later")
}

return nil
Expand All @@ -713,7 +713,13 @@ func (l *Loader) prepareTableFiles(files map[string]struct{}) error {
}
tables, ok := l.db2Tables[db]
if !ok {
return errors.Errorf("invalid table schema file, cannot find db - %s", file)
log.Warnf("can't find schema create file for db %s, will generate one", db)
if err := generateSchemaCreateFile(l.cfg.Dir, db); err != nil {
return errors.Trace(err)
}
l.db2Tables[db] = make(Tables2DataFiles)
tables = l.db2Tables[db]
l.totalFileCount.Add(1)
}

if _, ok := tables[table]; ok {
Expand Down
18 changes: 18 additions & 0 deletions loader/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ import (
"crypto/sha1"
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/pingcap/errors"
)

// CollectDirFiles gets files in path
Expand Down Expand Up @@ -73,3 +76,18 @@ func shortSha1(s string) string {
func percent(a int64, b int64) string {
return fmt.Sprintf("%.2f %%", float64(a)/float64(b)*100)
}

func generateSchemaCreateFile(dir string, schema string) error {
file, err := os.Create(path.Join(dir, fmt.Sprintf("%s-schema-create.sql", schema)))
if err != nil {
return errors.Trace(err)
}
defer file.Close()

_, err = fmt.Fprintf(file, "CREATE DATABASE `%s`;\n", escapeName(schema))
return errors.Trace(err)
}

func escapeName(name string) string {
return strings.Replace(name, "`", "``", -1)
}
31 changes: 31 additions & 0 deletions loader/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
package loader

import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -61,3 +65,30 @@ func (t *testUtilSuite) TestSQLReplace(c *C) {
func (t *testUtilSuite) TestShortSha1(c *C) {
c.Assert(shortSha1("/tmp/test_sha1_short_6"), Equals, "97b645")
}

func (t *testUtilSuite) TestGenerateSchemaCreateFile(c *C) {
dir := c.MkDir()
testCases := []struct {
schema string
createSQL string
}{
{
"loader_test",
"CREATE DATABASE `loader_test`;\n",
}, {
"loader`test",
"CREATE DATABASE `loader``test`;\n",
},
}
for _, testCase := range testCases {
err := generateSchemaCreateFile(dir, testCase.schema)
c.Assert(err, IsNil)

file, err := os.Open(path.Join(dir, fmt.Sprintf("%s-schema-create.sql", testCase.schema)))
c.Assert(err, IsNil)

data, err := ioutil.ReadAll(file)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, testCase.createSQL)
}
}