-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator.go
executable file
·73 lines (59 loc) · 2.23 KB
/
migrator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package migrator
import (
"fmt"
"github.com/D3rus/go-library/configurator"
"github.com/D3rus/go-library/data_base/client"
"github.com/D3rus/go-library/data_base/client/driver"
source2 "github.com/D3rus/go-library/data_base/migrator/source"
"github.com/D3rus/go-library/data_base/table"
"github.com/D3rus/go-library/logger"
"github.com/golang-migrate/migrate/v4"
migratorMySQL "github.com/golang-migrate/migrate/v4/database/mysql"
migratorSQLite "github.com/golang-migrate/migrate/v4/database/sqlite"
"github.com/golang-migrate/migrate/v4/source"
"strings"
)
//go:generate mockgen -package=migrator -destination=mock_configurator_test.go github.com/D3rus/go-library/configurator Configurator
func NewWithConfigurator(
config *Config,
configurator configurator.Configurator,
db client.DataBase,
informer logger.Informer,
tableReplacer table.Replacer,
) (*migrate.Migrate, error) {
return New(Configuration(config, configurator), db, informer, tableReplacer)
}
func Configuration(config *Config, configurator configurator.Configurator) *Config {
if config.Path == "" || config.Path == PathDefault {
configurator.SetDefault(PathFieldName, PathDefault)
config.Path = configurator.GetString(PathFieldName)
}
return config
}
func New(
config *Config,
db client.DataBase,
informer logger.Informer,
tableReplacer table.Replacer,
) (*migrate.Migrate, error) {
path := strings.ReplaceAll(config.Path, DriverReplacement, db.DriverName())
informer.Infof("migrator: migration path '%s'", path)
source.Register(source2.RegisteredDriverName, source2.NewFile(tableReplacer))
switch db.DriverName() {
case driver.MySQL:
informer.Infof("migrator: usage sql driver '%s'", driver.MySQL)
d, err := migratorMySQL.WithInstance(db.SQL(), &migratorMySQL.Config{})
if err != nil {
return nil, err
}
return migrate.NewWithDatabaseInstance(path, strings.ToLower(driver.MySQL), d)
case driver.SQLite:
informer.Infof("migrator: usage sql driver '%s'", driver.SQLite)
d, err := migratorSQLite.WithInstance(db.SQL(), &migratorSQLite.Config{})
if err != nil {
return nil, err
}
return migrate.NewWithDatabaseInstance(path, strings.ToLower(driver.SQLite), d)
}
return nil, fmt.Errorf("migrator: sql driver '%s' unknown", db.DriverName())
}