-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.go
46 lines (39 loc) · 1.06 KB
/
migrate.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
package database
import (
"fmt"
"app/api/pkg/logger"
"github.com/jmoiron/sqlx"
"github.com/josephspurrier/rove"
"github.com/josephspurrier/rove/pkg/adapter/mysql"
)
// Migrate will run the database migrations and will create the database if it
// does not exist.
func Migrate(l logger.ILog, con *mysql.Connection, changesets string) (*sqlx.DB, error) {
// Connect to the database.
db, err := mysql.New(con)
if err != nil {
// Attempt to connect without the database name.
d, err := con.Connect(false)
if err != nil {
return nil, err
}
// Create the database.
_, err = d.Query(fmt.Sprintf(`CREATE DATABASE IF NOT EXISTS %v
DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;`, con.Name))
if err != nil {
return nil, err
}
if l != nil {
l.Printf("Database created.")
}
// Attempt to reconnect with the database name.
db, err = mysql.New(con)
if err != nil {
return nil, err
}
}
// Perform all migrations against the database.
r := rove.NewChangesetMigration(db, changesets)
r.Verbose = false
return db.DB, r.Migrate(0)
}