Skip to content

Commit

Permalink
feat: Allow to revert db to previous version
Browse files Browse the repository at this point in the history
  • Loading branch information
moebiusband73 committed Mar 8, 2024
1 parent 2c2c1ac commit 06d0196
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cmd/cc-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func initEnv() {
}

func main() {
var flagReinitDB, flagInit, flagServer, flagSyncLDAP, flagGops, flagMigrateDB, flagForceDB, flagDev, flagVersion, flagLogDateTime bool
var flagReinitDB, flagInit, flagServer, flagSyncLDAP, flagGops, flagMigrateDB, flagRevertDB, flagForceDB, flagDev, flagVersion, flagLogDateTime bool
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(&flagReinitDB, "init-db", false, "Go through job-archive and re-initialize the 'job', 'tag', and 'jobtag' tables (all running jobs will be lost!)")
Expand All @@ -144,6 +144,7 @@ func main() {
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(&flagMigrateDB, "migrate-db", false, "Migrate database to supported version and exit")
flag.BoolVar(&flagRevertDB, "revert-db", false, "Migrate database to previous 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.StringVar(&flagConfigFile, "config", "./config.json", "Specify alternative path to `config.json`")
Expand Down Expand Up @@ -206,6 +207,14 @@ func main() {
os.Exit(0)
}

if flagRevertDB {
err := repository.RevertDB(config.Keys.DBDriver, config.Keys.DB)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}

if flagForceDB {
err := repository.ForceDB(config.Keys.DBDriver, config.Keys.DB)
if err != nil {
Expand Down
20 changes: 19 additions & 1 deletion internal/repository/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,25 @@ func MigrateDB(backend string, db string) error {
return err
}

if err := m.Migrate(Version); err != nil {
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 {
if err == migrate.ErrNoChange {
log.Info("DB already up to date!")
} else {
Expand Down

0 comments on commit 06d0196

Please sign in to comment.