-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrator.go
More file actions
56 lines (41 loc) · 1 KB
/
migrator.go
File metadata and controls
56 lines (41 loc) · 1 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
package exporter
import (
"database/sql"
"github.com/remind101/migrate"
"github.com/rafalmnich/exporter/migration"
)
import _ "github.com/jinzhu/gorm/dialects/postgres"
//go:generate go run ./migration/routine/generator.go
type Migrator struct {
db *sql.DB
schema *Schema
migrator *migrate.Migrator
}
type Schema struct{}
func (s *Schema) migrations() []migrate.Migration {
return migration.Migrations
}
func NewMigrator(db *sql.DB) *Migrator {
m := migrate.NewPostgresMigrator(db)
m.TransactionMode = migrate.SingleTransaction
return &Migrator{
db: db,
schema: &Schema{},
migrator: m,
}
}
func (m *Migrator) MigrateUp() error {
return m.migrator.Exec(migrate.Up, m.migrations()...)
}
func (m *Migrator) MigrateDown() error {
return m.migrator.Exec(migrate.Down, m.migrations()...)
}
func (m *Migrator) migrations() []migrate.Migration {
return m.schema.migrations()
}
func (m *Migrator) IsHealthy() error {
if err := m.db.Ping(); err != nil {
return err
}
return nil
}