-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-file.go
71 lines (58 loc) · 1.49 KB
/
create-file.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
package migrate
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
)
const (
defaultTimeFormat = "20060102150405"
defaultTimezone = "UTC"
defaultExt = "sql"
)
// CreateCmd (meant to be called via a CLI command) creates a new migration
func CreateCmd(dir string, name string, print bool) error {
dir = filepath.Clean(dir)
ext := "." + strings.TrimPrefix(defaultExt, ".")
currentTime := time.Now()
timezone, err := time.LoadLocation(defaultTimezone)
if err != nil {
log.Fatal(err)
}
startTime := currentTime.In(timezone)
version := startTime.Format(defaultTimeFormat)
versionGlob := filepath.Join(dir, version+"_*"+ext)
matches, err := filepath.Glob(versionGlob)
if err != nil {
return err
}
if len(matches) > 0 {
return fmt.Errorf("duplicate migration version: %s", version)
}
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
for _, direction := range []string{"up", "down"} {
basename := fmt.Sprintf("%s_%s.%s%s", version, name, direction, ext)
filename := filepath.Join(dir, basename)
if err = createFile(filename); err != nil {
return err
}
if print {
absPath, _ := filepath.Abs(filename)
log.Println(absPath)
}
}
return nil
}
func createFile(filename string) error {
// create exclusive (fails if file already exists)
// os.Create() specifies 0666 as the FileMode, so we're doing the same
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return err
}
return f.Close()
}