Reorganized code + migration to go1.22

This commit is contained in:
brajkovic committed 2024-04-01 20:29:24 +02:00
1 parent e3d77d55b9
commit 7369777098
25 files changed
+825 -605

No files matched your search

+9 -7
View File
@@ -4,10 +4,11 @@ import (
"context"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
"io/fs"
"log"
"log/slog"
"sort"
"strings"
"time"
@@ -92,7 +93,8 @@ func validateMigrations(db *sqlx.DB, migrations map[string]Migration, migrationF
}
func executeMigration(db *sqlx.DB, name string, script string) error {
log.Printf("[INFO] script='%s' | migrations - executing", name)
logger := slog.Default().With(slog.String("script", name))
logger.Info("migrations - executing")
tx := db.MustBeginTx(context.Background(), nil)
var err error = nil
if _, e := tx.Exec(script); e != nil {
@@ -102,10 +104,10 @@ func executeMigration(db *sqlx.DB, name string, script string) error {
err = e
}
if err != nil {
log.Printf("[ERROR] script='%s' | migrations - failed executing", name)
logger.Error("migrations - failed executing", slog.String("err", err.Error()))
tx.Rollback()
} else {
log.Printf("[INFO] script='%s' | migrations - succesfully executed", name)
logger.Info("migrations - successfully executed")
tx.Commit()
}
return err
@@ -119,9 +121,9 @@ func validateMigration(name string, migration Migration, script string) error {
calculatedHash := hash(script)
if calculatedHash != migration.Hash {
err := fmt.Sprintf("migrations - mismatch in hash for %s (expected '%s', calculated '%s')", name, migration.Hash, calculatedHash)
log.Printf("[ERROR] script='%s' err='%s' | migrations - failed executing", script, err)
return fmt.Errorf("migrations - mismatch in hashes for %s", name)
err := errors.New(fmt.Sprintf("migrations - mismatch in hash for %s (expected '%s', calculated '%s')", name, migration.Hash, calculatedHash))
slog.Error("migrations - failed validation", slog.String("script", name), slog.String("err", err.Error()))
return err
}
return nil
}