Skip to content

Commit

Permalink
fix(migrate): added module name in migration (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi authored Mar 28, 2024
1 parent 3bd191a commit 1acbdae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
11 changes: 7 additions & 4 deletions migrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (

const TABLE_MIGRATIONS = "CREATE TABLE IF NOT EXISTS sqle_migrations(" +
"`checksum` varchar(32) NOT NULL," +
"`module` varchar(45) NOT NULL," +
"`version` varchar(45) NOT NULL," +
"`name` varchar(45) NOT NULL," +
"`rank` int NOT NULL DEFAULT '0'," +
Expand All @@ -43,6 +44,7 @@ const TABLE_ROTATIONS = "CREATE TABLE IF NOT EXISTS sqle_rotations(" +
type Migrator struct {
dbs []*sqle.DB
suffix string
module string

Versions []Semver
MonthlyRotations []Rotation
Expand Down Expand Up @@ -212,9 +214,9 @@ func (m *Migrator) Migrate(ctx context.Context) error {
n := len(m.dbs)
for i, db := range m.dbs {
if n == 1 {
log.Println("migrate:")
log.Printf("migrate: %s\n", m.module)
} else {
log.Printf("migrate db%v:\n", i)
log.Printf("migrate db-%v: %s\n", i, m.module)
}

err = m.startMigrate(ctx, db)
Expand Down Expand Up @@ -266,6 +268,7 @@ func (m *Migrator) startMigrate(ctx context.Context, db *sqle.DB) error {
et := round(time.Since(now)).String()
cmd.Insert("sqle_migrations").
Set("checksum", s.Checksum).
Set("module", m.module).
Set("version", v.Name).
Set("name", s.Name).
Set("rank", s.Rank).
Expand Down Expand Up @@ -345,9 +348,9 @@ func (m *Migrator) Rotate(ctx context.Context) error {
n := len(m.dbs)
for i, db := range m.dbs {
if n == 1 {
log.Println("rotate:")
log.Printf("rotate: %s\n", m.module)
} else {
log.Printf("rotate db%v:\n", i)
log.Printf("rotate db-%v: %s\n", i, m.module)
}

now := m.now().UTC()
Expand Down
2 changes: 1 addition & 1 deletion migrate/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func TestMigrate(t *testing.T) {
PRIMARY KEY (id)
);`),
},
})
}, WithModule("tests"))

if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions migrate/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ func WithSuffix(suffix string) Option {
}
}
}

func WithModule(name string) Option {
return func(m *Migrator) {
m.module = name
}
}

0 comments on commit 1acbdae

Please sign in to comment.