mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2024-11-10 08:57:25 +01:00
Add migration and introduce dirty flag handling
This commit is contained in:
parent
de1d83e1a9
commit
105b7eabf0
@ -134,7 +134,7 @@ func initEnv() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var flagReinitDB, flagInit, flagServer, flagSyncLDAP, flagGops, flagMigrateDB, flagDev, flagVersion, flagLogDateTime bool
|
var flagReinitDB, flagInit, flagServer, flagSyncLDAP, flagGops, flagMigrateDB, flagForceDB, flagDev, flagVersion, flagLogDateTime bool
|
||||||
var flagNewUser, flagDelUser, flagGenJWT, flagConfigFile, flagImportJob, flagLogLevel string
|
var flagNewUser, flagDelUser, flagGenJWT, flagConfigFile, flagImportJob, flagLogLevel string
|
||||||
flag.BoolVar(&flagInit, "init", false, "Setup var directory, initialize swlite database file, config.json and .env")
|
flag.BoolVar(&flagInit, "init", false, "Setup var directory, initialize swlite database file, config.json and .env")
|
||||||
flag.BoolVar(&flagReinitDB, "init-db", false, "Go through job-archive and re-initialize the 'job', 'tag', and 'jobtag' tables (all running jobs will be lost!)")
|
flag.BoolVar(&flagReinitDB, "init-db", false, "Go through job-archive and re-initialize the 'job', 'tag', and 'jobtag' tables (all running jobs will be lost!)")
|
||||||
@ -144,6 +144,7 @@ func main() {
|
|||||||
flag.BoolVar(&flagDev, "dev", false, "Enable development components: GraphQL Playground and Swagger UI")
|
flag.BoolVar(&flagDev, "dev", false, "Enable development components: GraphQL Playground and Swagger UI")
|
||||||
flag.BoolVar(&flagVersion, "version", false, "Show version information and exit")
|
flag.BoolVar(&flagVersion, "version", false, "Show version information and exit")
|
||||||
flag.BoolVar(&flagMigrateDB, "migrate-db", false, "Migrate database to supported version and exit")
|
flag.BoolVar(&flagMigrateDB, "migrate-db", false, "Migrate database to supported version and exit")
|
||||||
|
flag.BoolVar(&flagForceDB, "force-db", false, "Force database version, clear dirty flag and exit")
|
||||||
flag.BoolVar(&flagLogDateTime, "logdate", false, "Set this flag to add date and time to log messages")
|
flag.BoolVar(&flagLogDateTime, "logdate", false, "Set this flag to add date and time to log messages")
|
||||||
flag.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`")
|
flag.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`")
|
||||||
flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `<username>:[admin,support,manager,api,user]:<password>`")
|
flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `<username>:[admin,support,manager,api,user]:<password>`")
|
||||||
@ -205,6 +206,14 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if flagForceDB {
|
||||||
|
err := repository.ForceDB(config.Keys.DBDriver, config.Keys.DB)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
repository.Connect(config.Keys.DBDriver, config.Keys.DB)
|
repository.Connect(config.Keys.DBDriver, config.Keys.DB)
|
||||||
db := repository.GetConnection()
|
db := repository.GetConnection()
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Version uint = 6
|
const Version uint = 7
|
||||||
|
|
||||||
//go:embed migrations/*
|
//go:embed migrations/*
|
||||||
var migrationFiles embed.FS
|
var migrationFiles embed.FS
|
||||||
@ -57,7 +57,7 @@ func checkDBVersion(backend string, db *sql.DB) error {
|
|||||||
log.Fatalf("unsupported database backend: %s", backend)
|
log.Fatalf("unsupported database backend: %s", backend)
|
||||||
}
|
}
|
||||||
|
|
||||||
v, _, err := m.Version()
|
v, dirty, err := m.Version()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == migrate.ErrNilVersion {
|
if err == migrate.ErrNilVersion {
|
||||||
log.Warn("Legacy database without version or missing database file!")
|
log.Warn("Legacy database without version or missing database file!")
|
||||||
@ -68,18 +68,18 @@ func checkDBVersion(backend string, db *sql.DB) error {
|
|||||||
|
|
||||||
if v < Version {
|
if v < Version {
|
||||||
return fmt.Errorf("unsupported database version %d, need %d.\nPlease backup your database file and run cc-backend -migrate-db", v, Version)
|
return fmt.Errorf("unsupported database version %d, need %d.\nPlease backup your database file and run cc-backend -migrate-db", v, Version)
|
||||||
|
} 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
if v > Version {
|
if dirty {
|
||||||
return fmt.Errorf("unsupported database version %d, need %d.\nPlease refer to documentation how to downgrade db with external migrate tool", v, Version)
|
return fmt.Errorf("last migration to version %d has failed, please fix the db manually and force version with -force-db flag", Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MigrateDB(backend string, db string) error {
|
func getMigrateInstance(backend string, db string) (m *migrate.Migrate, err error) {
|
||||||
var m *migrate.Migrate
|
|
||||||
|
|
||||||
switch backend {
|
switch backend {
|
||||||
case "sqlite3":
|
case "sqlite3":
|
||||||
d, err := iofs.New(migrationFiles, "migrations/sqlite3")
|
d, err := iofs.New(migrationFiles, "migrations/sqlite3")
|
||||||
@ -89,22 +89,31 @@ func MigrateDB(backend string, db string) error {
|
|||||||
|
|
||||||
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("sqlite3://%s?_foreign_keys=on", db))
|
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("sqlite3://%s?_foreign_keys=on", db))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return m, err
|
||||||
}
|
}
|
||||||
case "mysql":
|
case "mysql":
|
||||||
d, err := iofs.New(migrationFiles, "migrations/mysql")
|
d, err := iofs.New(migrationFiles, "migrations/mysql")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return m, err
|
||||||
}
|
}
|
||||||
|
|
||||||
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("mysql://%s?multiStatements=true", db))
|
m, err = migrate.NewWithSourceInstance("iofs", d, fmt.Sprintf("mysql://%s?multiStatements=true", db))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return m, err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Fatalf("unsupported database backend: %s", backend)
|
log.Fatalf("unsupported database backend: %s", backend)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func MigrateDB(backend string, db string) error {
|
||||||
|
m, err := getMigrateInstance(backend, db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := m.Up(); err != nil {
|
if err := m.Up(); err != nil {
|
||||||
if err == migrate.ErrNoChange {
|
if err == migrate.ErrNoChange {
|
||||||
log.Info("DB already up to date!")
|
log.Info("DB already up to date!")
|
||||||
@ -116,3 +125,17 @@ func MigrateDB(backend string, db string) error {
|
|||||||
m.Close()
|
m.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -36,7 +36,7 @@ CREATE TABLE IF NOT EXISTS job (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS tag (
|
CREATE TABLE IF NOT EXISTS tag (
|
||||||
id INTEGER AUTO_INCREMENT PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
tag_type VARCHAR(255) NOT NULL,
|
tag_type VARCHAR(255) NOT NULL,
|
||||||
tag_name VARCHAR(255) NOT NULL,
|
tag_name VARCHAR(255) NOT NULL,
|
||||||
UNIQUE (tag_type, tag_name));
|
UNIQUE (tag_type, tag_name));
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE tag MODIFY id INTEGER;
|
@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE tag MODIFY id INTEGER AUTO_INCREMENT;
|
Loading…
Reference in New Issue
Block a user