-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.go
60 lines (51 loc) · 1.14 KB
/
migrations.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
package dbfs
import (
"embed"
"fmt"
"io/fs"
"github.com/GuiaBolso/darwin"
"github.com/hashicorp/go-multierror"
"github.com/jmoiron/sqlx"
)
//go:embed migrations/*.sql
var migrationFiles embed.FS
func getMigrations() (migrations []darwin.Migration, ret error) {
defer func() {
if ret != nil {
migrations = nil
}
}()
readFile := func(fname string) []byte {
data, err := fs.ReadFile(migrationFiles, fname)
if err != nil {
ret = multierror.Append(
ret,
fmt.Errorf("cannot read migration file %s: %w", fname, err),
)
return nil
}
return data
}
migrations = []darwin.Migration{
{
Version: 1.0,
Description: "base database structure for a read only fs implementation",
Script: string(readFile("migrations/01_base_sqlite.sql")),
},
}
return
}
func runMigrations(db *sqlx.DB) (ret error) {
migrations, err := getMigrations()
if err != nil {
return fmt.Errorf("cannot get migrations: %w", err)
}
if err := darwin.Migrate(
darwin.NewGenericDriver(db.DB, darwin.SqliteDialect{}),
migrations,
nil,
); err != nil {
return fmt.Errorf("cannot run database migrations: %w", err)
}
return nil
}