-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgmig.go
45 lines (37 loc) · 1.02 KB
/
pgmig.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
package pgmig
import (
"github.com/go-pg/pg/v10"
"io/fs"
)
// Create accepts a name and creates up and down migration files.
func Create(migDirPath, name string) error {
return create(migDirPath, name)
}
// Migrator generates and runs migrations
type Migrator struct {
db *pg.DB
migFS fs.FS
}
// New initializes a Migrator with a go-pg connection and
// migrations FS
func New(db *pg.DB, migFS fs.FS) Migrator {
return Migrator{db: db, migFS: migFS}
}
// Run accepts a command and runs the migrations Collection,
// defined through the migration SQL files in the queries dir.
// Commands: init, up, down, version, set_version [version]
func (m Migrator) Run(migrationCmd string) error {
return run(m.db, m.migFS, migrationCmd)
}
// Init creates version info table in the database
func (m Migrator) Init() error {
return m.Run("init")
}
// Migrate runs all new migrations
func (m Migrator) Migrate() error {
return m.Run("up")
}
// Rollback undoes the latest migration
func (m Migrator) Rollback() error {
return m.Run("down")
}