2024-04-11 23:04:30 +02:00
|
|
|
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
2023-02-21 10:57:22 +01:00
|
|
|
// 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"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2024-03-08 10:47:32 +01:00
|
|
|
const Version uint = 7
|
2023-02-21 10:57:22 +01:00
|
|
|
|
2023-02-21 14:45:59 +01:00
|
|
|
//go:embed migrations/*
|
2023-02-21 10:57:22 +01:00
|
|
|
var migrationFiles embed.FS
|
|
|
|
|
2023-04-21 12:59:27 +02:00
|
|
|
func checkDBVersion(backend string, db *sql.DB) error {
|
2023-02-21 14:45:59 +01:00
|
|
|
var m *migrate.Migrate
|
2023-02-21 10:57:22 +01:00
|
|
|
|
2023-05-04 15:34:36 +02:00
|
|
|
switch backend {
|
|
|
|
case "sqlite3":
|
2023-02-21 14:45:59 +01:00
|
|
|
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
|
|
|
|
if err != nil {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
|
|
|
d, err := iofs.New(migrationFiles, "migrations/sqlite3")
|
|
|
|
if err != nil {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err = migrate.NewWithInstance("iofs", d, "sqlite3", driver)
|
|
|
|
if err != nil {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
2023-05-04 15:34:36 +02:00
|
|
|
case "mysql":
|
2023-02-21 14:45:59 +01:00
|
|
|
driver, err := mysql.WithInstance(db, &mysql.Config{})
|
|
|
|
if err != nil {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
|
|
|
d, err := iofs.New(migrationFiles, "migrations/mysql")
|
|
|
|
if err != nil {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err = migrate.NewWithInstance("iofs", d, "mysql", driver)
|
|
|
|
if err != nil {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
2023-06-28 12:41:27 +02:00
|
|
|
default:
|
|
|
|
log.Fatalf("unsupported database backend: %s", backend)
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
|
|
|
|
2024-03-08 10:47:32 +01:00
|
|
|
v, dirty, err := m.Version()
|
2023-02-21 10:57:22 +01:00
|
|
|
if err != nil {
|
|
|
|
if err == migrate.ErrNilVersion {
|
|
|
|
log.Warn("Legacy database without version or missing database file!")
|
|
|
|
} else {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:43:46 +02:00
|
|
|
if v < Version {
|
2023-06-27 14:30:09 +02:00
|
|
|
return fmt.Errorf("unsupported database version %d, need %d.\nPlease backup your database file and run cc-backend -migrate-db", v, Version)
|
2024-03-08 10:47:32 +01:00
|
|
|
} else if v > Version {
|
|
|
|
return fmt.Errorf("unsupported database version %d, need %d.\nPlease refer to documentation how to downgrade db with external migrate tool", v, Version)
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
2023-02-21 13:50:15 +01:00
|
|
|
|
2024-03-08 10:47:32 +01:00
|
|
|
if dirty {
|
|
|
|
return fmt.Errorf("last migration to version %d has failed, please fix the db manually and force version with -force-db flag", Version)
|
2023-02-21 13:50:15 +01:00
|
|
|
}
|
2023-04-21 12:59:27 +02:00
|
|
|
|
|
|
|
return nil
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
|
|
|
|
2024-03-08 10:47:32 +01:00
|
|
|
func getMigrateInstance(backend string, db string) (m *migrate.Migrate, err error) {
|
2023-05-04 15:34:36 +02:00
|
|
|
switch backend {
|
|
|
|
case "sqlite3":
|
2023-02-21 14:45:59 +01:00
|
|
|
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 {
|
2024-03-08 10:47:32 +01:00
|
|
|
return m, err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
2023-05-04 15:34:36 +02:00
|
|
|
case "mysql":
|
2023-02-21 14:45:59 +01:00
|
|
|
d, err := iofs.New(migrationFiles, "migrations/mysql")
|
|
|
|
if err != nil {
|
2024-03-08 10:47:32 +01:00
|
|
|
return m, err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("mysql://%s?multiStatements=true", db))
|
|
|
|
if err != nil {
|
2024-03-08 10:47:32 +01:00
|
|
|
return m, err
|
2023-02-21 14:45:59 +01:00
|
|
|
}
|
2023-06-28 12:41:27 +02:00
|
|
|
default:
|
|
|
|
log.Fatalf("unsupported database backend: %s", backend)
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
2023-02-21 14:45:59 +01:00
|
|
|
|
2024-03-08 10:47:32 +01:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func MigrateDB(backend string, db string) error {
|
|
|
|
m, err := getMigrateInstance(backend, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:28:26 +01:00
|
|
|
if err := m.Up(); err != nil {
|
|
|
|
if err == migrate.ErrNoChange {
|
|
|
|
log.Info("DB already up to date!")
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RevertDB(backend string, db string) error {
|
|
|
|
m, err := getMigrateInstance(backend, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Migrate(Version - 1); err != nil {
|
2023-02-25 08:26:56 +01:00
|
|
|
if err == migrate.ErrNoChange {
|
|
|
|
log.Info("DB already up to date!")
|
|
|
|
} else {
|
2023-04-21 12:59:27 +02:00
|
|
|
return err
|
2023-02-25 08:26:56 +01:00
|
|
|
}
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.Close()
|
2023-04-21 12:59:27 +02:00
|
|
|
return nil
|
2023-02-21 10:57:22 +01:00
|
|
|
}
|
2024-03-08 10:47:32 +01:00
|
|
|
|
|
|
|
func ForceDB(backend string, db string) error {
|
|
|
|
m, err := getMigrateInstance(backend, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.Force(int(Version)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Close()
|
|
|
|
return nil
|
|
|
|
}
|