cc-backend/internal/repository/migration.go

110 lines
2.5 KiB
Go
Raw Normal View History

2023-02-21 10:57:22 +01:00
// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repository
import (
"database/sql"
"embed"
"fmt"
"os"
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/golang-migrate/migrate/v4"
2023-02-21 14:45:59 +01:00
"github.com/golang-migrate/migrate/v4/database/mysql"
2023-02-21 10:57:22 +01:00
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
)
const supportedVersion uint = 2
2023-02-21 14:45:59 +01:00
//go:embed migrations/*
2023-02-21 10:57:22 +01:00
var migrationFiles embed.FS
2023-02-21 14:45:59 +01:00
func checkDBVersion(backend string, db *sql.DB) {
var m *migrate.Migrate
2023-02-21 10:57:22 +01:00
2023-02-21 14:45:59 +01:00
if backend == "sqlite3" {
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
if err != nil {
log.Fatal(err)
}
d, err := iofs.New(migrationFiles, "migrations/sqlite3")
if err != nil {
log.Fatal(err)
}
m, err = migrate.NewWithInstance("iofs", d, "sqlite3", driver)
if err != nil {
log.Fatal(err)
}
} else if backend == "mysql" {
driver, err := mysql.WithInstance(db, &mysql.Config{})
if err != nil {
log.Fatal(err)
}
d, err := iofs.New(migrationFiles, "migrations/mysql")
if err != nil {
log.Fatal(err)
}
m, err = migrate.NewWithInstance("iofs", d, "mysql", driver)
if err != nil {
log.Fatal(err)
}
2023-02-21 10:57:22 +01:00
}
v, _, err := m.Version()
if err != nil {
if err == migrate.ErrNilVersion {
log.Warn("Legacy database without version or missing database file!")
} else {
log.Fatal(err)
}
}
if v < supportedVersion {
log.Warnf("Unsupported database version %d, need %d.\nPlease backup your database file and run cc-backend --migrate-db", v, supportedVersion)
os.Exit(0)
}
2023-02-21 13:50:15 +01:00
if v > supportedVersion {
log.Warnf("Unsupported database version %d, need %d.\nPlease refer to documentation how to downgrade db with external migrate tool!", v, supportedVersion)
os.Exit(0)
}
2023-02-21 10:57:22 +01:00
}
2023-02-21 14:45:59 +01:00
func MigrateDB(backend string, db string) {
var m *migrate.Migrate
2023-02-21 10:57:22 +01:00
2023-02-21 14:45:59 +01:00
if backend == "sqlite3" {
d, err := iofs.New(migrationFiles, "migrations/sqlite3")
if err != nil {
log.Fatal(err)
}
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("sqlite3://%s?_foreign_keys=on", db))
if err != nil {
log.Fatal(err)
}
} else if backend == "mysql" {
d, err := iofs.New(migrationFiles, "migrations/mysql")
if err != nil {
log.Fatal(err)
}
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("mysql://%s?multiStatements=true", db))
if err != nil {
log.Fatal(err)
}
2023-02-21 10:57:22 +01:00
}
2023-02-21 14:45:59 +01:00
2023-02-21 10:57:22 +01:00
if err := m.Up(); err != nil {
log.Fatal(err)
}
m.Close()
}