From 711823fc318f0518a3a727f48c60ebecab3c7138 Mon Sep 17 00:00:00 2001 From: yedongfu Date: Thu, 3 Jun 2021 14:43:33 +0800 Subject: [PATCH] test passed --- common/conf.sample | 6 ++++++ examples/data.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 common/conf.sample create mode 100644 examples/data.go diff --git a/common/conf.sample b/common/conf.sample new file mode 100644 index 0000000..800c29f --- /dev/null +++ b/common/conf.sample @@ -0,0 +1,6 @@ +mysql: + host: 'xxx' + user: 'xxx' + password: 'xxx' + database: 'xxx' + port: '3306' diff --git a/examples/data.go b/examples/data.go new file mode 100644 index 0000000..afeb1e9 --- /dev/null +++ b/examples/data.go @@ -0,0 +1,35 @@ +package examples + +import ( + "io/ioutil" + "strings" + + "github.com/sirupsen/logrus" + "github.com/yedf/dtm/common" +) + +func RunSqlScript(mysql map[string]string, script string) { + conf := map[string]string{} + common.MustRemarshal(mysql, &conf) + conf["database"] = "" + db, con := common.DbAlone(conf) + defer func() { con.Close() }() + content, err := ioutil.ReadFile(script) + if err != nil { + e2p(err) + } + sqls := strings.Split(string(content), ";") + for _, sql := range sqls { + s := strings.TrimSpace(sql) + if strings.Contains(strings.ToLower(s), "drop") || s == "" { + continue + } + logrus.Printf("executing: '%s'", s) + db.Must().Exec(s) + } +} + +func PopulateMysql() { + common.InitApp(common.GetCurrentPath(), &Config) + RunSqlScript(Config.Mysql, common.GetCurrentPath()+"/examples.sql") +}