Skip to content

Commit

Permalink
feat(database): use dsn to dump
Browse files Browse the repository at this point in the history
  • Loading branch information
alomerry committed May 2, 2024
1 parent 20621f6 commit 3c959f1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
45 changes: 45 additions & 0 deletions static/cons/database.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package cons

import (
"errors"
"regexp"
)

const (
DbSchemeMySQL = "mysql"

DsnInvalid = "dsn invalid"
DsnInvalidNoSuitableResolver = "no suitable resolver"
)

var (
MySQL = newDatabaseType("mysql")
)

var (
Umami = newDatabase(MySQL, "umami")
WalineBlog = newDatabase(MySQL, "waline_blog")

// mysql://xxx:[email protected]:3306/xxx
dsnReg = regexp.MustCompile(`([\w]+)://(.*):(.*)@(.*):([0-9]+)/([\w_]+)`)
)

type DatabaseType string
Expand All @@ -23,3 +38,33 @@ type Database struct {
func newDatabase(dbType DatabaseType, name string) Database {
return Database{dbType, name}
}

type BaseDbInfo struct {
User string
Password string
Host string
Port string
}

func ParseDbDsn(dsn string) (*BaseDbInfo, error) {
if !dsnReg.MatchString(dsn) {
return nil, errors.New("不含 dsn")
}

res := dsnReg.FindStringSubmatch(dsn)
if len(res) < 5 {
return nil, errors.New(DsnInvalid)
}
switch res[1] {
case DbSchemeMySQL:
return &BaseDbInfo{
User: res[2],
Password: res[3],
Host: res[4],
Port: res[5],
}, nil
default:
return nil, errors.New(DsnInvalidNoSuitableResolver)
}
return nil, nil
}
16 changes: 16 additions & 0 deletions static/cons/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cons

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseDbDsn(t *testing.T) {
var dsn = "mysql://aaa:[email protected]:3306/xxx"
info, err := ParseDbDsn(dsn)
assert.Nil(t, err)
assert.Equal(t, "aaa", info.User)
assert.Equal(t, "bbb", info.Password)
assert.Equal(t, "example.com", info.Host)
assert.Equal(t, "3306", info.Port)
}
15 changes: 10 additions & 5 deletions utils/db/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ type innerDumpTool interface {

type GenDumpCmdParamFunc func(*DumpTool)

func MySQLDumpCmdParam(user, host, port, password string) GenDumpCmdParamFunc {
func MySQLDumpCmdParam(dsn string) GenDumpCmdParamFunc {
return func(tool *DumpTool) {
info, err := cons.ParseDbDsn(dsn)
if err != nil {
panic(err)
}

tool.dbCfg[cons.MySQL] = map[string]any{
"user": user,
"host": host,
"port": port,
"password": password,
"user": info.User,
"host": info.Host,
"port": info.Port,
"password": info.Password,
}
}
}
Expand Down
20 changes: 7 additions & 13 deletions utils/db/dump_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package db

import (
"fmt"
"github.com/alomerry/go-tools/static/cons"
"testing"
)

func TestDumpDatabase(t *testing.T) {
tool := NewDumpTool(
MySQLDumpCmdParam("root", "alomerry.com", "13306", "e0t=ereFqvpibm}91Y:n"),
SetDumpPath("/Users/alomerry/workspace/go-tools/output"),
)
fmt.Println(tool.DumpDbs(cons.WalineBlog))
}
//func TestDumpDatabase(t *testing.T) {
// tool := NewDumpTool(
// MySQLDumpCmdParam("mysql://root:[email protected]:12345"),
// SetDumpPath("/Users/alomerry/workspace/go-tools/output"),
// )
// fmt.Println(tool.DumpDbs(cons.WalineBlog))
//}

0 comments on commit 3c959f1

Please sign in to comment.