-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrator.go
More file actions
82 lines (68 loc) · 2 KB
/
Copy pathmigrator.go
File metadata and controls
82 lines (68 loc) · 2 KB
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
74
75
76
77
78
79
80
81
82
package miglite
import (
"database/sql"
"github.com/gookit/miglite/internal/config"
"github.com/gookit/miglite/internal/database"
"github.com/gookit/miglite/pkg/command"
)
// Migrator manage the migration
type Migrator struct {
cfg *Config
// TODO add migrations by go code
//
// Example:
// mig.Add("2026...-add_user_table", `UP sql`, `DOWN sql`, options)
//
// migrations []*migration.Migration
}
// NewAuto creates a new Migrator instance with autoload default config
func NewAuto(fns ...ConfigFn) (*Migrator, error) {
return New("", fns...)
}
// New creates a new Migrator instance, with optional configuration functions
//
// - configFile: if not exist, will skip load it.
func New(configFile string, fns ...ConfigFn) (*Migrator, error) {
cfg, err := config.Load(configFile)
if err != nil {
return nil, err
}
for _, fn := range fns {
fn(cfg)
}
return NewWithConfig(cfg), nil
}
// NewWithConfig creates a new Migrator instance with a pre-configured Config
func NewWithConfig(cfg *Config) *Migrator {
command.SetCfg(cfg)
return &Migrator{cfg: cfg}
}
// SetSqlDB sets the database connection
func (m *Migrator) SetSqlDB(db *sql.DB) {
dbCfg := m.cfg.Database
command.SetDB(database.NewWithSqlDB(dbCfg.Driver, db))
}
// Init initializes the migration schema
func (m *Migrator) Init(opt command.InitOption) error {
return command.HandleInit(opt)
}
// Up runs the migration up operation.
func (m *Migrator) Up(opt command.UpOption) error {
return command.HandleUp(opt)
}
// Down runs the migration down operation.
func (m *Migrator) Down(opt command.DownOption) error {
return command.HandleDown(opt)
}
// Skip skips some migration files.
func (m *Migrator) Skip(opt command.SkipOption) error {
return command.HandleSkip(opt)
}
// Status shows the status of the migrations.
func (m *Migrator) Status(opt command.StatusOption) error {
return command.HandleStatus(opt)
}
// Show displays all tables in the database.
func (m *Migrator) Show(opt command.ShowOption) error {
return command.HandleShow(opt)
}