Skip to content

Commit

Permalink
feat(项目): 增加同步器功能
Browse files Browse the repository at this point in the history
  • Loading branch information
storezhang committed May 17, 2024
1 parent b38d951 commit f703847
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 22 deletions.
4 changes: 4 additions & 0 deletions internal/config/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package config

type Sync struct {
}
39 changes: 39 additions & 0 deletions internal/db/synchronizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package db

import (
"github.com/goexl/gox"
"github.com/goexl/gox/field"
"github.com/goexl/log"
"github.com/pangum/db/internal/config"
)

type Synchronizer struct {
gox.CannotCopy

engine *Engine
config *config.Sync
logger log.Logger
}

func NewSynchronizer(engine *Engine, config *config.Sync, logger log.Logger) *Synchronizer {
return &Synchronizer{
engine: engine,
config: config,
logger: logger,
}
}

func (s *Synchronizer) Sync(models ...any) (err error) {
fields := gox.Fields[any]{
field.New("models", models),
field.New("config", s.config),
}
s.logger.Info("同步数据库表开始", fields...)
if err = s.engine.Sync(models...); nil == err {
s.logger.Info("同步数据库表成功", fields...)
} else {
s.logger.Error("同步数据库表失败", fields...)
}

return
}
29 changes: 16 additions & 13 deletions internal/plugin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,41 @@ import (
type Config struct {
// 数据库类型
// nolint:lll
Type string `default:"sqlite3" json:"type" yaml:"type" xml:"type" toml:"type" validate:"required,oneof=mysql sqlite3 mssql oracle psql"`
Type string `default:"sqlite3" json:"type,omitempty" yaml:"type" xml:"type" toml:"type" validate:"required,oneof=mysql sqlite3 mssql oracle psql"`

// 地址,填写服务器地址
Addr string `default:"127.0.0.1:3306" json:"addr" yaml:"addr" xml:"addr" toml:"addr" validate:"required,hostname_port"`
// nolint:lll
Addr string `default:"127.0.0.1:3306" json:"addr,omitempty" yaml:"addr" xml:"addr" toml:"addr" validate:"required,hostname_port"`
// 授权,用户名
Username string `json:"username" yaml:"username" xml:"username" toml:"username"`
Username string `json:"username,omitempty" yaml:"username" xml:"username" toml:"username"`
// 授权,密码
Password string `json:"password" yaml:"password" xml:"password" toml:"password"`
Password string `json:"password,omitempty" yaml:"password" xml:"password" toml:"password"`
// 连接协议
// nolint: lll
Protocol string `default:"tcp" json:"protocol" yaml:"protocol" xml:"protocol" toml:"password" validate:"required,oneof=tcp udp"`
Protocol string `default:"tcp" json:"protocol,omitempty" yaml:"protocol" xml:"protocol" toml:"password" validate:"required,oneof=tcp udp"`

// 连接池配置
Connection config.Connection `json:"connection" yaml:"connection" xml:"connection" toml:"connection"`
Connection config.Connection `json:"connection,omitempty" yaml:"connection" xml:"connection" toml:"connection"`

// 表名的前缀
Suffix string `json:"suffix" yaml:"suffix" xml:"suffix" toml:"suffix"`
Suffix string `json:"suffix,omitempty" yaml:"suffix" xml:"suffix" toml:"suffix"`
// 表名后缀
Prefix string `json:"prefix" yaml:"prefix" xml:"prefix" toml:"prefix"`
Prefix string `json:"prefix,omitempty" yaml:"prefix" xml:"prefix" toml:"prefix"`
// 连接的数据库名
Schema string `default:"data.db" json:"schema" yaml:"schema" xml:"schema" toml:"schema" validate:"required"`
Schema string `default:"data.db" json:"schema,omitempty" yaml:"schema" xml:"schema" toml:"schema" validate:"required"`

// 额外参数
// nolint: lll
Parameters string `default:"parseTime=true&loc=Local" json:"parameters" yaml:"parameters" xml:"parameters" toml:"parameters"`
Parameters string `default:"parseTime=true&loc=Local" json:"parameters,omitempty" yaml:"parameters" xml:"parameters" toml:"parameters"`
// 是否连接时测试数据库连接是否完好
Ping bool `default:"true" json:"ping" yaml:"ping" xml:"ping" toml:"ping"`
Ping bool `default:"true" json:"ping,omitempty" yaml:"ping" xml:"ping" toml:"ping"`
// 是否显示执行语句
Show bool `default:"false" json:"show" yaml:"show" xml:"show" toml:"show"`
Show bool `default:"false" json:"show,omitempty" yaml:"show" xml:"show" toml:"show"`

// SSH代理连接
SSH *config.Ssh `json:"ssh" yaml:"ssh" xml:"ssh" toml:"ssh"`
SSH *config.Ssh `json:"ssh,omitempty" yaml:"ssh" xml:"ssh" toml:"ssh"`
// 同步
Sync *config.Sync `json:"sync,omitempty" yaml:"sync" xml:"sync" toml:"sync"`
}

func (c *Config) dsn() (dsn string, err error) {
Expand Down
29 changes: 23 additions & 6 deletions internal/plugin/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/goexl/gox"
"github.com/goexl/gox/field"
"github.com/goexl/log"
"github.com/pangum/db/internal/config"
"github.com/pangum/db/internal/db"
"github.com/pangum/db/internal/internal"
"github.com/pangum/pangu"
Expand All @@ -21,13 +22,10 @@ type Constructor struct {
// 构造方法
}

func (c *Constructor) New(config *pangu.Config, logger log.Logger) (engine *db.Engine, err error) {
wrapper := new(Wrapper)
if ge := config.Build().Get(wrapper); nil != ge {
err = ge
} else if created, ne := c.new(wrapper.Db, logger); nil != ne {
func (c *Constructor) New(config *Config, logger log.Logger) (engine *db.Engine, err error) {
if created, ne := c.new(config, logger); nil != ne {
err = ne
} else if se := c.setup(wrapper.Db, created, logger); nil != se {
} else if se := c.setup(config, created, logger); nil != se {
err = se
} else {
engine = created
Expand All @@ -40,6 +38,25 @@ func (c *Constructor) NewTransaction(engine *db.Engine, logger log.Logger) *db.T
return db.NewTransaction(engine, logger)
}

func (c *Constructor) NewSynchronizer(engine *db.Engine, config *config.Sync, logger log.Logger) *db.Synchronizer {
return db.NewSynchronizer(engine, config, logger)
}

func (c *Constructor) Config(config *pangu.Config) (conf *Config, err error) {
wrapper := new(Wrapper)
if ge := config.Build().Get(wrapper); nil != ge {
err = ge
} else {
conf = wrapper.Db
}

return
}

func (c *Constructor) Sync(config *Config) *config.Sync {
return config.Sync
}

func (c *Constructor) setup(config *Config, engine *db.Engine, logger log.Logger) (err error) {
// 替换成统一的日志框架
engine.SetLogger(internal.NewXorm(logger))
Expand Down
9 changes: 6 additions & 3 deletions pangu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
)

func init() {
creator := new(plugin.Constructor)
ctor := new(plugin.Constructor)
pangu.New().Get().Dependency().Put(
creator.New,
creator.NewTransaction,
ctor.Config,
ctor.Sync,
ctor.New,
ctor.NewTransaction,
ctor.NewSynchronizer,
).Build().Build().Apply()
}
8 changes: 8 additions & 0 deletions synchronizer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package db

import (
"github.com/pangum/db/internal/db"
)

// Synchronizer 同步器
type Synchronizer = db.Synchronizer

0 comments on commit f703847

Please sign in to comment.