-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
52 lines (43 loc) · 1021 Bytes
/
migration.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
package data
import (
"database/sql"
"log"
migrate "github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
bindata "github.com/golang-migrate/migrate/v4/source/go_bindata"
// Interfacing with the sqlite3 database
_ "github.com/mattn/go-sqlite3"
)
func (d *Data) migrate() error {
db, err := sql.Open("sqlite3", "./.audiofile.db")
if err != nil {
log.Fatalf("Couldn't open DB: %s", err)
}
source, err := bindata.WithInstance(bindata.Resource(
AssetNames(),
func(name string) ([]byte, error) {
return Asset(name)
},
))
if err != nil {
log.Fatalf("Unable to work with bindata: %s", err)
}
data, err := sqlite3.WithInstance(db, &sqlite3.Config{})
if err != nil {
log.Fatalf("Couldn't connect: %s", err)
}
m, err := migrate.NewWithInstance(
"go-bindata",
source,
"sqlite3",
data,
)
if err != nil {
log.Fatalf("Unable to migrate: %s", err)
}
err = m.Up()
if err != nil && err.Error() != "no change" {
return err
}
return nil
}