forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
119 lines (106 loc) · 2.34 KB
/
storage.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
package googlecloudstorage
import (
"fmt"
"io"
"net/url"
"os"
"path"
"strings"
"cloud.google.com/go/storage"
"context"
"github.com/golang-migrate/migrate/v4/source"
"google.golang.org/api/iterator"
)
func init() {
source.Register("gcs", &gcs{})
}
type gcs struct {
bucket *storage.BucketHandle
prefix string
migrations *source.Migrations
}
func (g *gcs) Open(folder string) (source.Driver, error) {
u, err := url.Parse(folder)
if err != nil {
return nil, err
}
client, err := storage.NewClient(context.Background())
if err != nil {
return nil, err
}
driver := gcs{
bucket: client.Bucket(u.Host),
prefix: strings.Trim(u.Path, "/") + "/",
migrations: source.NewMigrations(),
}
err = driver.loadMigrations()
if err != nil {
return nil, err
}
return &driver, nil
}
func (g *gcs) loadMigrations() error {
iter := g.bucket.Objects(context.Background(), &storage.Query{
Prefix: g.prefix,
Delimiter: "/",
})
object, err := iter.Next()
for ; err == nil; object, err = iter.Next() {
_, fileName := path.Split(object.Name)
m, parseErr := source.DefaultParse(fileName)
if parseErr != nil {
continue
}
if !g.migrations.Append(m) {
return fmt.Errorf("unable to parse file %v", object.Name)
}
}
if err != iterator.Done {
return err
}
return nil
}
func (g *gcs) Close() error {
return nil
}
func (g *gcs) First() (uint, error) {
v, ok := g.migrations.First()
if !ok {
return 0, os.ErrNotExist
}
return v, nil
}
func (g *gcs) Prev(version uint) (uint, error) {
v, ok := g.migrations.Prev(version)
if !ok {
return 0, os.ErrNotExist
}
return v, nil
}
func (g *gcs) Next(version uint) (uint, error) {
v, ok := g.migrations.Next(version)
if !ok {
return 0, os.ErrNotExist
}
return v, nil
}
func (g *gcs) ReadUp(version uint) (io.ReadCloser, string, error) {
if m, ok := g.migrations.Up(version); ok {
return g.open(m)
}
return nil, "", os.ErrNotExist
}
func (g *gcs) ReadDown(version uint) (io.ReadCloser, string, error) {
if m, ok := g.migrations.Down(version); ok {
return g.open(m)
}
return nil, "", os.ErrNotExist
}
func (g *gcs) open(m *source.Migration) (io.ReadCloser, string, error) {
objectPath := path.Join(g.prefix, m.Raw)
reader, err := g.bucket.Object(objectPath).NewReader(context.Background())
if err != nil {
return nil, "", err
}
return reader, m.Identifier, nil
}