forked from robinjoseph08/go-pg-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.go
53 lines (40 loc) · 1.06 KB
/
create.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
52
53
package migrations
import (
"fmt"
"io/ioutil"
"path"
"time"
)
const timeFormat = "20060102150405"
var template = `package main
import (
"github.com/go-pg/pg/v10/orm"
migrations "github.com/robinjoseph08/go-pg-migrations/v3"
)
func init() {
up := func(db orm.DB) error {
_, err := db.Exec(` + "`" + `
-- SELECT audit.disable_all_tracking();
-- SELECT ensure_updated_at_trigger('public', 'mynewtable');
-- SELECT audit.enable_all_tracking();
` + "`" + `)
return err
}
down := func(db orm.DB) error {
_, err := db.Exec(` + "`" + `
-- SELECT audit.disable_all_tracking();
-- SELECT audit.enable_all_tracking();
` + "`" + `)
return err
}
opts := migrations.MigrationOptions{}
migrations.Register("%s", up, down, opts)
}
`
func create(directory, name string) error {
version := time.Now().UTC().Format(timeFormat)
fullname := fmt.Sprintf("%s_%s", version, name)
filename := path.Join(directory, fmt.Sprintf("%s.go", fullname))
fmt.Printf("Creating %s...\n", filename)
return ioutil.WriteFile(filename, []byte(fmt.Sprintf(template, fullname)), 0644)
}