feat: make migrate — серверные SQL-миграции вместо AutoMigrate
`qfs -migrate` теперь применяет только migrations/*.sql (встроены в бинарник) с учётом в qt_schema_migrations и завершается, не запуская приложение. Новые команды: make migrate-status (read-only список applied/pending), make migrate, make migrate-baseline UPTO=<file> (пометить как применённые без выполнения). migrate отказывается работать, если в истории нет ни одной применённой миграции — иначе все файлы пошли бы заново. Удалён GORM AutoMigrate против MariaDB (models.Migrate, SeedCategories): устаревший бинарник v1.18 с -migrate прогнал по боевой базе AutoMigrate своих моделей. Миграции идут через отдельное соединение без read/write таймаутов — 3s-таймаут sync-соединения обрывал backfill в 032. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
259bd48d2e
commit
a2a1904d91
12 files changed
+374
-107
No files matched your search
@@ -4,8 +4,7 @@ import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"io/fs"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -24,10 +23,87 @@ func (SQLSchemaMigration) TableName() string {
|
||||
return "qt_schema_migrations"
|
||||
}
|
||||
|
||||
// NeedsSQLMigrations reports whether at least one SQL migration from migrationsDir
|
||||
// SQLMigrationState is one server migration file and when it was recorded as applied
|
||||
// (nil = pending).
|
||||
type SQLMigrationState struct {
|
||||
Filename string
|
||||
AppliedAt *time.Time
|
||||
}
|
||||
|
||||
// SQLMigrationStatus lists every server migration in migrations with its applied state.
|
||||
// Read-only: does not create qt_schema_migrations.
|
||||
func SQLMigrationStatus(db *gorm.DB, migrations fs.FS) ([]SQLMigrationState, error) {
|
||||
files, err := listSQLMigrationFiles(migrations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
applied := map[string]time.Time{}
|
||||
if db.Migrator().HasTable(&SQLSchemaMigration{}) {
|
||||
var rows []SQLSchemaMigration
|
||||
if err := db.Find(&rows).Error; err != nil {
|
||||
return nil, fmt.Errorf("read qt_schema_migrations: %w", err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
applied[r.Filename] = r.AppliedAt
|
||||
}
|
||||
}
|
||||
states := make([]SQLMigrationState, 0, len(files))
|
||||
for _, f := range files {
|
||||
st := SQLMigrationState{Filename: f}
|
||||
if at, ok := applied[f]; ok {
|
||||
at := at
|
||||
st.AppliedAt = &at
|
||||
}
|
||||
states = append(states, st)
|
||||
}
|
||||
return states, nil
|
||||
}
|
||||
|
||||
// BaselineSQLMigrations records every migration up to and including upto as applied
|
||||
// WITHOUT executing it — for a server whose schema was brought up to date by other
|
||||
// means before qt_schema_migrations was used. Returns the filenames newly recorded.
|
||||
func BaselineSQLMigrations(db *gorm.DB, migrations fs.FS, upto string) ([]string, error) {
|
||||
files, err := listSQLMigrationFiles(migrations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
found := false
|
||||
for _, f := range files {
|
||||
if f == upto {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return nil, fmt.Errorf("baseline migration %q not found", upto)
|
||||
}
|
||||
if err := ensureSQLMigrationsTable(db); err != nil {
|
||||
return nil, fmt.Errorf("migrate qt_schema_migrations table: %w", err)
|
||||
}
|
||||
var recorded []string
|
||||
for _, f := range files {
|
||||
if f > upto {
|
||||
break
|
||||
}
|
||||
var count int64
|
||||
if err := db.Model(&SQLSchemaMigration{}).Where("filename = ?", f).Count(&count).Error; err != nil {
|
||||
return recorded, fmt.Errorf("check migration %s: %w", f, err)
|
||||
}
|
||||
if count > 0 {
|
||||
continue
|
||||
}
|
||||
if err := db.Create(&SQLSchemaMigration{Filename: f}).Error; err != nil {
|
||||
return recorded, fmt.Errorf("record baseline migration %s: %w", f, err)
|
||||
}
|
||||
recorded = append(recorded, f)
|
||||
}
|
||||
return recorded, nil
|
||||
}
|
||||
|
||||
// NeedsSQLMigrations reports whether at least one SQL migration from migrations
|
||||
// is not yet recorded in qt_schema_migrations.
|
||||
func NeedsSQLMigrations(db *gorm.DB, migrationsDir string) (bool, error) {
|
||||
files, err := listSQLMigrationFiles(migrationsDir)
|
||||
func NeedsSQLMigrations(db *gorm.DB, migrations fs.FS) (bool, error) {
|
||||
files, err := listSQLMigrationFiles(migrations)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -48,50 +124,51 @@ func NeedsSQLMigrations(db *gorm.DB, migrationsDir string) (bool, error) {
|
||||
return count < int64(len(files)), nil
|
||||
}
|
||||
|
||||
// RunSQLMigrations applies SQL files from migrationsDir once and records them in qt_schema_migrations.
|
||||
// Local SQLite-only scripts are skipped automatically.
|
||||
func RunSQLMigrations(db *gorm.DB, migrationsDir string) error {
|
||||
// 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 fmt.Errorf("migrate qt_schema_migrations table: %w", err)
|
||||
return nil, fmt.Errorf("migrate qt_schema_migrations table: %w", err)
|
||||
}
|
||||
|
||||
files, err := listSQLMigrationFiles(migrationsDir)
|
||||
files, err := listSQLMigrationFiles(migrations)
|
||||
if err != nil {
|
||||
return err
|
||||
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 fmt.Errorf("check migration %s: %w", filename, err)
|
||||
return applied, fmt.Errorf("check migration %s: %w", filename, err)
|
||||
}
|
||||
if count > 0 {
|
||||
continue
|
||||
}
|
||||
if onApply != nil {
|
||||
onApply(filename)
|
||||
}
|
||||
|
||||
path := filepath.Join(migrationsDir, filename)
|
||||
content, err := os.ReadFile(path)
|
||||
content, err := fs.ReadFile(migrations, filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read migration %s: %w", filename, err)
|
||||
return applied, fmt.Errorf("read migration %s: %w", filename, err)
|
||||
}
|
||||
|
||||
statements := splitSQLStatements(string(content))
|
||||
if len(statements) == 0 {
|
||||
if err := db.Create(&SQLSchemaMigration{Filename: filename}).Error; err != nil {
|
||||
return fmt.Errorf("record empty migration %s: %w", filename, err)
|
||||
if statements := splitSQLStatements(string(content)); len(statements) > 0 {
|
||||
if err := executeMigrationStatements(db, filename, statements); err != nil {
|
||||
return applied, err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := executeMigrationStatements(db, filename, statements); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Create(&SQLSchemaMigration{Filename: filename}).Error; err != nil {
|
||||
return fmt.Errorf("record migration %s: %w", filename, err)
|
||||
return applied, fmt.Errorf("record migration %s: %w", filename, err)
|
||||
}
|
||||
applied = append(applied, filename)
|
||||
}
|
||||
|
||||
return nil
|
||||
return applied, nil
|
||||
}
|
||||
|
||||
// IsMigrationPermissionError returns true if err indicates insufficient privileges
|
||||
@@ -199,13 +276,13 @@ func splitSQLStatements(script string) []string {
|
||||
return stmts
|
||||
}
|
||||
|
||||
func listSQLMigrationFiles(migrationsDir string) ([]string, error) {
|
||||
entries, err := os.ReadDir(migrationsDir)
|
||||
func listSQLMigrationFiles(migrations fs.FS) ([]string, error) {
|
||||
entries, err := fs.ReadDir(migrations, ".")
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("read migrations dir %s: %w", migrationsDir, err)
|
||||
return nil, fmt.Errorf("read migrations dir: %w", err)
|
||||
}
|
||||
|
||||
files := make([]string, 0, len(entries))
|
||||
|
||||
Reference in new issue
Block a user