65 Star 398 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
import.go 3.68 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Nging is a toolbox for webmasters
Copyright (C) 2019-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package utils
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/admpub/archiver"
loga "github.com/admpub/log"
"github.com/admpub/nging/application/library/cron"
"github.com/admpub/nging/application/library/dbmanager/driver"
"github.com/webx-top/com"
)
// Import 导入SQL文件
func Import(ctx context.Context, cfg *driver.DbAuth, cacheDir string, files []string, asyncs ...bool) error {
if len(files) == 0 {
return nil
}
log.Println(`Starting import:`, files)
var (
port, host string
async = true
)
if len(asyncs) > 0 {
async = asyncs[0]
}
if p := strings.LastIndex(cfg.Host, `:`); p > 0 {
host = cfg.Host[0:p]
port = cfg.Host[p+1:]
} else {
host = cfg.Host
}
if len(port) == 0 {
port = `3306`
}
args := []string{
"-h" + host,
"-P" + port,
"-u" + cfg.Username,
"-p" + cfg.Password,
cfg.Db,
"-e",
``,
}
sqls := `SET FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS=0;source %s;SET FOREIGN_KEY_CHECKS=1;SET UNIQUE_CHECKS=1;`
var delDirs []string
sqlFiles := []string{}
defer func() {
for _, delDir := range delDirs {
os.RemoveAll(delDir)
}
for _, sqlFile := range sqlFiles {
if !com.FileExists(sqlFile) {
continue
}
os.Remove(sqlFile)
}
}()
nowTime := com.String(time.Now().Unix())
dataFiles := []string{}
for index, sqlFile := range files {
switch strings.ToLower(filepath.Ext(sqlFile)) {
case `.sql`:
if strings.Contains(filepath.Base(sqlFile), `struct`) {
sqlFiles = append(sqlFiles, sqlFile)
} else {
dataFiles = append(dataFiles, sqlFile)
}
case `.zip`:
dir := filepath.Join(cacheDir, fmt.Sprintf("upload-"+nowTime+"-%d", index))
err := archiver.Zip.Open(sqlFile, dir)
if err != nil {
loga.Error(err)
continue
}
delDirs = append(delDirs, dir)
err = os.Remove(sqlFile)
if err != nil {
loga.Error(err)
}
ifiles := []string{}
err = filepath.Walk(dir, func(fpath string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return err
}
if strings.ToLower(filepath.Ext(fpath)) != `.sql` {
return nil
}
if strings.Contains(info.Name(), `struct`) {
sqlFiles = append(sqlFiles, fpath)
return nil
}
ifiles = append(ifiles, fpath)
return nil
})
sqlFiles = append(sqlFiles, ifiles...)
}
}
sqlFiles = append(sqlFiles, dataFiles...)
rec := cron.NewCmdRec(1000)
for _, sqlFile := range sqlFiles {
if len(sqlFile) == 0 {
continue
}
sqlFile = filepath.ToSlash(sqlFile)
lastIndex := len(args) - 1
args[lastIndex] = fmt.Sprintf(sqls, sqlFile)
//log.Println(`mysql`, strings.Join(args, ` `))
cmd := exec.CommandContext(ctx, "mysql", args...)
cmd.Stderr = rec
if err := cmd.Start(); err != nil {
return fmt.Errorf(`Failed to import: %v`, err)
}
if !async { //非异步,需阻塞
if err := cmd.Wait(); err != nil {
return errors.New(err.Error() + `: ` + rec.String())
}
}
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.2

搜索帮助