2019-02-09 12:30:49 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gobuffalo/packr/v2"
|
|
|
|
"github.com/golang-migrate/migrate/v4"
|
|
|
|
"github.com/golang-migrate/migrate/v4/source"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2019-02-10 02:53:08 +00:00
|
|
|
"github.com/stashapp/stash/logger"
|
2019-02-10 00:50:06 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var DB *sqlx.DB
|
2019-02-10 00:50:06 +00:00
|
|
|
var appSchemaVersion uint = 1
|
2019-02-09 12:30:49 +00:00
|
|
|
|
|
|
|
func Initialize(databasePath string) {
|
|
|
|
runMigrations(databasePath)
|
|
|
|
|
|
|
|
// https://github.com/mattn/go-sqlite3
|
|
|
|
conn, err := sqlx.Open("sqlite3", "file:"+databasePath+"?_fk=true")
|
|
|
|
conn.SetMaxOpenConns(10)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("db.Open(): %q\n", err)
|
|
|
|
}
|
|
|
|
DB = conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func Reset(databasePath string) {
|
|
|
|
_, _ = DB.Exec("PRAGMA writable_schema = 1;")
|
|
|
|
_, _ = DB.Exec("delete from sqlite_master where type in ('table', 'index', 'trigger');")
|
|
|
|
_, _ = DB.Exec("PRAGMA writable_schema = 0;")
|
|
|
|
_, _ = DB.Exec("VACUUM;")
|
|
|
|
_, _ = DB.Exec("PRAGMA INTEGRITY_CHECK;")
|
|
|
|
runMigrations(databasePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate the database
|
|
|
|
func runMigrations(databasePath string) {
|
|
|
|
migrationsBox := packr.New("Migrations Box", "./migrations")
|
|
|
|
packrSource := &Packr2Source{
|
|
|
|
Box: migrationsBox,
|
|
|
|
Migrations: source.NewMigrations(),
|
|
|
|
}
|
2019-02-10 00:50:06 +00:00
|
|
|
|
|
|
|
// Golang migrate has an issue where the \ isn't recognized as a valid on windows
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
databasePath = strings.Replace(databasePath, "\\", "/", -1)
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
s, _ := WithInstance(packrSource)
|
|
|
|
m, err := migrate.NewWithSourceInstance(
|
|
|
|
"packr2",
|
|
|
|
s,
|
2019-02-10 00:50:06 +00:00
|
|
|
fmt.Sprintf("sqlite3://%s", "file:"+databasePath),
|
2019-02-09 12:30:49 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
|
2019-02-10 00:50:06 +00:00
|
|
|
databaseSchemaVersion, _, _ := m.Version()
|
|
|
|
stepNumber := appSchemaVersion - databaseSchemaVersion
|
|
|
|
if stepNumber != 0 {
|
|
|
|
err = m.Steps(int(stepNumber))
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
}
|