fix: verify server snapshots and recover whole migration sessions (#3)

This commit is contained in:
Mikhail Chusavitin committed 2026-10-26 22:48:07 +03:00
1 parent 3d4101ecc0
commit 8569871a17
29 files changed
+1923 -133

No files matched your search

-72
View File
@@ -1,7 +1,6 @@
package models
import (
"bufio"
"errors"
"fmt"
"io/fs"
@@ -124,48 +123,6 @@ func NeedsSQLMigrations(db *gorm.DB, migrations fs.FS) (bool, error) {
return count < int64(len(files)), nil
}
// RunSQLMigrations applies pending SQL files from migrations once each, in filename
// order, and records them in qt_schema_migrations. onApply (optional) is called with
// each filename before it runs. Local SQLite-only scripts are skipped automatically.
// Returns the filenames applied.
func RunSQLMigrations(db *gorm.DB, migrations fs.FS, onApply func(filename string)) ([]string, error) {
if err := ensureSQLMigrationsTable(db); err != nil {
return nil, fmt.Errorf("migrate qt_schema_migrations table: %w", err)
}
files, err := listSQLMigrationFiles(migrations)
if err != nil {
return nil, err
}
var applied []string
for _, filename := range files {
var count int64
if err := db.Model(&SQLSchemaMigration{}).Where("filename = ?", filename).Count(&count).Error; err != nil {
return applied, fmt.Errorf("check migration %s: %w", filename, err)
}
if count > 0 {
continue
}
if onApply != nil {
onApply(filename)
}
content, err := fs.ReadFile(migrations, filename)
if err != nil {
return applied, fmt.Errorf("read migration %s: %w", filename, err)
}
if err := applySQLMigrationFile(db, filename, string(content)); err != nil {
return applied, err
}
applied = append(applied, filename)
}
return applied, nil
}
// IsMigrationPermissionError returns true if err indicates insufficient privileges
// to create/alter/read migration metadata or target schema objects.
func IsMigrationPermissionError(err error) bool {
@@ -239,35 +196,6 @@ func isIgnorableMigrationError(message string) bool {
return false
}
func splitSQLStatements(script string) []string {
scanner := bufio.NewScanner(strings.NewReader(script))
scanner.Buffer(make([]byte, 1024), 1024*1024)
lines := make([]string, 0, 128)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if strings.HasPrefix(line, "--") {
continue
}
lines = append(lines, scanner.Text())
}
combined := strings.Join(lines, "\n")
raw := strings.Split(combined, ";")
stmts := make([]string, 0, len(raw))
for _, stmt := range raw {
trimmed := strings.TrimSpace(stmt)
if trimmed == "" {
continue
}
stmts = append(stmts, trimmed)
}
return stmts
}
func listSQLMigrationFiles(migrations fs.FS) ([]string, error) {
entries, err := fs.ReadDir(migrations, ".")
if err != nil {