/
service.go
150 lines (122 loc) · 3.73 KB
/
service.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/bradfitz/slice"
"github.com/elwinar/rambler/driver"
_ "github.com/elwinar/rambler/driver/mysql"
_ "github.com/elwinar/rambler/driver/postgresql"
_ "github.com/elwinar/rambler/driver/sqlite"
)
var (
// ErrNilMigration is returned when the service is given a nil migration
ErrNilMigration = errors.New("nil migration")
)
// Service is the struct that gather operations to manipulate the
// database and migrations on disk
type Service struct {
conn driver.Conn
env Environment
}
// NewService initialize a new service with the given environment
func NewService(env Environment) (*Service, error) {
fi, err := os.Stat(env.Directory)
if err != nil {
return nil, fmt.Errorf("directory %s unavailable: %s", env.Directory, err.Error())
}
if !fi.Mode().IsDir() {
return nil, fmt.Errorf("%s isn't a directory", env.Directory)
}
conn, err := driver.Get(env.Driver, env.DSN(), env.Database, env.Table)
if err != nil {
return nil, fmt.Errorf("unable to initialize driver: %s", err.Error())
}
return &Service{
conn: conn,
env: env,
}, nil
}
// Initialized check if the migration table exists in the
// database
func (s Service) Initialized() (bool, error) {
return s.conn.HasTable()
}
// Initialize create the migration table in the database
func (s Service) Initialize() error {
return s.conn.CreateTable()
}
// Available return the migrations in the environment's directory sorted in
// ascending lexicographic order.
func (s Service) Available() ([]*Migration, error) {
files, _ := filepath.Glob(filepath.Join(s.env.Directory, "*.sql")) // The only possible error here is a pattern error
var migrations []*Migration
for _, file := range files {
migration, err := NewMigration(file)
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
slice.Sort(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})
return migrations, nil
}
// Applied return the migrations in the environment's directory that are marked
// as applied in the database sorted in ascending lexicographic order.
func (s Service) Applied() ([]*Migration, error) {
files, err := s.conn.GetApplied()
if err != nil {
return nil, err
}
var migrations []*Migration
for _, file := range files {
migration, err := NewMigration(filepath.Join(s.env.Directory, file))
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
slice.Sort(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})
return migrations, nil
}
// Apply execute the up statements of the given migration to the
// database then mark the migration as applied
func (s Service) Apply(migration *Migration) error {
if migration == nil {
return ErrNilMigration
}
for _, statement := range migration.Up() {
err := s.conn.Execute(statement)
if err != nil {
return fmt.Errorf("unable to apply migration %s: %s\n%s", migration.Name, err, statement)
}
}
err := s.conn.AddApplied(migration.Name)
if err != nil {
return fmt.Errorf("unable to mark migration %s as applied: %s", migration.Name, err)
}
return nil
}
// Reverse execute the down statements of the given migration to the
// database then mark the migration as not applied
func (s Service) Reverse(migration *Migration) error {
if migration == nil {
return ErrNilMigration
}
for _, statement := range migration.Down() {
err := s.conn.Execute(statement)
if err != nil {
return fmt.Errorf("unable to reverse migration %s: %s\n%s", migration.Name, err, statement)
}
}
err := s.conn.RemoveApplied(migration.Name)
if err != nil {
return fmt.Errorf("unable to mark migration %s as not applied: %s", migration.Name, err)
}
return nil
}