forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.go
104 lines (89 loc) · 3.04 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pop
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"time"
"github.com/pkg/errors"
)
// MigrationCreate writes contents for a given migration in normalized files
func MigrationCreate(path, name, ext string, up, down []byte) error {
n := time.Now().UTC()
s := n.Format("20060102150405")
err := os.MkdirAll(path, 0766)
if err != nil {
return errors.Wrapf(err, "couldn't create migrations path %s", path)
}
upf := filepath.Join(path, (fmt.Sprintf("%s_%s.up.%s", s, name, ext)))
err = ioutil.WriteFile(upf, up, 0666)
if err != nil {
return errors.Wrapf(err, "couldn't write up migration %s", upf)
}
fmt.Printf("> %s\n", upf)
downf := filepath.Join(path, (fmt.Sprintf("%s_%s.down.%s", s, name, ext)))
err = ioutil.WriteFile(downf, down, 0666)
if err != nil {
return errors.Wrapf(err, "couldn't write up migration %s", downf)
}
fmt.Printf("> %s\n", downf)
return nil
}
// MigrateUp is deprecated, and will be removed in a future version. Use FileMigrator#Up instead.
func (c *Connection) MigrateUp(path string) error {
warningMsg := "Connection#MigrateUp is deprecated, and will be removed in a future version. Use FileMigrator#Up instead."
_, file, no, ok := runtime.Caller(1)
if ok {
warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no)
}
log.Println(warningMsg)
mig, err := NewFileMigrator(path, c)
if err != nil {
return errors.WithStack(err)
}
return mig.Up()
}
// MigrateDown is deprecated, and will be removed in a future version. Use FileMigrator#Down instead.
func (c *Connection) MigrateDown(path string, step int) error {
warningMsg := "Connection#MigrateDown is deprecated, and will be removed in a future version. Use FileMigrator#Down instead."
_, file, no, ok := runtime.Caller(1)
if ok {
warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no)
}
log.Println(warningMsg)
mig, err := NewFileMigrator(path, c)
if err != nil {
return errors.WithStack(err)
}
return mig.Down(step)
}
// MigrateStatus is deprecated, and will be removed in a future version. Use FileMigrator#Status instead.
func (c *Connection) MigrateStatus(path string) error {
warningMsg := "Connection#MigrateStatus is deprecated, and will be removed in a future version. Use FileMigrator#Status instead."
_, file, no, ok := runtime.Caller(1)
if ok {
warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no)
}
log.Println(warningMsg)
mig, err := NewFileMigrator(path, c)
if err != nil {
return errors.WithStack(err)
}
return mig.Status()
}
// MigrateReset is deprecated, and will be removed in a future version. Use FileMigrator#Reset instead.
func (c *Connection) MigrateReset(path string) error {
warningMsg := "Connection#MigrateReset is deprecated, and will be removed in a future version. Use FileMigrator#Reset instead."
_, file, no, ok := runtime.Caller(1)
if ok {
warningMsg = fmt.Sprintf("%s Called from %s:%d", warningMsg, file, no)
}
log.Println(warningMsg)
mig, err := NewFileMigrator(path, c)
if err != nil {
return errors.WithStack(err)
}
return mig.Reset()
}