Files
QuoteForge/internal/models/sql_migration_verify.go
T
Mikhail ChusavitinandClaude Opus 5.5 44a09d4420 fix(sync): stop MariaDB connection leak and cut per-cycle sync waste
- ConnectionManager reuses a stale-but-healthy pool instead of dialing a new
  one every 30s, and closes every pool it replaces (one client held ~30
  connections). Pool capped at 2 connections, idle released after 1 min.
- Background sync every 1 min with ±30% jitter, scheduled from cycle start;
  users-status online threshold = interval × 1.3. One heartbeat per cycle.
- Partnumber books: read headers only, fetch partnumbers_json per book when
  new/incomplete; empty book counts as complete; an incomplete book is
  re-pulled once per process (server book lists BOT_AGIBOT_G2_PERF missing
  from qt_partnumber_book_items).
- NeedSync in one UNION ALL query; SyncQtSettings skips unchanged rewrite.
- Migration 035: server-maintained server_updated_at on qt_configurations and
  qt_projects. Clients pull only changed rows after one full pull per process;
  servers without 035 and old clients keep full pulls.
- Migrator: fresh GORM statement per verify query and history insert; files
  with two verify checks failed to record.

ADL: bible-local/decisions/2026-10-26-mariadb-single-pool.md,
bible-local/decisions/2026-10-26-incremental-config-project-pull.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 20:46:59 +03:00

69 lines
2.2 KiB
Go

package models
import (
"fmt"
"strings"
"gorm.io/gorm"
)
type migrationVerification struct {
description string
query string
}
func parseMigrationVerifications(script string) ([]migrationVerification, error) {
var checks []migrationVerification
for _, line := range strings.Split(script, "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "-- verify:") {
continue
}
description, query, ok := strings.Cut(strings.TrimPrefix(line, "-- verify:"), "|")
description, query = strings.TrimSpace(description), strings.TrimSpace(query)
if !ok || description == "" || query == "" {
return nil, fmt.Errorf("invalid migration verify header")
}
checks = append(checks, migrationVerification{description: description, query: query})
}
return checks, nil
}
func applySQLMigrationFile(db *gorm.DB, filename, content string) error {
checks, err := parseMigrationVerifications(content)
if err != nil {
return fmt.Errorf("parse migration %s: %w", filename, err)
}
return db.Connection(func(conn *gorm.DB) error {
if err := executeMigrationStatements(conn, filename, splitSQLStatements(content)); err != nil {
return err
}
// Each query gets a fresh statement on the pinned connection: chaining
// Raw/Create on conn itself leaks the previous query's state (with two
// verify checks the history insert targeted a table named "rows").
fresh := func() *gorm.DB { return conn.Session(&gorm.Session{NewDB: true}) }
for _, check := range checks {
rows, err := fresh().Raw(check.query).Rows()
if err != nil {
return fmt.Errorf("verify migration %s (%s): %w", filename, check.description, err)
}
failed := rows.Next()
rowErr := rows.Err()
closeErr := rows.Close()
if rowErr != nil {
return fmt.Errorf("read migration verification %s (%s): %w", filename, check.description, rowErr)
}
if closeErr != nil {
return fmt.Errorf("close migration verification %s (%s): %w", filename, check.description, closeErr)
}
if failed {
return fmt.Errorf("verify migration %s failed: %s", filename, check.description)
}
}
if err := fresh().Create(&SQLSchemaMigration{Filename: filename}).Error; err != nil {
return fmt.Errorf("record migration %s: %w", filename, err)
}
return nil
})
}