-
Notifications
You must be signed in to change notification settings - Fork 10
/
migration.go
51 lines (42 loc) · 965 Bytes
/
migration.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
package infrastructure
import (
"clean_gin_api/lib"
migrate "github.com/rubenv/sql-migrate"
)
// Migrations -> Migration Struct
type Migrations struct {
logger lib.Logger
db Database
}
// NewMigrations -> return new Migrations struct
func NewMigrations(
logger lib.Logger,
db Database,
) Migrations {
return Migrations{
logger: logger,
db: db,
}
}
// Migrate migrates all migrations that are defined
func (m Migrations) Migrate() error {
migrations := &migrate.FileMigrationSource{
Dir: "migration/",
}
sqlDB, err := m.db.DB.DB()
if err != nil {
return err
}
m.logger.Info("running migration.")
_, err = migrate.Exec(sqlDB, "mysql", migrations, migrate.Up)
if err != nil {
return err
}
m.logger.Info("migration completed.")
return nil
}
// RunMigration runs the migration provided logger and database instance
func RunMigration(logger lib.Logger, db Database) error {
m := Migrations{logger, db}
return m.Migrate()
}