added migrate history-sync command

This commit is contained in:
Gani Georgiev
2023-03-25 21:48:19 +02:00
parent e5a22b8bd8
commit 5678339af0
4 changed files with 76 additions and 0 deletions

View File

@@ -107,6 +107,14 @@ func (r *Runner) Run(args ...string) error {
}
}
return nil
case "history-sync":
if err := r.removeMissingAppliedMigrations(); err != nil {
color.Red(err.Error())
return err
}
color.Green("The %s table was synced with the available migrations.", r.tableName)
return nil
default:
return fmt.Errorf("Unsupported command: %q\n", cmd)
@@ -252,3 +260,18 @@ func (r *Runner) lastAppliedMigrations(limit int) ([]string, error) {
return files, nil
}
func (r *Runner) removeMissingAppliedMigrations() error {
loadedMigrations := r.migrationsList.Items()
names := make([]any, len(loadedMigrations))
for i, migration := range loadedMigrations {
names[i] = migration.File
}
_, err := r.db.Delete(r.tableName, dbx.Not(dbx.HashExp{
"file": names,
})).Execute()
return err
}