-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_file.go
208 lines (174 loc) · 4.75 KB
/
driver_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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package adapt
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"sort"
"time"
)
// FileDriverOption provides configuration values for a Driver implemented using
// on-disk file storage for meta-data.
type FileDriverOption func(*fileDriver) error
// FileDriverFilePermission sets the file permission used when writing files to
// disk. By default 0600 ist used.
func FileDriverFilePermission(perm os.FileMode) FileDriverOption {
return func(driver *fileDriver) error {
driver.optFilePermission = perm
return nil
}
}
// NewFileDriver returns a Driver from a filename and variadic FileDriverOption that
// can interact with local JSON-file as storage for meta information.
func NewFileDriver(filename string, opts ...FileDriverOption) Driver {
return &fileDriver{
filename: filename,
opts: opts,
optFilePermission: 0600,
}
}
type fileDriver struct {
filename string
opts []FileDriverOption
optFilePermission os.FileMode
log *slog.Logger
}
func (d *fileDriver) Name() string {
return "driver_file"
}
func (d *fileDriver) Init(log *slog.Logger) error {
d.log = log
for _, opt := range d.opts {
err := opt(d)
if err != nil {
d.log.Error("init failed due to option error", "error", err)
return err
}
}
return nil
}
type fileDriverStorage struct {
Migrations []*Migration
}
func (d *fileDriver) readStorage() (*fileDriverStorage, error) {
f, err := os.Open(d.filename)
if err != nil && !os.IsNotExist(err) {
d.log.Error("failed to open file descriptor", "filename", d.filename, "error", err)
return nil, err
}
if os.IsNotExist(err) {
return &fileDriverStorage{Migrations: []*Migration{}}, nil
}
defer func() {
_ = f.Close()
}()
s := &fileDriverStorage{}
err = json.NewDecoder(f).Decode(s)
if err != nil {
d.log.Error("failed to decode fileDriverStorage into memory structure", "filename", d.filename, "error", err)
return nil, err
}
// sort the ordering of our migrations
sort.Slice(s.Migrations, func(i, j int) bool {
return s.Migrations[i].ID < s.Migrations[j].ID
})
return s, nil
}
func (d *fileDriver) writeStorage(s *fileDriverStorage) error {
f, err := os.OpenFile(d.filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, d.optFilePermission)
if err != nil {
d.log.Error("failed to open file descriptor", "filename", d.filename, "error", err)
return err
}
defer func() {
_ = f.Close()
}()
buf, err := json.MarshalIndent(s, "", " ")
if err != nil {
d.log.Error("failed to encode memory structure into json buffer", "error", err)
return err
}
_, err = f.Write(buf)
if err != nil {
d.log.Error("failed to write encoded json buffer", "error", err)
return err
}
return nil
}
func (d *fileDriver) Healthy() error {
// check if filename exists
_, err := os.Stat(d.filename)
if err != nil && !os.IsNotExist(err) {
d.log.Error("cannot stat filename. unknown error", "filename", d.filename, "error", err)
return err
}
// file doesn't exist at the moment - will be created later
if os.IsNotExist(err) {
d.log.Debug("file doesn't exist. will be created on first write", "filename", d.filename)
return nil
}
// read storage to check if we can decode (unmarshal) it.
_, err = d.readStorage()
if err != nil {
return err
}
return nil
}
func (d *fileDriver) SupportsLocks() bool {
// TODO: copy lockedfile package from go's "go" command and enable locking for basic driver
// https://pkg.go.dev/cmd/go/internal/lockedfile
// https://pkg.go.dev/cmd/go/internal/lockedfile/internal/filelock
return false
}
func (d *fileDriver) AcquireLock() error {
d.log.Error("not supported")
panic("not supported")
}
func (d *fileDriver) ReleaseLock() error {
d.log.Error("not supported")
panic("not supported")
}
func (d *fileDriver) ListMigrations() ([]*Migration, error) {
s, err := d.readStorage()
if err != nil {
return nil, err
}
return s.Migrations, nil
}
func (d *fileDriver) AddMigration(migration *Migration) error {
s, err := d.readStorage()
if err != nil {
return err
}
for _, item := range s.Migrations {
if item.ID == migration.ID {
d.log.Error("migration already exists", "migration_id", migration.ID)
return fmt.Errorf("adapt.fileDriver: migration duplication")
}
}
s.Migrations = append(s.Migrations, migration)
return d.writeStorage(s)
}
func (d *fileDriver) SetMigrationToFinished(migrationID string) error {
s, err := d.readStorage()
if err != nil {
return err
}
var set bool
for _, item := range s.Migrations {
if item.ID == migrationID {
now := time.Now().UTC()
item.Finished = &now
set = true
break
}
}
if !set {
d.log.Error("migration not found", "migration_id", migrationID)
return fmt.Errorf("adapt.fileDriver: migration missing")
}
return d.writeStorage(s)
}
func (d *fileDriver) Close() error {
return nil
}