Skip to content

Commit 574b1c5

Browse files
committed
增加sql函数的导入导出 ⭐
1 parent 73e6444 commit 574b1c5

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ go语言版mysqldump, goroutine并发导sql, 比navicat等工具导出sql快!
99
此项目为: https://github.com/xelabs/go-mydumper 项目的修改优化版
1010

1111
如下变更:
12-
- 加入mysql source组合源命令(-m), 传参 -m user:pass@host:port 即可定义mysql源, 简化传参
13-
- 加入排除指定table数据导出sql 命令(-exclude)
1412
- 修复视图view导出后无法导入的问题
13+
- 增加function的导入导出
14+
- 加入mysql source组合源命令(-m), 传参 -m user:pass@host:port, 简化传参
15+
- 加入排除指定table数据导出sql 命令(-exclude)
1516
- 优化日志格式和运行时间的显示
16-
- 合并导入和导出sql功能到同一个文件(-i/-o来分区导入sql还是导出sql)
17+
- 合并导入和导出sql功能到同一个文件(-i/-o来分区)
1718
- 提供所有平台的release编译文件
1819

1920
## 命令行

dumper.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ func writeDBName(args *common.Args) {
1919
_ = common.WriteFile(file, args.Database)
2020
}
2121

22+
func dumpFunctionSchema(log *xlog.Log, conn *common.Connection, args *common.Args) {
23+
qr, err := conn.Fetch(fmt.Sprintf("SELECT `name` FROM mysql.proc WHERE type = 'FUNCTION' AND db = '%s'", args.Database))
24+
common.AssertNil(err)
25+
26+
for _, t := range qr.Rows {
27+
function := t[0].String()
28+
qr, err := conn.Fetch(fmt.Sprintf("SHOW CREATE FUNCTION `%s`.`%s`", args.Database, function))
29+
common.AssertNil(err)
30+
31+
schema := qr.Rows[0][2].String() + ";\n"
32+
file := fmt.Sprintf("%s/%s-schema-function.sql", args.Outdir, function)
33+
_ = common.WriteFile(file, schema)
34+
log.Info("dumping.function[%s.%s].schema...", args.Database, function)
35+
}
36+
}
37+
2238
func dumpTableSchema(log *xlog.Log, conn *common.Connection, args *common.Args, table string) {
2339
qr, err := conn.Fetch(fmt.Sprintf("SHOW CREATE TABLE `%s`.`%s`", args.Database, table))
2440
common.AssertNil(err)
@@ -139,10 +155,14 @@ func Dumper(log *xlog.Log, args *common.Args) {
139155
// database.
140156
conn := pool.Get()
141157

142-
// tables.
143158
var wg sync.WaitGroup
144159
var tables []string
145160
t := time.Now()
161+
162+
//function
163+
dumpFunctionSchema(log, conn, args)
164+
165+
//table
146166
if args.ExcludeTables != "" {
147167
excludeTable = excludeTable + args.ExcludeTables + ","
148168
}

loader.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ import (
1616

1717
// Files tuple.
1818
type Files struct {
19-
schemas []string
20-
tables []string
19+
function []string
20+
schemas []string
21+
tables []string
2122
}
2223

2324
var (
24-
viewSuffix = "-schema-view.sql"
25-
schemaSuffix = "-schema.sql"
26-
tableSuffix = ".sql"
27-
dbName = ""
25+
functionSuffix = "-schema-function.sql"
26+
viewSuffix = "-schema-view.sql"
27+
schemaSuffix = "-schema.sql"
28+
tableSuffix = ".sql"
29+
dbName = ""
2830
)
2931

3032
func loadFiles(log *xlog.Log, dir string) *Files {
@@ -39,6 +41,8 @@ func loadFiles(log *xlog.Log, dir string) *Files {
3941
switch {
4042
case strings.HasSuffix(path, schemaSuffix):
4143
files.schemas = append(files.schemas, path)
44+
case strings.HasSuffix(path, functionSuffix):
45+
files.function = append(files.function, path)
4246
case strings.HasSuffix(path, viewSuffix):
4347
views = append(views, path)
4448
default:
@@ -73,6 +77,24 @@ func restoreDatabaseSchema(log *xlog.Log, db string, conn *common.Connection) {
7377
log.Info("restoring.database[%s]", dbName)
7478
}
7579

80+
func restoreFunctionSchema(log *xlog.Log, functions []string, conn *common.Connection) {
81+
for _, function := range functions {
82+
name := strings.TrimSuffix(filepath.Base(function), functionSuffix)
83+
84+
dropQuery := fmt.Sprintf("DROP FUNCTION IF EXISTS %s", name)
85+
err := conn.Execute(dropQuery)
86+
common.AssertNil(err)
87+
88+
data, err := common.ReadFile(function)
89+
common.AssertNil(err)
90+
query := common.BytesToString(data)
91+
92+
err = conn.Execute(query)
93+
common.AssertNil(err)
94+
log.Info("restoring.function[%s]", name)
95+
}
96+
}
97+
7698
func restoreTableSchema(log *xlog.Log, overwrite bool, tables []string, conn *common.Connection) {
7799
for _, table := range tables {
78100
// use
@@ -81,10 +103,7 @@ func restoreTableSchema(log *xlog.Log, overwrite bool, tables []string, conn *co
81103

82104
log.Info("working.table[%s]", name)
83105

84-
err := conn.Execute(fmt.Sprintf("USE `%s`", dbName))
85-
common.AssertNil(err)
86-
87-
err = conn.Execute("SET FOREIGN_KEY_CHECKS=0")
106+
err := conn.Execute("SET FOREIGN_KEY_CHECKS=0")
88107
common.AssertNil(err)
89108

90109
data, err := common.ReadFile(table)
@@ -146,16 +165,22 @@ func Loader(log *xlog.Log, args *common.Args) {
146165
common.AssertNil(err)
147166
defer pool.Close()
148167

168+
t := time.Now()
149169
files := loadFiles(log, args.Outdir)
150170

151-
// database.
152171
conn := pool.Get()
172+
173+
// database.
153174
restoreDatabaseSchema(log, args.Database, conn)
154-
pool.Put(conn)
155175

176+
err = conn.Execute(fmt.Sprintf("USE `%s`", dbName))
177+
common.AssertNil(err)
178+
179+
// function.
180+
restoreFunctionSchema(log, files.function, conn)
156181
// tables.
157-
conn = pool.Get()
158182
restoreTableSchema(log, args.OverwriteTables, files.schemas, conn)
183+
159184
pool.Put(conn)
160185

161186
// Shuffle the tables
@@ -166,7 +191,6 @@ func Loader(log *xlog.Log, args *common.Args) {
166191

167192
var wg sync.WaitGroup
168193
var bytes uint64
169-
t := time.Now()
170194
for _, table := range files.tables {
171195
conn := pool.Get()
172196
wg.Add(1)

0 commit comments

Comments
 (0)