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

+22 -2
View File
@@ -441,6 +441,25 @@ MariaDB:
`UPTO` as applied **without running them**, for a server whose schema is already current; `UPTO` as applied **without running them**, for a server whose schema is already current;
- migrations use their own connection without read/write timeouts (`openMigrationDB`), - migrations use their own connection without read/write timeouts (`openMigrationDB`),
not the app's 3s-timeout sync connection — a long backfill must not be cut mid-file; not the app's 3s-timeout sync connection — a long backfill must not be cut mid-file;
- server migration commands finish before runtime connection workers start;
- `-migrate` requires a DBA maintenance window with every other writer stopped,
directly granted global ALL PRIVILEGES, and no other connection using the schema.
Do not expand runtime-user privileges; use the existing developer DBA workflow;
- the runner holds a schema advisory lock and one connection for the entire session.
It automatically writes a full consistent snapshot before changes and per-table
snapshots before every file; each archive is restored into a temporary schema
and compared before any corresponding original-schema change. Snapshot or
verification failure stops the run. See [06-backup.md](06-backup.md);
- on a statement/verify failure, per-step snapshots are restored in reverse order,
including migration history; a full-session snapshot is the fallback. A persistent
marker refuses retry after interruption until `qfs -migrate-restore <archive.zip>`
restores and verifies the original state. Connection/recovery failure retains the
marker and archives and emits an explicit restore diagnostic;
- recovery supports InnoDB base tables and their triggers. Views, routines,
events and cross-schema FKs block migrations before changes; new migrations must
stay within supported table SQL. Existing triggers are captured with their
definer, body, order and SQL/charset context, then restored after all data; Verification requires CREATE/DROP DATABASE.
The runner refuses NO_BACKSLASH_ESCAPES sessions;
- each file, its `-- verify: <description> | <SQL>` queries and history insertion run - each file, its `-- verify: <description> | <SQL>` queries and history insertion run
on one database session, preserving session variables; on one database session, preserving session variables;
- every verify query and the history insert use a fresh GORM statement - every verify query and the history insert use a fresh GORM statement
@@ -452,8 +471,9 @@ MariaDB:
- only duplicate column/key names and already-existing objects are tolerated. - only duplicate column/key names and already-existing objects are tolerated.
FK creation errors (including errno 150/121) remain fatal; FK creation errors (including errno 150/121) remain fatal;
- each file must be safe to re-run after a partial failure (`ADD COLUMN IF NOT EXISTS`, - each file must be safe to re-run after a partial failure (`ADD COLUMN IF NOT EXISTS`,
idempotent `UPDATE ... WHERE <not yet done>`). The current statement splitter assumes idempotent `UPDATE ... WHERE <not yet done>`). The statement parser preserves
semicolons occur only between statements; do not add semicolons inside literals; semicolons in literals/quoted identifiers and strips ordinary comments. Malformed
quotes, executable comments and DELIMITER directives are rejected before execution;
- there is no GORM `AutoMigrate` against MariaDB: it retypes production columns to match the - there is no GORM `AutoMigrate` against MariaDB: it retypes production columns to match the
running binary's models. On 2026-09-24 a stale v1.18 binary run with the old AutoMigrate running binary's models. On 2026-09-24 a stale v1.18 binary run with the old AutoMigrate
`-migrate` did exactly that, which is why it was removed. `-migrate` did exactly that, which is why it was removed.
+1
View File
@@ -65,6 +65,7 @@ They are stored in SQLite and encrypted with `local_encryption.key` unless `QUOT
| `-reset-localdb` | reset after mandatory verified backup; refuses unsynced work | | `-reset-localdb` | reset after mandatory verified backup; refuses unsynced work |
| `-force-reset-localdb` | with `-reset-localdb`, allow clearing unsynced work; backup remains mandatory | | `-force-reset-localdb` | with `-reset-localdb`, allow clearing unsynced work; backup remains mandatory |
| `-migrate` | apply pending server SQL migrations (`migrations/*.sql`) and exit | | `-migrate` | apply pending server SQL migrations (`migrations/*.sql`) and exit |
| `-migrate-restore <archive.zip>` | restore and verify a server migration snapshot during DBA maintenance |
| `-migrate-status` | list server SQL migrations with applied/pending state and exit | | `-migrate-status` | list server SQL migrations with applied/pending state and exit |
| `-migrate-baseline <file>` | record server migrations up to `<file>` as applied without running them, and exit | | `-migrate-baseline <file>` | record server migrations up to `<file>` as applied without running them, and exit |
| `-version` | print app version and exit | | `-version` | print app version and exit |
+64
View File
@@ -71,3 +71,67 @@ in one transaction and preserve connection settings. Missing legacy tables are
skipped. Use the restore procedure above for the pre-reset archive; retain it skipped. Use the restore procedure above for the pre-reset archive; retain it
until the reset has been verified. The environment reset switch was removed to until the reset has been verified. The environment reset switch was removed to
prevent repeated destructive startup. prevent repeated destructive startup.
## Server migration snapshots
`qfs -migrate` writes mandatory archives under
`<backup root>/server-migrations/qfs-mariadb-v1-<label>-<UTC timestamp>-<unique>.zip`.
`QFS_BACKUP_DIR` overrides the root; `QFS_BACKUP_DISABLE` never disables safety
snapshots. Paths are checked outside git, including symlink targets, before
creation and restore. Archives have private filesystem permissions and contain
server table schemas and data, which may be sensitive; never put them in git.
Connection passwords and the local encryption key are not copied into metadata.
One full snapshot precedes the session. Before each file, a separate snapshot
contains a schema definition and row stream for **every** table: this includes
all affected tables even when a legacy Tables-affected header is incomplete.
This intentionally favors recovery over storage/time efficiency for the manual
DBA tool. Retain safety snapshots outside normal rotation until the migration
has been verified; remove verified-session artifacts as operational cleanup.
The versioned ZIP manifest records table schemas, writable column order, row
counts and order-independent row digests. Row streams preserve NULL, empty
strings, binary values and fractional timestamps. Restore temporarily uses
NO_AUTO_VALUE_ON_ZERO to preserve explicit zero IDs and legacy zero dates;
row digests detect coercion and the original session mode is restored. Generated columns are rebuilt
from schema, not inserted. Trigger bodies, definers, firing order and creation
SQL/charset context are preserved. Triggers are recreated after row loading so
restore does not duplicate audit data or alter restored values. CRC/format/digests are checked, then each archive is
restored into a unique temporary schema on the same server and compared for
schema and data equality. The temporary schema is dropped afterward. A failed
or interrupted check must be investigated before retry; abandoned `qfs_verify_`
schemas are DBA cleanup artifacts, never application data.
Stop every other writer for the entire migration/recovery window, including
clients that could reconnect. The runner detects other current schema connections
and uses an advisory lock to exclude a second runner; the lock does not stop
unrelated SQL clients. An active client appearing during failure recovery causes
recovery refusal rather than overwriting its work. InnoDB base tables and triggers are supported; views, routines, events and
cross-schema FKs require explicit recovery support before migration. Triggers
whose recorded database collation differs from the current database collation
also cause refusal until that legacy state has supported recovery. Trigger
routine calls and qualified references to other schemas are refused; source
SQL must have no effects outside the captured schema.
Unsupported schemas stop before changes. Runtime database-user grants stay as
listed in [03-database.md](03-database.md); the tool requires an existing DBA account.
Failure restores step snapshots in reverse order; the full-session archive is
used if step recovery is insufficient. Schema, writable data and migration
history must match the session start before recovery is reported successful.
The persistent `.pending` marker is keyed by configured endpoint and schema, so
server restarts do not hide an interrupted session. Backups survive connection
loss; a failed recovery retains its marker and reports the full snapshot path.
After an interruption, keep clients stopped and use the snapshot path reported
by the runner:
```bash
go run ./cmd/qfs -migrate-restore /outside/repository/server-migrations/qfs-mariadb-v1-session-<timestamp>-<unique>.zip
make migrate-status
make migrate
```
Restore uses the currently saved DBA connection, requires the same database name,
checks the archive by restoring it into a temporary schema first, restores the
original schema, verifies equality and clears the matching pending marker. It
does not require mysql/mysqldump or any operator-installed database utility.
+4
View File
@@ -12,6 +12,10 @@ go vet ./...
make security-check # pinned govulncheck, blocks reachable vulnerabilities make security-check # pinned govulncheck, blocks reachable vulnerabilities
# Optional live transport checks; owns disposable Docker containers: # Optional live transport checks; owns disposable Docker containers:
go test -tags integration ./internal/mariadb -run TestMariaDBTLSIntegration -count=1 go test -tags integration ./internal/mariadb -run TestMariaDBTLSIntegration -count=1
# Snapshot/DDL recovery checks; owns a disposable MariaDB 11.8 container:
go test -tags integration ./internal/models -run TestSQLMigrationSafetyMariaDB -count=1
# Production-dump rehearsal; source path via QFS_TEST_PRODUCTION_DUMP:
go test -tags integration ./internal/models -run TestProductionDumpMigrationMariaDB -count=1 -v
make build-release make build-release
make install-hooks make install-hooks
``` ```
@@ -0,0 +1,62 @@
# Decision: verified snapshots and whole-session recovery for server migrations
**Date:** 2026-10-26
**Status:** active
## Context
Issue #3 fixes FK error propagation, session variables and verify execution.
Its proposed manual-backup exception conflicts with the shared go-database
contract. MariaDB DDL cannot be made rollback-safe by wrapping the migration
files in an ordinary transaction: see [MariaDB implicit commits](https://mariadb.com/docs/server/reference/sql-statements/transactions/sql-statements-that-cause-an-implicit-commit).
## Decision
The migration engine, rather than the operator, owns full-session and pre-file
per-table backups. A Go ZIP/JSON snapshot format avoids external database-client
dependencies. Every archive is restored into a temporary schema and compared
before the corresponding original-schema changes. Per-file archives include all
tables to cover incomplete legacy affected-table headers and dynamic SQL.
On failure, restore each step snapshot in reverse order; use the full-session
snapshot as fallback. Include migration history in recovery. Persist a recovery
marker before applying any changes, refuse an interrupted session on retry and
ship an explicit `-migrate-restore` path. Restore failures retain archives and
produce diagnostics instead of claiming success.
The production dump contains 27 triggers, so the implementation preserves
InnoDB tables and trigger bodies, definers, order and SQL/charset context.
Triggers are recreated after row loading to avoid firing them during restore;
verification binds them to the temporary schema and performs no later DML.
Views, routines, events, cross-schema FKs and legacy trigger/database collation
mismatches are refused before changes. It requires an existing
DBA account with directly granted global ALL PRIVILEGES so object/client
visibility cannot silently hide unsupported state, plus a maintenance window
without other writers. Do not grant these privileges to the runtime user. The
advisory migration lock excludes other runners, not arbitrary SQL clients.
The parser supports SQL literals and quoted identifiers containing semicolons,
escaped/doubled quotes and ordinary comments. It rejects malformed input,
executable comments and DELIMITER rather than silently misinterpreting them.
Existing merged SQL files are unchanged.
## Consequences
Safety snapshots and restore verification add disk usage, temporary server schema
creation and runtime to the developer-only migration command. Normal local-first
CRUD and sync gain no MariaDB dependency. Migration commands start no background
connection worker. Before production use, rehearse on a current production dump
in MariaDB matching production version and collation; synthetic tests alone do
not satisfy that gate. Unsupported schema features need explicit recovery support
before such schemas may be migrated by this tool.
## Acceptance
Rehearsed on the supplied 2026-10-26 production dump (45 tables, 27 triggers),
using an isolated MariaDB 11.8.3 container and utf8mb4_uca1400_ai_ci. The test
verifies full restore, actual migration 035 on the reconstructed pre-035 clone,
reverse recovery after a later DDL/verify failure, successful retry and exact
restoration of the original dump state. Synthetic tests additionally exercise
FK errno 150, interrupted-session recovery, NULL/empty/binary/decimal/timestamp
values, zero auto-increment IDs and legacy zero dates. No production changes
are performed by these checks.
+45 -18
View File
@@ -2,10 +2,10 @@
Repository: https://git.mchus.pro/mchus/QuoteForge Repository: https://git.mchus.pro/mchus/QuoteForge
Six issues (#2-#7) remain open. Their initial fixes were merged into `main` Scope: issues #2-#7. Initial fixes were merged into `main` in
in [PR #8](https://git.mchus.pro/mchus/QuoteForge/pulls/8), with release notes [PR #8](https://git.mchus.pro/mchus/QuoteForge/pulls/8), with release notes in v3.50.
in v3.50. Acceptance and remaining rule compliance work continue sequentially The follow-up adds completed acceptance checks and server migration recovery
on `fix/issue-acceptance-and-migration-safety`. required by the shared rules. Each issue has its own implementation/acceptance commit.
| Order | Issue | Implementation and acceptance | Status | | Order | Issue | Implementation and acceptance | Status |
| --- | --- | --- | --- | | --- | --- | --- | --- |
@@ -13,7 +13,7 @@ on `fix/issue-acceptance-and-migration-safety`.
| 2 | [#4](https://git.mchus.pro/mchus/QuoteForge/issues/4) | Move LOT-bearing cart/rental events to delegated listeners and escaped data attributes; escape BOM and sync output; share HTML escaping; render toast as text. Malicious LOT regression checks and headless Chrome DOM checks pass. | Acceptance passed | | 2 | [#4](https://git.mchus.pro/mchus/QuoteForge/issues/4) | Move LOT-bearing cart/rental events to delegated listeners and escaped data attributes; escape BOM and sync output; share HTML escaping; render toast as text. Malicious LOT regression checks and headless Chrome DOM checks pass. | Acceptance passed |
| 3 | [#7](https://git.mchus.pro/mchus/QuoteForge/issues/7) | Require a fresh verified archive outside rotation before reset, refuse unsynced/legacy states without explicit force, use transactional deletion, remove environment-triggered reset. Verified refusal, ZIP restore, backup failure, rollback and CLI startup after restore. | Acceptance passed | | 3 | [#7](https://git.mchus.pro/mchus/QuoteForge/issues/7) | Require a fresh verified archive outside rotation before reset, refuse unsynced/legacy states without explicit force, use transactional deletion, remove environment-triggered reset. Verified refusal, ZIP restore, backup failure, rollback and CLI startup after restore. | Acceptance passed |
| 4 | [#5](https://git.mchus.pro/mchus/QuoteForge/issues/5) | Persist TLS mode/CA in SQLite, expose setup controls, share policy across setup/sync/migration DSNs. Verified certificate trust, hostname checks, fallback policy, settings round-trip and live MariaDB 11.8 transport. | Acceptance passed | | 4 | [#5](https://git.mchus.pro/mchus/QuoteForge/issues/5) | Persist TLS mode/CA in SQLite, expose setup controls, share policy across setup/sync/migration DSNs. Verified certificate trust, hostname checks, fallback policy, settings round-trip and live MariaDB 11.8 transport. | Acceptance passed |
| 5 | [#3](https://git.mchus.pro/mchus/QuoteForge/issues/3) | Keep each migration file in one session, execute verify headers before history insertion, propagate FK failures. Verify failed queries/checks never enter history. | Implemented | | 5 | [#3](https://git.mchus.pro/mchus/QuoteForge/issues/3) | Keep each migration file in one session, execute verify headers before history insertion, propagate FK failures. Failed queries/checks restore the entire session. Verified snapshots, literal-aware SQL parsing, trigger preservation and crash recovery pass synthetic tests and production-dump rehearsal on MariaDB 11.8.3. | Acceptance passed |
| 6 | [#6](https://git.mchus.pro/mchus/QuoteForge/issues/6) | Require Go 1.27.3, update vulnerable dependencies, block release on pinned govulncheck. Tests, vet, security scan, cross-platform builds and release failure checks pass. | Acceptance passed | | 6 | [#6](https://git.mchus.pro/mchus/QuoteForge/issues/6) | Require Go 1.27.3, update vulnerable dependencies, block release on pinned govulncheck. Tests, vet, security scan, cross-platform builds and release failure checks pass. | Acceptance passed |
## Decisions ## Decisions
@@ -28,22 +28,15 @@ on `fix/issue-acceptance-and-migration-safety`.
encryption. The setup form explains this distinction. encryption. The setup form explains this distinction.
- `go 1.27.3` enforces the minimum compiler, including with `GOTOOLCHAIN=local`. - `go 1.27.3` enforces the minimum compiler, including with `GOTOOLCHAIN=local`.
A redundant `toolchain go1.27.3` is removed by Go tooling and is unnecessary. A redundant `toolchain go1.27.3` is removed by Go tooling and is unnecessary.
- The local ignored `.claude/skills/release/SKILL.md` was updated with the security
prerequisite; the shared Makefile and release script enforce it in git.
## Validation and handoff ## Validation and handoff
Passed: `go test ./...`, `go vet ./...`, `node scripts/tests/html-security.cjs`, The original implementation PR is merged. Current acceptance results are listed
`make security-check`, shell syntax and diff checks, and CGO-free builds for below. Govulncheck reports zero reachable vulnerabilities and one module-level
macOS arm64 and Windows amd64. advisory whose affected code is not called. Initial cross-platform builds and
all Go/JavaScript checks pass; final verification also covers the recovery change.
Govulncheck reports zero reachable vulnerabilities and one module-level advisory Production-dump rehearsal also passes on MariaDB 11.8.3 with the production
whose affected code is not called. Gin's transitive quic-go was explicitly raised collation. No production migration or new release is performed by this work.
to v0.59.1 after the first scan identified a reachable advisory in v0.59.0.
The checks use temporary SQLite databases and synthetic certificates. They do
not exercise a live MariaDB server or browser automation. The original implementation PR is merged. Remaining acceptance work and
rule compliance are tracked below; issue state changes are separate.
## Acceptance follow-up ## Acceptance follow-up
@@ -101,3 +94,37 @@ failing Go scanner in an isolated directory and confirms all five release build
targets and `scripts/release.sh` stop before compilation or artifact creation. targets and `scripts/release.sh` stop before compilation or artifact creation.
The scanner reports zero reachable vulnerabilities and one uncalled module-level The scanner reports zero reachable vulnerabilities and one uncalled module-level
advisory. No release is published by these acceptance checks. advisory. No release is published by these acceptance checks.
### Issue #3
The parser, mandatory server snapshots, restore-readiness checks, reverse step
recovery and explicit crash restore are implemented; see
[server migration recovery decision](decisions/2026-10-26-server-migration-recovery.md).
`go test -tags integration ./internal/models -run TestSQLMigrationSafetyMariaDB -count=1`
passes on a disposable MariaDB 11.8: session variables and two verify checks,
semicolon literals, a later failed verify, a real FK errno 150 after successful
DDL, reverse recovery of prior files, interrupted-session refusal/restore/retry,
unsupported-schema refusal, active-client refusal and backup failure.
Restored schema, binary/NULL/empty/decimal/timestamp data and history are compared
to the original snapshot. Unit tests cover snapshot format/digests, symlink path
refusal and all embedded migration SQL syntax.
The supplied `2026-10-26_RFQ_LOG.sql` dump (45 tables, 27 triggers) is loaded
read-only into a new disposable MariaDB **11.8.3** container with
`utf8mb4_uca1400_ai_ci`, matching the production version and collation.
`TestProductionDumpMigrationMariaDB` verifies a full snapshot restore, rebuilds
the pre-035 schema only in that clone, applies the real embedded migration 035,
forces a subsequent DDL/verify failure, verifies reverse recovery against the
complete pre-session snapshot, then applies 035 successfully and restores the
exact original dump state. Trigger bodies, definers and order are preserved;
triggers never fire during row loading. No production schema or dump file is changed.
Run this optional, destructive-to-its-own-container rehearsal with an existing
`mariadb:11.8.3` image and `QFS_TEST_PRODUCTION_DUMP=/outside/repository/dump.sql`:
```bash
go test -tags integration ./internal/models -run TestProductionDumpMigrationMariaDB -count=1 -v
```
The environment variable supplies the source file only; no externally supplied
DSN is accepted. The test owns and removes its container and temporary archives.
+16 -9
View File
@@ -62,6 +62,7 @@ func main() {
resetLocalDB := flag.Bool("reset-localdb", false, "reset local SQLite data after a verified safety backup (keeps connection settings)") resetLocalDB := flag.Bool("reset-localdb", false, "reset local SQLite data after a verified safety backup (keeps connection settings)")
forceResetLocalDB := flag.Bool("force-reset-localdb", false, "allow -reset-localdb to clear unsynced work after a verified backup") forceResetLocalDB := flag.Bool("force-reset-localdb", false, "allow -reset-localdb to clear unsynced work after a verified backup")
migrate := flag.Bool("migrate", false, "apply pending server (MariaDB) SQL migrations and exit") migrate := flag.Bool("migrate", false, "apply pending server (MariaDB) SQL migrations and exit")
migrateRestore := flag.String("migrate-restore", "", "restore a verified server migration snapshot during maintenance and exit")
migrateStatus := flag.Bool("migrate-status", false, "list server SQL migrations with applied/pending state and exit") migrateStatus := flag.Bool("migrate-status", false, "list server SQL migrations with applied/pending state and exit")
migrateBaseline := flag.String("migrate-baseline", "", "record server SQL migrations up to and including this file as applied, without running them, and exit") migrateBaseline := flag.String("migrate-baseline", "", "record server SQL migrations up to and including this file as applied, without running them, and exit")
version := flag.Bool("version", false, "show version information") version := flag.Bool("version", false, "show version information")
@@ -132,6 +133,10 @@ func main() {
// Check if running in setup mode (no connection settings) // Check if running in setup mode (no connection settings)
if !local.HasSettings() { if !local.HasSettings() {
if *migrate || *migrateStatus || *migrateBaseline != "" || *migrateRestore != "" {
slog.Error("server migration command requires saved DBA connection settings")
os.Exit(1)
}
slog.Info("no database settings found, starting setup mode") slog.Info("no database settings found, starting setup mode")
runSetupMode(local) runSetupMode(local)
return return
@@ -174,13 +179,6 @@ func main() {
// Create connection manager. Runtime stays local-first; MariaDB is used on demand by sync/setup only. // Create connection manager. Runtime stays local-first; MariaDB is used on demand by sync/setup only.
connMgr := db.NewConnectionManager(local) connMgr := db.NewConnectionManager(local)
// Keep the online-status cache fresh in the background so request-handling
// goroutines (health checks, middleware, etc.) never block on the MySQL
// dial/read timeout themselves.
connMgrCtx, connMgrCancel := context.WithCancel(context.Background())
defer connMgrCancel()
connMgr.Start(connMgrCtx)
dbUser := local.GetDBUser() dbUser := local.GetDBUser()
slog.Info("starting QuoteForge server", slog.Info("starting QuoteForge server",
@@ -191,19 +189,21 @@ func main() {
"online", false, "online", false,
) )
if *migrate || *migrateStatus || *migrateBaseline != "" { if *migrate || *migrateStatus || *migrateBaseline != "" || *migrateRestore != "" {
mariaDB, err := openMigrationDB(local) mariaDB, err := openMigrationDB(local)
if err != nil { if err != nil {
slog.Error("cannot run migrations: database not available", "error", err) slog.Error("cannot run migrations: database not available", "error", err)
os.Exit(1) os.Exit(1)
} }
switch { switch {
case *migrateRestore != "":
err = models.RestoreSQLMigrationSession(mariaDB, *migrateRestore)
case *migrateStatus: case *migrateStatus:
err = runMigrateStatus(mariaDB) err = runMigrateStatus(mariaDB)
case *migrateBaseline != "": case *migrateBaseline != "":
err = runMigrateBaseline(mariaDB, *migrateBaseline) err = runMigrateBaseline(mariaDB, *migrateBaseline)
default: default:
err = runMigrateApply(mariaDB) err = runMigrateApply(mariaDB, resolvedLocalDBPath)
} }
if err != nil { if err != nil {
slog.Error("migration command failed", "error", err) slog.Error("migration command failed", "error", err)
@@ -212,6 +212,13 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// Keep the online-status cache fresh in the background so request-handling
// goroutines (health checks, middleware, etc.) never block on the MySQL
// dial/read timeout themselves.
connMgrCtx, connMgrCancel := context.WithCancel(context.Background())
defer connMgrCancel()
connMgr.Start(connMgrCtx)
gin.SetMode(cfg.Server.Mode) gin.SetMode(cfg.Server.Mode)
restartSig := make(chan struct{}, 1) restartSig := make(chan struct{}, 1)
+13 -2
View File
@@ -6,6 +6,7 @@ import (
"time" "time"
qfassets "git.mchus.pro/mchus/quoteforge" qfassets "git.mchus.pro/mchus/quoteforge"
"git.mchus.pro/mchus/quoteforge/internal/appstate"
"git.mchus.pro/mchus/quoteforge/internal/localdb" "git.mchus.pro/mchus/quoteforge/internal/localdb"
"git.mchus.pro/mchus/quoteforge/internal/models" "git.mchus.pro/mchus/quoteforge/internal/models"
mysqlDriver "github.com/go-sql-driver/mysql" mysqlDriver "github.com/go-sql-driver/mysql"
@@ -39,6 +40,12 @@ func openMigrationDB(local *localdb.LocalDB) (*gorm.DB, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("connect to server: %w", err) return nil, fmt.Errorf("connect to server: %w", err)
} }
sqlDB, err := db.DB()
if err != nil {
return nil, fmt.Errorf("migration pool: %w", err)
}
sqlDB.SetMaxOpenConns(1)
sqlDB.SetMaxIdleConns(1)
return db, nil return db, nil
} }
@@ -68,7 +75,7 @@ func runMigrateStatus(db *gorm.DB) error {
// runMigrateApply applies pending server migrations. Refuses on a server with no // runMigrateApply applies pending server migrations. Refuses on a server with no
// migration history at all: its schema was built by other means, so every file would be // migration history at all: its schema was built by other means, so every file would be
// re-run — record a baseline with -migrate-baseline first. // re-run — record a baseline with -migrate-baseline first.
func runMigrateApply(db *gorm.DB) error { func runMigrateApply(db *gorm.DB, localDBPath string) error {
migrations, err := qfassets.MigrationsFS() migrations, err := qfassets.MigrationsFS()
if err != nil { if err != nil {
return err return err
@@ -76,9 +83,13 @@ func runMigrateApply(db *gorm.DB) error {
if err := requireMigrationHistory(db, migrations); err != nil { if err := requireMigrationHistory(db, migrations); err != nil {
return err return err
} }
backupRoot, err := appstate.ServerMigrationBackupDir(localDBPath)
if err != nil {
return err
}
applied, err := models.RunSQLMigrations(db, migrations, func(filename string) { applied, err := models.RunSQLMigrations(db, migrations, func(filename string) {
fmt.Printf(" applying %s ...\n", filename) fmt.Printf(" applying %s ...\n", filename)
}) }, backupRoot)
if err != nil { if err != nil {
return err return err
} }
+51
View File
@@ -0,0 +1,51 @@
package appstate
import (
"fmt"
"os"
"path/filepath"
)
// ServerMigrationBackupDir is separate from local backup retention.
func ServerMigrationBackupDir(dbPath string) (string, error) {
root := filepath.Join(resolveBackupRoot(dbPath), "server-migrations")
if err := ValidateBackupLocation(root); err != nil {
return "", err
}
if err := os.MkdirAll(root, 0700); err != nil {
return "", fmt.Errorf("create server backup directory: %w", err)
}
resolved, err := filepath.EvalSymlinks(root)
if err != nil {
return "", fmt.Errorf("resolve server backup directory: %w", err)
}
if err := ValidateBackupLocation(resolved); err != nil {
return "", err
}
return resolved, nil
}
// ValidateBackupLocation checks the destination, including existing symlinks.
func ValidateBackupLocation(path string) error {
if err := validateBackupRoot(path); err != nil {
return err
}
existing := path
for {
if _, err := os.Lstat(existing); err == nil {
break
} else if !os.IsNotExist(err) {
return fmt.Errorf("inspect backup path: %w", err)
}
parent := filepath.Dir(existing)
if parent == existing {
return fmt.Errorf("backup path has no existing parent")
}
existing = parent
}
resolved, err := filepath.EvalSymlinks(existing)
if err != nil {
return fmt.Errorf("resolve backup path: %w", err)
}
return validateBackupRoot(resolved)
}
@@ -0,0 +1,29 @@
package appstate
import (
"os"
"path/filepath"
"testing"
)
func TestServerBackupRejectsGitAndSymlinks(t *testing.T) {
root := t.TempDir()
repo := filepath.Join(root, "repo")
if err := os.MkdirAll(filepath.Join(repo, ".git"), 0700); err != nil {
t.Fatal(err)
}
link := filepath.Join(root, "link")
if err := os.Symlink(repo, link); err != nil {
t.Fatal(err)
}
for _, path := range []string{filepath.Join(repo, "backups"), filepath.Join(link, "backups")} {
t.Setenv("QFS_BACKUP_DIR", path)
if _, err := ServerMigrationBackupDir(filepath.Join(root, "state", "qfs.db")); err == nil {
t.Fatalf("accepted repository backup: %s", path)
}
}
t.Setenv("QFS_BACKUP_DIR", filepath.Join(root, "backups"))
if _, err := ServerMigrationBackupDir(filepath.Join(root, "state", "qfs.db")); err != nil {
t.Fatal(err)
}
}
+185
View File
@@ -0,0 +1,185 @@
package mariadb
import (
"archive/zip"
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"git.mchus.pro/mchus/quoteforge/internal/appstate"
"gorm.io/gorm"
)
type snapshotTable struct {
Name, Create, Entry, Digest string
Columns []string
Rows int64
}
type snapshotManifest struct {
Format int
Schema, Collation string
Tables []snapshotTable
Triggers []snapshotTrigger
}
func freshDB(db *gorm.DB) *gorm.DB { return db.Session(&gorm.Session{NewDB: true}) }
func quoteIdentifier(s string) string { return "`" + strings.ReplaceAll(s, "`", "``") + "`" }
// CreateSnapshot writes a consistent InnoDB snapshot, without external DB tools.
// The caller supplies a dedicated connection and excludes concurrent writers
// for the entire migration session, including any recovery.
func CreateSnapshot(db *gorm.DB, root, label string) (string, error) {
if err := appstate.ValidateBackupLocation(root); err != nil {
return "", err
}
if filepath.Base(label) != label {
return "", fmt.Errorf("invalid snapshot label")
}
if err := os.MkdirAll(root, 0700); err != nil {
return "", fmt.Errorf("create snapshot directory: %w", err)
}
if err := appstate.ValidateBackupLocation(root); err != nil {
return "", err
}
file, err := os.CreateTemp(root, "qfs-mariadb-v1-"+label+"-"+time.Now().UTC().Format("20060102T150405Z")+"-*.zip")
if err != nil {
return "", fmt.Errorf("create snapshot: %w", err)
}
path := file.Name()
archive := zip.NewWriter(file)
err = freshDB(db).Transaction(func(tx *gorm.DB) error {
manifest, err := snapshotInventory(tx)
if err != nil {
return err
}
for i := range manifest.Tables {
table := &manifest.Tables[i]
table.Entry = fmt.Sprintf("tables/%06d.jsonl", i)
out, err := archive.Create(table.Entry)
if err != nil {
return fmt.Errorf("create table entry %s: %w", table.Name, err)
}
if err := snapshotRows(tx, table, out); err != nil {
return err
}
}
out, err := archive.Create("manifest.json")
if err != nil {
return fmt.Errorf("create snapshot manifest: %w", err)
}
if err := json.NewEncoder(out).Encode(manifest); err != nil {
return fmt.Errorf("write snapshot manifest: %w", err)
}
return nil
}, &sql.TxOptions{Isolation: sql.LevelRepeatableRead, ReadOnly: true})
zipErr := archive.Close()
syncErr := file.Sync()
closeErr := file.Close()
for _, candidate := range []error{err, zipErr, syncErr, closeErr} {
if candidate != nil {
return "", fmt.Errorf("write snapshot %s: %w", path, candidate)
}
}
if _, err := readSnapshot(path, nil); err != nil {
return "", err
}
return path, nil
}
func snapshotInventory(db *gorm.DB) (*snapshotManifest, error) {
m := &snapshotManifest{Format: 1}
if err := freshDB(db).Raw("SELECT DATABASE(), @@collation_database").Row().Scan(&m.Schema, &m.Collation); err != nil {
return nil, fmt.Errorf("read snapshot schema: %w", err)
}
var unsupported int64
query := `SELECT
(SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=? AND (TABLE_TYPE<>'BASE TABLE' OR ENGINE<>'InnoDB')) +
(SELECT COUNT(*) FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA=?) +
(SELECT COUNT(*) FROM information_schema.EVENTS WHERE EVENT_SCHEMA=?) +
(SELECT COUNT(*) FROM information_schema.KEY_COLUMN_USAGE WHERE
(TABLE_SCHEMA=? AND REFERENCED_TABLE_SCHEMA<>?) OR (REFERENCED_TABLE_SCHEMA=? AND TABLE_SCHEMA<>?))`
if err := freshDB(db).Raw(query, m.Schema, m.Schema, m.Schema, m.Schema, m.Schema, m.Schema, m.Schema).Scan(&unsupported).Error; err != nil {
return nil, fmt.Errorf("check snapshot support: %w", err)
}
if unsupported != 0 {
return nil, fmt.Errorf("snapshot refused: views, non-InnoDB tables, routines, events or cross-schema foreign keys require supported recovery before migration")
}
var names []string
if err := freshDB(db).Raw("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA=? ORDER BY TABLE_NAME", m.Schema).Scan(&names).Error; err != nil {
return nil, fmt.Errorf("list snapshot tables: %w", err)
}
for _, name := range names {
table := snapshotTable{Name: name}
var ignored string
if err := freshDB(db).Raw("SHOW CREATE TABLE "+quoteIdentifier(name)).Row().Scan(&ignored, &table.Create); err != nil {
return nil, fmt.Errorf("read schema for %s: %w", name, err)
}
if err := freshDB(db).Raw(`SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND EXTRA NOT LIKE '%VIRTUAL GENERATED%' AND EXTRA NOT LIKE '%STORED GENERATED%' ORDER BY ORDINAL_POSITION`, m.Schema, name).Scan(&table.Columns).Error; err != nil {
return nil, fmt.Errorf("read columns for %s: %w", name, err)
}
if len(table.Columns) == 0 {
return nil, fmt.Errorf("snapshot table %s has no writable columns", name)
}
m.Tables = append(m.Tables, table)
}
if err := readSnapshotTriggers(db, m); err != nil {
return nil, err
}
return m, nil
}
func snapshotRows(db *gorm.DB, table *snapshotTable, out io.Writer) error {
columns := make([]string, len(table.Columns))
for i, col := range table.Columns {
columns[i] = "CAST(" + quoteIdentifier(col) + " AS BINARY)"
}
rows, err := freshDB(db).Raw("SELECT " + strings.Join(columns, ",") + " FROM " + quoteIdentifier(table.Name)).Rows()
if err != nil {
return fmt.Errorf("read snapshot table %s: %w", table.Name, err)
}
defer rows.Close()
var digest [32]byte
encoder := json.NewEncoder(out)
table.Rows = 0
for rows.Next() {
values := make([]sql.RawBytes, len(columns))
args := make([]any, len(columns))
for i := range args {
args[i] = &values[i]
}
if err := rows.Scan(args...); err != nil {
return fmt.Errorf("read row in %s: %w", table.Name, err)
}
if err := encoder.Encode(values); err != nil {
return fmt.Errorf("write row in %s: %w", table.Name, err)
}
addRowDigest(&digest, values)
table.Rows++
}
if err := rows.Err(); err != nil {
return fmt.Errorf("iterate %s: %w", table.Name, err)
}
if err := rows.Close(); err != nil {
return fmt.Errorf("close rows in %s: %w", table.Name, err)
}
table.Digest = hex.EncodeToString(digest[:])
return nil
}
func addRowDigest(sum *[32]byte, row []sql.RawBytes) {
encoded, _ := json.Marshal(row) // Raw bytes and nil always marshal successfully.
hash := sha256.Sum256(encoded)
carry := uint16(0)
for i := len(sum) - 1; i >= 0; i-- {
value := uint16(sum[i]) + uint16(hash[i]) + carry
sum[i], carry = byte(value), value>>8
}
}
+101
View File
@@ -0,0 +1,101 @@
package mariadb
import (
"archive/zip"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"git.mchus.pro/mchus/quoteforge/internal/appstate"
)
func readSnapshot(path string, visit func(snapshotTable, []sql.RawBytes) error) (*snapshotManifest, error) {
if err := appstate.ValidateBackupLocation(path); err != nil {
return nil, err
}
archive, err := zip.OpenReader(path)
if err != nil {
return nil, fmt.Errorf("open server snapshot: %w", err)
}
defer archive.Close()
files := map[string]*zip.File{}
for _, f := range archive.File {
if files[f.Name] != nil {
return nil, fmt.Errorf("duplicate snapshot entry")
}
files[f.Name] = f
}
manifestFile := files["manifest.json"]
if manifestFile == nil || manifestFile.UncompressedSize64 > 16<<20 {
return nil, fmt.Errorf("missing or oversized snapshot manifest")
}
in, err := manifestFile.Open()
if err != nil {
return nil, fmt.Errorf("open server manifest: %w", err)
}
data, readErr := io.ReadAll(in)
closeErr := in.Close()
if readErr != nil || closeErr != nil {
return nil, fmt.Errorf("read server manifest: %w", errors.Join(readErr, closeErr))
}
var m snapshotManifest
if err := json.Unmarshal(data, &m); err != nil {
return nil, fmt.Errorf("parse server manifest: %w", err)
}
if m.Format != 1 || m.Schema == "" || len(files) != len(m.Tables)+1 {
return nil, fmt.Errorf("invalid server snapshot manifest")
}
names, entries := map[string]bool{}, map[string]bool{}
for _, table := range m.Tables {
if table.Name == "" || names[table.Name] || entries[table.Entry] || files[table.Entry] == nil || len(table.Columns) == 0 || table.Rows < 0 || !strings.HasPrefix(table.Create, "CREATE TABLE "+quoteIdentifier(table.Name)+" (") {
return nil, fmt.Errorf("invalid snapshot table metadata")
}
names[table.Name], entries[table.Entry] = true, true
if err := readSnapshotTable(files[table.Entry], table, visit); err != nil {
return nil, err
}
}
return &m, nil
}
func readSnapshotTable(file *zip.File, table snapshotTable, visit func(snapshotTable, []sql.RawBytes) error) error {
in, err := file.Open()
if err != nil {
return fmt.Errorf("open snapshot table %s: %w", table.Name, err)
}
defer in.Close()
decoder := json.NewDecoder(in)
var rows int64
var digest [32]byte
for {
var values []sql.RawBytes
err := decoder.Decode(&values)
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("read snapshot row in %s: %w", table.Name, err)
}
if len(values) != len(table.Columns) {
return fmt.Errorf("snapshot column count mismatch in %s", table.Name)
}
addRowDigest(&digest, values)
rows++
if visit != nil {
if err := visit(table, values); err != nil {
return err
}
}
}
if err := in.Close(); err != nil {
return fmt.Errorf("close snapshot table %s: %w", table.Name, err)
}
if rows != table.Rows || hex.EncodeToString(digest[:]) != table.Digest {
return fmt.Errorf("snapshot checksum mismatch in %s", table.Name)
}
return nil
}
+118
View File
@@ -0,0 +1,118 @@
package mariadb
import (
"database/sql"
"errors"
"fmt"
"strings"
"gorm.io/gorm"
)
// RestoreSnapshot restores and checks a snapshot on a dedicated connection.
// The same maintenance exclusion as migration is required throughout recovery.
func RestoreSnapshot(db *gorm.DB, path string) error {
m, err := readSnapshot(path, nil)
if err != nil {
return err
}
var schema string
if err := freshDB(db).Raw("SELECT DATABASE()").Scan(&schema).Error; err != nil {
return fmt.Errorf("read restore destination: %w", err)
}
if schema != m.Schema {
return fmt.Errorf("snapshot schema does not match restore destination")
}
if err := restoreSnapshotTables(db, path, m); err != nil {
return err
}
return compareSnapshot(db, m)
}
func restoreSnapshotTables(db *gorm.DB, path string, m *snapshotManifest) (err error) {
current, err := snapshotInventory(db)
if err != nil {
return err
}
var foreignKeys int
var sqlMode string
if err := freshDB(db).Raw("SELECT @@foreign_key_checks, @@sql_mode").Row().Scan(&foreignKeys, &sqlMode); err != nil {
return fmt.Errorf("read restore session policy: %w", err)
}
if err := freshDB(db).Exec("SET FOREIGN_KEY_CHECKS=0").Error; err != nil {
return fmt.Errorf("disable FK checks for restore: %w", err)
}
defer func() {
if restoreErr := freshDB(db).Exec("SET sql_mode=?", sqlMode).Error; restoreErr != nil {
err = errors.Join(err, fmt.Errorf("restore SQL mode: %w", restoreErr))
}
if restoreErr := freshDB(db).Exec("SET FOREIGN_KEY_CHECKS=?", foreignKeys).Error; restoreErr != nil {
err = errors.Join(err, fmt.Errorf("restore FK policy: %w", restoreErr))
}
}()
// Preserve explicit zero auto-increment IDs and legacy zero dates. Exact
// schema/data comparison detects any coercion before the source is changed.
if err := freshDB(db).Exec("SET sql_mode='NO_AUTO_VALUE_ON_ZERO'").Error; err != nil {
return fmt.Errorf("set snapshot insert mode: %w", err)
}
for i := len(current.Tables) - 1; i >= 0; i-- {
if err := freshDB(db).Exec("DROP TABLE " + quoteIdentifier(current.Tables[i].Name)).Error; err != nil {
return fmt.Errorf("drop restore table %s: %w", current.Tables[i].Name, err)
}
}
for _, table := range m.Tables {
if err := freshDB(db).Exec(table.Create).Error; err != nil {
return fmt.Errorf("restore schema for %s: %w", table.Name, err)
}
}
var prefix, rowSQL, tableName string
var args []any
var groups []string
var bytes int
flush := func() error {
if len(groups) == 0 {
return nil
}
// Bind through database/sql: GORM expands a byte slice after '('.
// Direct execution also keeps restored values out of SQL logs.
if _, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, prefix+strings.Join(groups, ","), args...); err != nil {
return fmt.Errorf("restore rows in %s: %w", tableName, err)
}
args, groups, bytes = nil, nil, 0
return nil
}
_, err = readSnapshot(path, func(table snapshotTable, values []sql.RawBytes) error {
if tableName != table.Name {
if err := flush(); err != nil {
return err
}
tableName = table.Name
columns, placeholders := make([]string, len(values)), make([]string, len(values))
for i := range values {
columns[i], placeholders[i] = quoteIdentifier(table.Columns[i]), "?"
}
prefix = "INSERT INTO " + quoteIdentifier(tableName) + " (" + strings.Join(columns, ",") + ") VALUES "
rowSQL = "(" + strings.Join(placeholders, ",") + ")"
}
for _, value := range values {
if value == nil {
args = append(args, nil)
} else {
args = append(args, []byte(value))
bytes += len(value)
}
}
groups = append(groups, rowSQL)
if len(groups) >= 250 || len(args)+len(values) > 60000 || bytes >= 1<<20 {
return flush()
}
return nil
})
if err == nil {
err = flush()
}
if err != nil {
return err
}
return restoreSnapshotTriggers(db, m)
}
+87
View File
@@ -0,0 +1,87 @@
package mariadb
import (
"archive/zip"
"database/sql"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"testing"
)
func TestSnapshotRowsAndValidation(t *testing.T) {
row := []sql.RawBytes{[]byte{0, 1, 255}, {}, nil}
var sum [32]byte
addRowDigest(&sum, row)
for _, tt := range []struct {
name string
format int
rows int64
digest string
width int
valid bool
}{
{"valid", 1, 1, hex.EncodeToString(sum[:]), 3, true},
{"bad format", 2, 1, hex.EncodeToString(sum[:]), 3, false},
{"bad count", 1, 2, hex.EncodeToString(sum[:]), 3, false},
{"bad digest", 1, 1, "invalid", 3, false},
{"bad width", 1, 1, hex.EncodeToString(sum[:]), 2, false},
} {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "backup.zip")
file, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
archive := zip.NewWriter(file)
entry, err := archive.Create("table.jsonl")
if err != nil {
t.Fatal(err)
}
if err := json.NewEncoder(entry).Encode(row); err != nil {
t.Fatal(err)
}
entry, err = archive.Create("manifest.json")
if err != nil {
t.Fatal(err)
}
table := snapshotTable{Name: "example", Create: "CREATE TABLE `example` (id INT)", Entry: "table.jsonl", Columns: []string{"a", "b", "c"}[:tt.width], Rows: tt.rows, Digest: tt.digest}
if err := json.NewEncoder(entry).Encode(snapshotManifest{Format: tt.format, Schema: "example", Tables: []snapshotTable{table}}); err != nil {
t.Fatal(err)
}
if err := archive.Close(); err != nil {
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
_, err = readSnapshot(path, nil)
if (err == nil) != tt.valid {
t.Fatalf("valid=%v err=%v", tt.valid, err)
}
if !tt.valid {
if err := RestoreSnapshot(nil, path); err == nil {
t.Fatal("invalid archive reached restore without preflight refusal")
}
}
})
}
}
func TestSnapshotDigestPreservesValues(t *testing.T) {
a, b := []sql.RawBytes{[]byte("a")}, []sql.RawBytes{[]byte("b")}
var forward, reverse, null, empty [32]byte
addRowDigest(&forward, a)
addRowDigest(&forward, b)
addRowDigest(&reverse, b)
addRowDigest(&reverse, a)
if forward != reverse {
t.Fatal("digest depends on row order")
}
addRowDigest(&null, []sql.RawBytes{nil})
addRowDigest(&empty, []sql.RawBytes{{}})
if null == empty {
t.Fatal("digest conflates NULL and empty bytes")
}
}
@@ -0,0 +1,43 @@
package mariadb
import (
"fmt"
"regexp"
"strings"
)
// Recovery covers the selected schema, so external trigger effects are refused.
func validateSnapshotTriggerReferences(trigger snapshotTrigger, schema string) error {
body := trigger.Body
quoted := `'(?:''|\\.|[^'\\])*'`
if strings.Contains(trigger.SQLMode, "NO_BACKSLASH_ESCAPES") {
quoted = `'(?:''|[^'])*'`
}
body = regexp.MustCompile(quoted).ReplaceAllString(body, " ")
if !strings.Contains(trigger.SQLMode, "ANSI_QUOTES") {
double := `"(?:""|\\.|[^"\\])*"`
if strings.Contains(trigger.SQLMode, "NO_BACKSLASH_ESCAPES") {
double = `"(?:""|[^"])*"`
}
body = regexp.MustCompile(double).ReplaceAllString(body, " ")
}
if strings.Contains(body, "/*!") || strings.Contains(body, "/*M!") {
return fmt.Errorf("trigger %s has unsupported executable comments", trigger.Name)
}
body = regexp.MustCompile(`(?s)/\*.*?\*/|--[\t ][^\n]*|#[^\n]*`).ReplaceAllString(body, " ")
if regexp.MustCompile(`(?i)\bCALL\s+`).MatchString(body) {
return fmt.Errorf("trigger %s calls a routine outside supported recovery", trigger.Name)
}
identifier := "(?:`(?:``|[^`])+`|\"(?:\"\"|[^\"])+\"|[\\pL_$][\\pL\\pN_$]*)"
for _, pattern := range []string{
"(?i)\\b(?:FROM|JOIN|UPDATE|INTO)\\s+(?:(?:LOW_PRIORITY|HIGH_PRIORITY|DELAYED|IGNORE)\\s+)*(" + identifier + ")\\s*\\.",
"(?i)(" + identifier + ")\\s*\\.\\s*" + identifier + "\\s*\\(",
} {
for _, match := range regexp.MustCompile(pattern).FindAllStringSubmatch(body, -1) {
if !strings.EqualFold(strings.Trim(match[1], "`\""), schema) {
return fmt.Errorf("trigger %s references another schema outside supported recovery", trigger.Name)
}
}
}
return nil
}
@@ -0,0 +1,29 @@
package mariadb
import "testing"
func TestSnapshotTriggerReferences(t *testing.T) {
for _, tt := range []struct {
name, body, mode string
fail bool
}{
{"local", "INSERT INTO audit VALUES (NEW.id, OLD.value)", "", false},
{"local qualified", "UPDATE `example`.`audit` SET id=NEW.id", "", false},
{"alias", "SELECT 1 FROM audit a WHERE a.id=OLD.id", "", false},
{"literal", "INSERT INTO audit VALUES ('example.com', '$.field', 'UPDATE other.audit')", "", false},
{"comment", "/* UPDATE other.audit */ INSERT INTO audit VALUES (NEW.id)", "", false},
{"external write", "UPDATE other.audit SET id=NEW.id", "", true},
{"external insert", "INSERT INTO `other`.`audit` VALUES (NEW.id)", "", true},
{"ANSI external", `UPDATE "other"."audit" SET id=NEW.id`, "ANSI_QUOTES", true},
{"external function", "SET NEW.value=other.example(NEW.value)", "", true},
{"routine", "CALL example.change_rows()", "", true},
{"executable", "/*!50000 UPDATE other.audit SET id=1 */", "", true},
} {
t.Run(tt.name, func(t *testing.T) {
err := validateSnapshotTriggerReferences(snapshotTrigger{Name: "example", Body: tt.body, SQLMode: tt.mode}, "example")
if (err != nil) != tt.fail {
t.Fatalf("error=%v", err)
}
})
}
}
+83
View File
@@ -0,0 +1,83 @@
package mariadb
import (
"errors"
"fmt"
"strings"
"gorm.io/gorm"
)
type snapshotTrigger struct {
Name string `gorm:"column:TRIGGER_NAME"`
Table string `gorm:"column:EVENT_OBJECT_TABLE"`
Event string `gorm:"column:EVENT_MANIPULATION"`
Timing string `gorm:"column:ACTION_TIMING"`
Order int `gorm:"column:ACTION_ORDER"`
Body string `gorm:"column:ACTION_STATEMENT"`
SQLMode string `gorm:"column:SQL_MODE"`
Definer string `gorm:"column:DEFINER"`
Charset string `gorm:"column:CHARACTER_SET_CLIENT"`
Collation string `gorm:"column:COLLATION_CONNECTION"`
DatabaseCollation string `gorm:"column:DATABASE_COLLATION"`
}
func readSnapshotTriggers(db *gorm.DB, m *snapshotManifest) error {
query := `SELECT TRIGGER_NAME, EVENT_OBJECT_TABLE, EVENT_MANIPULATION, ACTION_TIMING,
ACTION_ORDER, ACTION_STATEMENT, SQL_MODE, DEFINER, CHARACTER_SET_CLIENT,
COLLATION_CONNECTION, DATABASE_COLLATION FROM information_schema.TRIGGERS
WHERE TRIGGER_SCHEMA=? ORDER BY EVENT_OBJECT_TABLE, ACTION_TIMING, EVENT_MANIPULATION, ACTION_ORDER`
if err := freshDB(db).Raw(query, m.Schema).Scan(&m.Triggers).Error; err != nil {
return fmt.Errorf("read snapshot triggers: %w", err)
}
for _, trigger := range m.Triggers {
if err := validateSnapshotTriggerReferences(trigger, m.Schema); err != nil {
return err
}
if trigger.DatabaseCollation != m.Collation {
return fmt.Errorf("trigger %s has a legacy database collation; restore support is required before migration", trigger.Name)
}
}
return nil
}
func restoreSnapshotTriggers(db *gorm.DB, m *snapshotManifest) (err error) {
if len(m.Triggers) == 0 {
return nil
}
var mode, charset, collation, schema string
if err := freshDB(db).Raw("SELECT @@sql_mode, @@character_set_client, @@collation_connection, DATABASE()").Row().Scan(&mode, &charset, &collation, &schema); err != nil {
return fmt.Errorf("read trigger restore session: %w", err)
}
defer func() {
for _, reset := range []struct{ sql, value string }{{"SET sql_mode=?", mode}, {"SET character_set_client=?", charset}, {"SET collation_connection=?", collation}} {
if e := freshDB(db).Exec(reset.sql, reset.value).Error; e != nil {
err = errors.Join(err, fmt.Errorf("reset trigger restore session: %w", e))
}
}
}()
for _, trigger := range m.Triggers {
if trigger.Timing != "BEFORE" && trigger.Timing != "AFTER" {
return fmt.Errorf("invalid trigger timing")
}
if trigger.Event != "INSERT" && trigger.Event != "UPDATE" && trigger.Event != "DELETE" {
return fmt.Errorf("invalid trigger event")
}
at := strings.LastIndex(trigger.Definer, "@")
if at < 1 || at == len(trigger.Definer)-1 {
return fmt.Errorf("invalid trigger definer")
}
for _, setting := range []struct{ sql, value string }{{"SET sql_mode=?", trigger.SQLMode}, {"SET character_set_client=?", trigger.Charset}, {"SET collation_connection=?", trigger.Collation}} {
if err := freshDB(db).Exec(setting.sql, setting.value).Error; err != nil {
return fmt.Errorf("set trigger restore session: %w", err)
}
}
// Explicitly bind name and target table to the destination. Trigger bodies
// retain their original SQL; verification performs no DML after creation.
statement := "CREATE DEFINER=" + quoteIdentifier(trigger.Definer[:at]) + "@" + quoteIdentifier(trigger.Definer[at+1:]) + " TRIGGER " + quoteIdentifier(schema) + "." + quoteIdentifier(trigger.Name) + " " + trigger.Timing + " " + trigger.Event + " ON " + quoteIdentifier(schema) + "." + quoteIdentifier(trigger.Table) + " FOR EACH ROW " + trigger.Body
if _, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, statement); err != nil {
return fmt.Errorf("restore trigger %s: %w", trigger.Name, err)
}
}
return nil
}
+79
View File
@@ -0,0 +1,79 @@
package mariadb
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"reflect"
"gorm.io/gorm"
)
// CheckSnapshot compares restored schema and all writable row values.
func CheckSnapshot(db *gorm.DB, path string) error {
m, err := readSnapshot(path, nil)
if err != nil {
return err
}
return compareSnapshot(db, m)
}
// VerifySnapshot proves restore readiness in an isolated temporary database.
func VerifySnapshot(db *gorm.DB, path string) (err error) {
m, err := readSnapshot(path, nil)
if err != nil {
return err
}
var random [12]byte
if _, err := rand.Read(random[:]); err != nil {
return fmt.Errorf("name verification schema: %w", err)
}
scratch := "qfs_verify_" + hex.EncodeToString(random[:])
var original string
if err := freshDB(db).Raw("SELECT DATABASE()").Scan(&original).Error; err != nil {
return fmt.Errorf("read original schema: %w", err)
}
if err := freshDB(db).Exec("CREATE DATABASE " + quoteIdentifier(scratch) + " COLLATE " + quoteIdentifier(m.Collation)).Error; err != nil {
return fmt.Errorf("create restore verification schema: %w", err)
}
defer func() {
if e := freshDB(db).Exec("USE " + quoteIdentifier(original)).Error; e != nil {
err = errors.Join(err, fmt.Errorf("return to original schema: %w", e))
}
if e := freshDB(db).Exec("DROP DATABASE " + quoteIdentifier(scratch)).Error; e != nil {
err = errors.Join(err, fmt.Errorf("remove verification schema %s: %w", scratch, e))
}
}()
if err := freshDB(db).Exec("USE " + quoteIdentifier(scratch)).Error; err != nil {
return fmt.Errorf("select verification schema: %w", err)
}
if err := restoreSnapshotTables(db, path, m); err != nil {
return err
}
return compareSnapshot(db, m)
}
func compareSnapshot(db *gorm.DB, m *snapshotManifest) error {
current, err := snapshotInventory(db)
if err != nil {
return err
}
if !reflect.DeepEqual(current.Triggers, m.Triggers) {
return fmt.Errorf("restored trigger metadata mismatch")
}
if len(current.Tables) != len(m.Tables) {
return fmt.Errorf("restored table count mismatch")
}
for i := range m.Tables {
want, got := m.Tables[i], current.Tables[i]
if err := snapshotRows(db, &got, io.Discard); err != nil {
return err
}
if want.Name != got.Name || want.Create != got.Create || want.Rows != got.Rows || want.Digest != got.Digest {
return fmt.Errorf("restored snapshot mismatch in %s (name=%t schema=%t rows=%t digest=%t)", want.Name, want.Name == got.Name, want.Create == got.Create, want.Rows == got.Rows, want.Digest == got.Digest)
}
}
return nil
}
@@ -0,0 +1,162 @@
//go:build integration
package models
import (
"context"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"testing/fstest"
"time"
qfassets "git.mchus.pro/mchus/quoteforge"
"git.mchus.pro/mchus/quoteforge/internal/mariadb"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// Input is read-only. All SQL runs in a new, disposable local container.
func TestProductionDumpMigrationMariaDB(t *testing.T) {
dump := os.Getenv("QFS_TEST_PRODUCTION_DUMP")
if dump == "" {
t.Skip("QFS_TEST_PRODUCTION_DUMP is not set")
}
input, err := os.Open(dump)
if err != nil {
t.Fatal(err)
}
defer input.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
image := os.Getenv("QFS_TEST_MARIADB_IMAGE")
if image == "" {
image = "mariadb:11.8.3"
}
out, err := exec.CommandContext(ctx, "docker", "run", "--detach", "--rm", "--pull=never", "-p", "127.0.0.1::3306",
"-e", "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1", "-e", "MARIADB_ROOT_HOST=%", "-e", "MARIADB_DATABASE=RFQ_LOG", image,
"--skip-ssl", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_uca1400_ai_ci", "--max-allowed-packet=256M").CombinedOutput()
if err != nil {
t.Fatalf("start dump rehearsal MariaDB: %v", err)
}
id := strings.TrimSpace(string(out))
t.Cleanup(func() {
if _, err := exec.Command("docker", "stop", "-t", "2", id).CombinedOutput(); err != nil {
t.Errorf("stop dump rehearsal MariaDB: %v", err)
}
})
out, err = exec.CommandContext(ctx, "docker", "port", id, "3306/tcp").CombinedOutput()
if err != nil {
t.Fatal(err)
}
dsn := "root@tcp(" + strings.TrimSpace(string(out)) + ")/RFQ_LOG?parseTime=true&timeout=1s&tls=false"
var db *gorm.DB
deadline := time.Now().Add(45 * time.Second)
for {
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err == nil {
break
}
if time.Now().After(deadline) {
t.Fatal("dump rehearsal database did not become ready")
}
time.Sleep(150 * time.Millisecond)
}
pool, err := db.DB()
if err != nil {
t.Fatal(err)
}
defer pool.Close()
pool.SetMaxOpenConns(1)
pool.SetMaxIdleConns(1)
importer := exec.CommandContext(ctx, "docker", "exec", "-i", id, "mariadb", "--skip-ssl", "--binary-mode", "--default-character-set=utf8mb4", "RFQ_LOG")
importer.Stdin = input
if output, err := importer.CombinedOutput(); err != nil {
// Import errors can include entire data rows; report only error code/line.
location := regexp.MustCompile(`(?m)^ERROR[^\n]*`).Find(output)
location = regexp.MustCompile(`'[^']*'`).ReplaceAll(location, []byte("'<redacted>'"))
if len(location) > 250 {
location = location[:250]
}
t.Fatalf("load dump into disposable database: %v (%s)", err, location)
}
var version, collation string
if err := db.Raw("SELECT VERSION(), @@collation_database").Row().Scan(&version, &collation); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(version, "11.8.3-") || collation != "utf8mb4_uca1400_ai_ci" {
t.Fatalf("unexpected rehearsal server: version=%s collation=%s", version, collation)
}
var tables, triggers int64
if err := db.Raw("SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE()").Scan(&tables).Error; err != nil {
t.Fatal(err)
}
if err := db.Raw("SELECT COUNT(*) FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA=DATABASE()").Scan(&triggers).Error; err != nil {
t.Fatal(err)
}
t.Logf("production dump imported: MariaDB=%s collation=%s tables=%d triggers=%d", version, collation, tables, triggers)
if err := db.Exec("SET time_zone='+00:00'").Error; err != nil {
t.Fatal(err)
}
root := t.TempDir()
var original string
if err := db.Connection(func(conn *gorm.DB) error {
var err error
original, err = mariadb.CreateSnapshot(conn, root, "production-dump")
if err != nil {
return err
}
return mariadb.VerifySnapshot(conn, original)
}); err != nil {
t.Fatal(err)
}
t.Log("full dump snapshot restore verified")
const filename = "035_add_server_updated_at.sql"
if err := db.Where("filename=?", filename).Delete(&SQLSchemaMigration{}).Error; err != nil {
t.Fatal(err)
}
// Reconstruct the real pre-035 schema only in this disposable clone.
for _, table := range []string{"qt_configurations", "qt_projects"} {
if err := db.Exec("ALTER TABLE " + table + " DROP INDEX IF EXISTS idx_" + table + "_server_updated_at, DROP COLUMN IF EXISTS server_updated_at").Error; err != nil {
t.Fatal(err)
}
}
var before string
if err := db.Connection(func(conn *gorm.DB) error {
var err error
before, err = mariadb.CreateSnapshot(conn, root, "before-rehearsal")
return err
}); err != nil {
t.Fatal(err)
}
content, err := qfassets.MigrationFiles.ReadFile("migrations/" + filename)
if err != nil {
t.Fatal(err)
}
files := fstest.MapFS{filename: &fstest.MapFile{Data: content}, "036_recovery_probe.sql": &fstest.MapFile{Data: []byte("-- Tables affected: qt_configurations\n-- verify: deliberate rehearsal failure | SELECT 1\nALTER TABLE qt_configurations ADD COLUMN qfs_recovery_probe INT;")}}
if _, err := RunSQLMigrations(db, files, nil, root); err == nil || !strings.Contains(err.Error(), "all session changes restored") {
t.Fatalf("dump rollback rehearsal: %v", err)
}
if err := mariadb.CheckSnapshot(db, before); err != nil {
t.Fatalf("dump changed after rollback: %v", err)
}
t.Log("real migration plus later failed DDL/verify restored the complete dump")
delete(files, "036_recovery_probe.sql")
if applied, err := RunSQLMigrations(db, files, nil, root); err != nil || len(applied) != 1 {
t.Fatalf("real migration rehearsal: applied=%v error=%v", applied, err)
}
states, err := SQLMigrationStatus(db, files)
if err != nil || len(states) != 1 || states[0].AppliedAt == nil {
t.Fatalf("rehearsal history invalid: %v", err)
}
if err := RestoreSQLMigrationSession(db, original); err != nil {
t.Fatal(err)
}
if err := mariadb.CheckSnapshot(db, original); err != nil {
t.Fatal(err)
}
t.Logf("production dump rehearsal complete: %s, exact original restored", filename)
}
@@ -0,0 +1,44 @@
package models
import (
"crypto/sha256"
"fmt"
"strings"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
// migrationJournalName uses the configured endpoint, which survives server restarts.
func migrationJournalName(db *gorm.DB, schema string) (string, error) {
dialect, ok := db.Dialector.(*mysql.Dialector)
if !ok || dialect.DSNConfig == nil {
return "", fmt.Errorf("migration requires a MariaDB DSN")
}
identity := dialect.DSNConfig.Net + "/" + dialect.DSNConfig.Addr + "/" + schema
return fmt.Sprintf("qfs-migration-v1-%x.pending", sha256.Sum256([]byte(identity))), nil
}
func requireExclusiveMigrationSession(db *gorm.DB) error {
var grants []string
if err := db.Session(&gorm.Session{NewDB: true}).Raw("SHOW GRANTS FOR CURRENT_USER").Scan(&grants).Error; err != nil {
return fmt.Errorf("read maintenance privileges: %w", err)
}
allowed := false
for _, grant := range grants {
if strings.Contains(grant, " ON *.* ") && strings.Contains(grant, "ALL PRIVILEGES") {
allowed = true
}
}
if !allowed {
return fmt.Errorf("migration recovery requires a DBA with directly granted global ALL PRIVILEGES to inspect all objects and clients")
}
var clients int64
if err := db.Session(&gorm.Session{NewDB: true}).Raw("SELECT COUNT(*) FROM information_schema.PROCESSLIST WHERE ID<>CONNECTION_ID() AND DB=DATABASE()").Scan(&clients).Error; err != nil {
return fmt.Errorf("check maintenance clients: %w", err)
}
if clients != 0 {
return fmt.Errorf("migration requires maintenance window: %d other connections use this schema", clients)
}
return nil
}
+45
View File
@@ -0,0 +1,45 @@
package models
import (
"fmt"
"io/fs"
"regexp"
"gorm.io/gorm"
)
func planSQLMigrations(db *gorm.DB, migrations fs.FS) ([]plannedSQLMigration, error) {
states, err := SQLMigrationStatus(db, migrations)
if err != nil {
return nil, err
}
var plans []plannedSQLMigration
unsupported := regexp.MustCompile(`(?i)\b(?:CREATE|DROP|ALTER)\s+(?:DATABASE|SCHEMA|VIEW|TRIGGER|PROCEDURE|FUNCTION|EVENT)\b|\b(?:USE|DELIMITER|GRANT|REVOKE)\s|\bSET\s+(?:GLOBAL|SQL_MODE)\b|\bENGINE\s*=\s*(?:MyISAM|Aria|MEMORY|BLACKHOLE)\b`)
for _, state := range states {
if state.AppliedAt != nil {
continue
}
content, err := fs.ReadFile(migrations, state.Filename)
if err != nil {
return nil, fmt.Errorf("read migration %s: %w", state.Filename, err)
}
statements, err := splitSQLStatements(string(content))
if err != nil {
return nil, fmt.Errorf("parse migration %s: %w", state.Filename, err)
}
checks, err := parseMigrationVerifications(string(content))
if err != nil {
return nil, fmt.Errorf("migration %s verify headers: %w", state.Filename, err)
}
if len(checks) == 0 {
return nil, fmt.Errorf("migration %s needs verify headers", state.Filename)
}
for _, stmt := range statements {
if unsupported.MatchString(stmt) {
return nil, fmt.Errorf("migration %s contains SQL outside supported table recovery", state.Filename)
}
}
plans = append(plans, plannedSQLMigration{state.Filename, statements, checks})
}
return plans, nil
}
+67
View File
@@ -0,0 +1,67 @@
package models
import (
"crypto/sha256"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"git.mchus.pro/mchus/quoteforge/internal/mariadb"
"gorm.io/gorm"
)
// RestoreSQLMigrationSession is the shipped recovery path after interruption.
func RestoreSQLMigrationSession(db *gorm.DB, snapshot string) error {
return db.Connection(func(conn *gorm.DB) (err error) {
fresh := func() *gorm.DB { return conn.Session(&gorm.Session{NewDB: true}) }
var schema, zone string
var acquired int
if err := fresh().Raw("SELECT DATABASE(), @@time_zone").Row().Scan(&schema, &zone); err != nil {
return fmt.Errorf("read recovery session: %w", err)
}
lock := fmt.Sprintf("qfs-migrate-%x", sha256.Sum256([]byte(schema)))
if err := fresh().Raw("SELECT GET_LOCK(?,0)", lock).Scan(&acquired).Error; err != nil || acquired != 1 {
return fmt.Errorf("migration lock unavailable for recovery")
}
defer func() {
if e := fresh().Exec("SET time_zone=?", zone).Error; e != nil {
err = errors.Join(err, fmt.Errorf("reset recovery time zone: %w", e))
}
if e := fresh().Raw("SELECT RELEASE_LOCK(?)", lock).Scan(&acquired).Error; e != nil {
err = errors.Join(err, fmt.Errorf("release recovery lock: %w", e))
}
}()
if err := requireExclusiveMigrationSession(fresh()); err != nil {
return err
}
journalName, err := migrationJournalName(conn, schema)
if err != nil {
return err
}
journal := filepath.Join(filepath.Dir(snapshot), journalName)
if content, e := os.ReadFile(journal); e == nil {
if strings.TrimSpace(string(content)) != snapshot {
return fmt.Errorf("pending recovery requires its original snapshot: %s", strings.TrimSpace(string(content)))
}
} else if !os.IsNotExist(e) {
return fmt.Errorf("read recovery marker: %w", e)
}
if err := fresh().Exec("SET time_zone='+00:00'").Error; err != nil {
return fmt.Errorf("set restore time zone: %w", err)
}
if err := mariadb.VerifySnapshot(fresh(), snapshot); err != nil {
return fmt.Errorf("verify recovery snapshot: %w", err)
}
if err := mariadb.RestoreSnapshot(fresh(), snapshot); err != nil {
return fmt.Errorf("restore migration session: %w", err)
}
if err := os.Remove(journal); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("clear recovery marker: %w", err)
}
slog.Info("server migration snapshot restored", "schema", schema, "backup", snapshot, "state", "verified")
return nil
})
}
+165
View File
@@ -0,0 +1,165 @@
package models
import (
"crypto/sha256"
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"git.mchus.pro/mchus/quoteforge/internal/appstate"
"git.mchus.pro/mchus/quoteforge/internal/mariadb"
"gorm.io/gorm"
)
type plannedSQLMigration struct {
filename string
statements []string
checks []migrationVerification
}
// RunSQLMigrations requires a DBA maintenance window: stop all other writers.
// Backups are mandatory; each file gets per-table snapshots before any DDL.
func RunSQLMigrations(db *gorm.DB, migrations fs.FS, onApply func(string), backupRoot string) ([]string, error) {
var applied []string
err := db.Connection(func(conn *gorm.DB) (err error) {
fresh := func() *gorm.DB { return conn.Session(&gorm.Session{NewDB: true}) }
var schema, mode, zone, engine string
var foreignKeys int
if err := fresh().Raw("SELECT DATABASE(), @@sql_mode, @@time_zone, @@foreign_key_checks, @@default_storage_engine").Row().Scan(&schema, &mode, &zone, &foreignKeys, &engine); err != nil {
return fmt.Errorf("read migration session: %w", err)
}
if schema == "" || strings.Contains(mode, "NO_BACKSLASH_ESCAPES") || engine != "InnoDB" {
return fmt.Errorf("migration requires a selected InnoDB schema and backslash escape SQL mode")
}
lock := fmt.Sprintf("qfs-migrate-%x", sha256.Sum256([]byte(schema)))
var acquired int
if err := fresh().Raw("SELECT GET_LOCK(?,0)", lock).Scan(&acquired).Error; err != nil || acquired != 1 {
return fmt.Errorf("migration lock unavailable for %s", schema)
}
defer func() {
for _, reset := range []struct {
sql string
value any
}{{"SET time_zone=?", zone}, {"SET sql_mode=?", mode}, {"SET foreign_key_checks=?", foreignKeys}} {
if e := fresh().Exec(reset.sql, reset.value).Error; e != nil {
err = errors.Join(err, fmt.Errorf("reset migration session: %w", e))
}
}
if e := fresh().Raw("SELECT RELEASE_LOCK(?)", lock).Scan(&acquired).Error; e != nil {
err = errors.Join(err, fmt.Errorf("release migration lock: %w", e))
}
}()
plans, err := planSQLMigrations(fresh(), migrations)
if err != nil {
return err
}
if err := appstate.ValidateBackupLocation(backupRoot); err != nil {
return err
}
if err := os.MkdirAll(backupRoot, 0700); err != nil {
return fmt.Errorf("create migration backup root: %w", err)
}
journalName, err := migrationJournalName(conn, schema)
if err != nil {
return err
}
journal := filepath.Join(backupRoot, journalName)
if content, e := os.ReadFile(journal); e == nil {
return fmt.Errorf("interrupted migration requires restore before retry; safety snapshot: %s", strings.TrimSpace(string(content)))
} else if !os.IsNotExist(e) {
return fmt.Errorf("read migration recovery marker: %w", e)
}
if len(plans) == 0 {
return nil
}
if err := requireExclusiveMigrationSession(fresh()); err != nil {
return err
}
if err := fresh().Exec("SET time_zone='+00:00'").Error; err != nil {
return fmt.Errorf("set snapshot time zone: %w", err)
}
full, err := mariadb.CreateSnapshot(fresh(), backupRoot, "session")
if err != nil {
return fmt.Errorf("pre-migration snapshot: %w", err)
}
if err := mariadb.VerifySnapshot(fresh(), full); err != nil {
return fmt.Errorf("pre-migration restore verification: %w", err)
}
marker, err := os.OpenFile(journal, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
return fmt.Errorf("create migration recovery marker: %w", err)
}
_, writeErr := fmt.Fprintln(marker, full)
syncErr, closeErr := marker.Sync(), marker.Close()
if e := errors.Join(writeErr, syncErr, closeErr); e != nil {
return fmt.Errorf("persist migration recovery marker: %w", e)
}
slog.Info("verified server migration backup", "schema", schema, "path", full)
var steps []string
for _, plan := range plans {
if err := requireExclusiveMigrationSession(fresh()); err != nil {
return recoverSQLMigration(fresh(), journal, full, steps, plan.filename, err)
}
step, err := mariadb.CreateSnapshot(fresh(), backupRoot, plan.filename)
if err == nil {
err = mariadb.VerifySnapshot(fresh(), step)
}
if err != nil {
return recoverSQLMigration(fresh(), journal, full, steps, plan.filename, err)
}
steps = append(steps, step)
if onApply != nil {
onApply(plan.filename)
}
if err := ensureSQLMigrationsTable(fresh()); err != nil {
return recoverSQLMigration(fresh(), journal, full, steps, plan.filename, err)
}
if err := applySQLMigrationOnSession(fresh(), plan.filename, plan.statements, plan.checks); err != nil {
return recoverSQLMigration(fresh(), journal, full, steps, plan.filename, err)
}
applied = append(applied, plan.filename)
}
if err := os.Remove(journal); err != nil {
return fmt.Errorf("remove completed migration marker: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
return applied, nil
}
func recoverSQLMigration(db *gorm.DB, journal, full string, steps []string, filename string, cause error) error {
slog.Error("server migration failed", "step", filename, "error", cause, "backup", full)
if err := requireExclusiveMigrationSession(db); err != nil {
return errors.Join(cause, fmt.Errorf("recovery refused with active clients; keep maintenance window and restore %s: %w", full, err))
}
var recovery error
for i := len(steps) - 1; i >= 0; i-- {
slog.Warn("restoring migration step", "step", filename, "backup", steps[i])
if err := mariadb.RestoreSnapshot(db, steps[i]); err != nil {
recovery = err
break
}
}
if recovery == nil {
recovery = mariadb.CheckSnapshot(db, full)
}
if recovery != nil {
slog.Warn("restoring full migration session", "backup", full, "error", recovery)
recovery = mariadb.RestoreSnapshot(db, full)
}
if recovery != nil {
return errors.Join(cause, fmt.Errorf("recovery failed; database requires restore from %s: %w", full, recovery))
}
if err := os.Remove(journal); err != nil {
return errors.Join(cause, fmt.Errorf("database restored but recovery marker remains: %w", err))
}
slog.Warn("server migration session restored", "step", filename, "state", "before-session", "backup", full)
return fmt.Errorf("migration %s failed; all session changes restored: %w", filename, cause)
}
@@ -0,0 +1,227 @@
//go:build integration
package models
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"testing/fstest"
"time"
"git.mchus.pro/mchus/quoteforge/internal/mariadb"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func TestSQLMigrationSafetyMariaDB(t *testing.T) {
out, err := exec.Command("docker", "run", "--detach", "--rm", "--pull=never", "-p", "127.0.0.1::3306", "-e", "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1", "-e", "MARIADB_ROOT_HOST=%", "mariadb:11.8", "--skip-ssl").CombinedOutput()
if err != nil {
t.Fatalf("start test MariaDB: %v: %s", err, out)
}
id := strings.TrimSpace(string(out))
t.Cleanup(func() {
if out, err := exec.Command("docker", "stop", "-t", "2", id).CombinedOutput(); err != nil {
t.Errorf("stop test MariaDB: %v: %s", err, out)
}
})
out, err = exec.Command("docker", "port", id, "3306/tcp").CombinedOutput()
if err != nil {
t.Fatalf("get test MariaDB port: %v: %s", err, out)
}
addr := strings.TrimSpace(string(out))
dsn := "root@tcp(" + addr + ")/?parseTime=true&timeout=1s&tls=false"
var admin *gorm.DB
deadline := time.Now().Add(45 * time.Second)
for {
admin, err = gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err == nil {
break
}
if time.Now().After(deadline) {
t.Fatalf("test MariaDB unavailable: %v", err)
}
time.Sleep(150 * time.Millisecond)
}
pool, err := admin.DB()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { pool.Close() })
newDB := func(t *testing.T) *gorm.DB {
t.Helper()
schema := fmt.Sprintf("qfs_test_%d", time.Now().UnixNano())
if err := admin.Exec("CREATE DATABASE " + schema + " COLLATE utf8mb4_unicode_ci").Error; err != nil {
t.Fatal(err)
}
db, err := gorm.Open(mysql.Open(strings.Replace(dsn, ")/?", ")/"+schema+"?", 1)), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err != nil {
t.Fatal(err)
}
p, err := db.DB()
if err != nil {
t.Fatal(err)
}
p.SetMaxOpenConns(1)
p.SetMaxIdleConns(1)
t.Cleanup(func() {
p.Close()
if err := admin.Exec("DROP DATABASE " + schema).Error; err != nil {
t.Error(err)
}
})
for _, sql := range []string{
"SET time_zone='+00:00'",
"CREATE TABLE parent (id INT PRIMARY KEY AUTO_INCREMENT, value TEXT, empty_value TEXT, nullable TEXT, raw VARBINARY(32), amount DECIMAL(20,6), stamp TIMESTAMP(6), generated INT AS (id+1) VIRTUAL) ENGINE=InnoDB",
"INSERT INTO parent (id,value,empty_value,nullable,raw,amount,stamp) VALUES (1,'quote; and ''text''','',NULL,X'0001FF',123.456789,'2026-10-26 12:34:56.123456')",
"SET @saved_test_mode=@@sql_mode",
"SET sql_mode='NO_AUTO_VALUE_ON_ZERO'",
"INSERT INTO parent (id,value,stamp) VALUES (0,'legacy','0000-00-00 00:00:00')",
"SET sql_mode=@saved_test_mode",
"CREATE TABLE child (id INT PRIMARY KEY, parent_id INT, CONSTRAINT child_parent FOREIGN KEY (parent_id) REFERENCES parent(id)) ENGINE=InnoDB",
"INSERT INTO child VALUES (1,1)",
"CREATE TABLE audit (id INT PRIMARY KEY AUTO_INCREMENT, note TEXT) ENGINE=InnoDB",
"CREATE TRIGGER insert_value BEFORE INSERT ON parent FOR EACH ROW SET NEW.value=CONCAT(NEW.value,'!')",
"CREATE TRIGGER audit_z AFTER UPDATE ON parent FOR EACH ROW INSERT INTO audit (note) VALUES ('first')",
"CREATE TRIGGER audit_a AFTER UPDATE ON parent FOR EACH ROW INSERT INTO audit (note) VALUES ('second')",
} {
if err := db.Exec(sql).Error; err != nil {
t.Fatal(err)
}
}
return db
}
migration := func(sql, checks string) *fstest.MapFile {
return &fstest.MapFile{Data: []byte("-- Tables affected: parent, child\n" + checks + "\n" + sql)}
}
for _, failure := range []string{"none", "verify", "FK"} {
t.Run(failure, func(t *testing.T) {
db, root := newDB(t), t.TempDir()
var original string
err := db.Connection(func(conn *gorm.DB) error {
var e error
original, e = mariadb.CreateSnapshot(conn, root, "original")
return e
})
if err != nil {
t.Fatal(err)
}
files := fstest.MapFS{"001_a.sql": migration("SET @example='value; quoted'; PREPARE q FROM 'SELECT 1'; EXECUTE q; DEALLOCATE PREPARE q; ALTER TABLE parent ADD COLUMN added INT; UPDATE parent SET value=@example;", "-- verify: added missing | SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='parent' AND COLUMN_NAME='added' HAVING COUNT(*)=0\n-- verify: wrong value | SELECT 1 FROM parent WHERE value<>'value; quoted'")}
if failure == "verify" {
files["002_b.sql"] = migration("CREATE TABLE new_table (id INT); UPDATE parent SET value='changed';", "-- verify: deliberate failure | SELECT 1")
}
if failure == "FK" {
files["002_b.sql"] = migration("ALTER TABLE child ADD COLUMN added INT; ALTER TABLE child ADD CONSTRAINT broken_fk FOREIGN KEY (added) REFERENCES parent(missing);", "-- verify: should never run | SELECT 1 WHERE 0")
}
applied, err := RunSQLMigrations(db, files, nil, root)
if failure == "none" {
if err != nil || len(applied) != 1 {
t.Fatalf("apply=%v error=%v", applied, err)
}
var count int64
if err := db.Model(&SQLSchemaMigration{}).Count(&count).Error; err != nil || count != 1 {
t.Fatalf("history=%d err=%v", count, err)
}
} else {
if err == nil || !strings.Contains(err.Error(), "all session changes restored") || len(applied) != 0 {
t.Fatalf("apply=%v error=%v", applied, err)
}
if err := mariadb.CheckSnapshot(db, original); err != nil {
t.Fatalf("recovery changed original state: %v", err)
}
}
markers, err := filepath.Glob(filepath.Join(root, "*.pending"))
if err != nil || len(markers) != 0 {
t.Fatalf("markers=%v error=%v", markers, err)
}
})
}
t.Run("interrupted session", func(t *testing.T) {
db, root := newDB(t), t.TempDir()
files := fstest.MapFS{"001_a.sql": migration("ALTER TABLE parent ADD COLUMN added INT", "-- verify: ok | SELECT 1 WHERE 0")}
func() {
defer func() {
if recover() == nil {
t.Error("expected interruption")
}
}()
RunSQLMigrations(db, files, func(string) { panic("interruption") }, root)
}()
if err := db.Exec("ALTER TABLE parent ADD COLUMN added INT").Error; err != nil {
t.Fatal(err)
}
if _, err := RunSQLMigrations(db, files, nil, root); err == nil || !strings.Contains(err.Error(), "interrupted migration") {
t.Fatalf("retry not refused: %v", err)
}
markers, err := filepath.Glob(filepath.Join(root, "*.pending"))
if err != nil || len(markers) != 1 {
t.Fatalf("markers=%v err=%v", markers, err)
}
data, err := os.ReadFile(markers[0])
if err != nil {
t.Fatal(err)
}
if err := RestoreSQLMigrationSession(db, strings.TrimSpace(string(data))); err != nil {
t.Fatal(err)
}
if db.Table("parent").Migrator().HasColumn(&struct{ Added int }{}, "added") {
t.Fatal("interrupted DDL not restored")
}
if _, err := RunSQLMigrations(db, files, nil, root); err != nil {
t.Fatalf("retry after restore: %v", err)
}
})
t.Run("unsupported object", func(t *testing.T) {
db := newDB(t)
if err := db.Exec("CREATE VIEW example_view AS SELECT id FROM parent").Error; err != nil {
t.Fatal(err)
}
files := fstest.MapFS{"001_a.sql": migration("ALTER TABLE parent ADD COLUMN added INT", "-- verify: ok | SELECT 1 WHERE 0")}
if _, err := RunSQLMigrations(db, files, nil, t.TempDir()); err == nil || !strings.Contains(err.Error(), "snapshot refused") {
t.Fatalf("unsupported schema accepted: %v", err)
}
if db.Table("parent").Migrator().HasColumn(&struct{ Added int }{}, "added") {
t.Fatal("unsupported schema was changed")
}
})
t.Run("other client", func(t *testing.T) {
db := newDB(t)
p, err := db.DB()
if err != nil {
t.Fatal(err)
}
p.SetMaxOpenConns(2)
other, err := p.Conn(context.Background())
if err != nil {
t.Fatal(err)
}
defer other.Close()
files := fstest.MapFS{"001_a.sql": migration("ALTER TABLE parent ADD COLUMN added INT", "-- verify: ok | SELECT 1 WHERE 0")}
if _, err := RunSQLMigrations(db, files, nil, t.TempDir()); err == nil || !strings.Contains(err.Error(), "other connections") {
t.Fatalf("active client accepted: %v", err)
}
if db.Table("parent").Migrator().HasColumn(&struct{ Added int }{}, "added") {
t.Fatal("active schema changed")
}
})
t.Run("backup failure", func(t *testing.T) {
db := newDB(t)
path := filepath.Join(t.TempDir(), "not-directory")
if err := os.WriteFile(path, []byte("example"), 0600); err != nil {
t.Fatal(err)
}
files := fstest.MapFS{"001_a.sql": migration("ALTER TABLE parent ADD COLUMN added INT", "-- verify: ok | SELECT 1 WHERE 0")}
if _, err := RunSQLMigrations(db, files, nil, path); err == nil {
t.Fatal("backup failure ignored")
}
if db.Table("parent").Migrator().HasColumn(&struct{ Added int }{}, "added") {
t.Fatal("schema changed without backup")
}
})
}
+37 -29
View File
@@ -34,35 +34,43 @@ func applySQLMigrationFile(db *gorm.DB, filename, content string) error {
if err != nil { if err != nil {
return fmt.Errorf("parse migration %s: %w", filename, err) return fmt.Errorf("parse migration %s: %w", filename, err)
} }
statements, err := splitSQLStatements(content)
if err != nil {
return fmt.Errorf("parse migration %s SQL: %w", filename, err)
}
return db.Connection(func(conn *gorm.DB) error { return db.Connection(func(conn *gorm.DB) error {
if err := executeMigrationStatements(conn, filename, splitSQLStatements(content)); err != nil { return applySQLMigrationOnSession(conn, filename, statements, checks)
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
}) })
} }
func applySQLMigrationOnSession(conn *gorm.DB, filename string, statements []string, checks []migrationVerification) error {
if err := executeMigrationStatements(conn, filename, statements); 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
}
-72
View File
@@ -1,7 +1,6 @@
package models package models
import ( import (
"bufio"
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
@@ -124,48 +123,6 @@ func NeedsSQLMigrations(db *gorm.DB, migrations fs.FS) (bool, error) {
return count < int64(len(files)), nil 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 // IsMigrationPermissionError returns true if err indicates insufficient privileges
// to create/alter/read migration metadata or target schema objects. // to create/alter/read migration metadata or target schema objects.
func IsMigrationPermissionError(err error) bool { func IsMigrationPermissionError(err error) bool {
@@ -239,35 +196,6 @@ func isIgnorableMigrationError(message string) bool {
return false 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) { func listSQLMigrationFiles(migrations fs.FS) ([]string, error) {
entries, err := fs.ReadDir(migrations, ".") entries, err := fs.ReadDir(migrations, ".")
if err != nil { if err != nil {
+4 -1
View File
@@ -46,7 +46,10 @@ func TestSQLMigrationStatusAndListing(t *testing.T) {
} }
func TestSplitSQLStatementsSkipsComments(t *testing.T) { func TestSplitSQLStatementsSkipsComments(t *testing.T) {
got := splitSQLStatements("-- header; with semicolon\nALTER TABLE a ADD COLUMN x INT;\n\nALTER TABLE b\n ADD COLUMN y INT;\n") got, err := splitSQLStatements("-- header; with semicolon\nALTER TABLE a ADD COLUMN x INT;\n\nALTER TABLE b\n ADD COLUMN y INT;\n")
if err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[1] != "ALTER TABLE b\n ADD COLUMN y INT" { if len(got) != 2 || got[1] != "ALTER TABLE b\n ADD COLUMN y INT" {
t.Fatalf("unexpected statements %q", got) t.Fatalf("unexpected statements %q", got)
} }
+70
View File
@@ -0,0 +1,70 @@
package models
import (
"fmt"
"strings"
)
// splitSQLStatements supports MariaDB literals, quoted identifiers and comments.
func splitSQLStatements(script string) ([]string, error) {
var statements []string
var current strings.Builder
var quote byte
for i := 0; i < len(script); i++ {
ch := script[i]
if quote != 0 {
current.WriteByte(ch)
if ch == '\\' && quote != '`' && i+1 < len(script) {
i++
current.WriteByte(script[i])
} else if ch == quote {
if i+1 < len(script) && script[i+1] == quote {
i++
current.WriteByte(script[i])
} else {
quote = 0
}
}
continue
}
switch {
case ch == '\'' || ch == '"' || ch == '`':
quote = ch
current.WriteByte(ch)
case ch == '#' || (ch == '-' && i+2 < len(script) && script[i+1] == '-' && script[i+2] <= ' '):
for i < len(script) && script[i] != '\n' {
i++
}
current.WriteByte('\n')
case ch == '/' && i+1 < len(script) && script[i+1] == '*':
if strings.HasPrefix(script[i:], "/*!") || strings.HasPrefix(script[i:], "/*M!") {
return nil, fmt.Errorf("executable SQL comments are unsupported")
}
end := strings.Index(script[i+2:], "*/")
if end < 0 {
return nil, fmt.Errorf("unterminated SQL comment")
}
i += end + 3
current.WriteByte(' ')
case ch == ';':
if stmt := strings.TrimSpace(current.String()); stmt != "" {
statements = append(statements, stmt)
}
current.Reset()
default:
current.WriteByte(ch)
}
}
if quote != 0 {
return nil, fmt.Errorf("unterminated SQL quote")
}
if stmt := strings.TrimSpace(current.String()); stmt != "" {
statements = append(statements, stmt)
}
for _, stmt := range statements {
if strings.HasPrefix(strings.ToUpper(stmt), "DELIMITER ") {
return nil, fmt.Errorf("DELIMITER directives are unsupported")
}
}
return statements, nil
}
@@ -0,0 +1,70 @@
package models
import (
qfassets "git.mchus.pro/mchus/quoteforge"
"io/fs"
"reflect"
"strings"
"testing"
)
func TestSQLStatementParser(t *testing.T) {
for _, tt := range []struct {
name, sql string
want []string
fail bool
}{
{"empty", "-- comment;\n# more;", nil, false},
{"literal separators", "SELECT 'a;b', \"c;d\", `e;f`; SELECT 2;", []string{"SELECT 'a;b', \"c;d\", `e;f`", "SELECT 2"}, false},
{"doubled quotes", "SELECT 'a'';b', `a``;b`;", []string{"SELECT 'a'';b', `a``;b`"}, false},
{"backslash quote", "SELECT 'a\\';b'; SELECT 2", []string{"SELECT 'a\\';b'", "SELECT 2"}, false},
{"inline comments", "SELECT/* ; */ 1; -- end;\nSELECT 2# end;\n;", []string{"SELECT 1", "SELECT 2"}, false},
{"not a comment", "SELECT 4--2;", []string{"SELECT 4--2"}, false},
{"unclosed quote", "SELECT 'a;", nil, true},
{"unclosed block", "SELECT 1; /*", nil, true},
{"executable comment", "/*! SELECT 1 */", nil, true},
{"MariaDB comment", "/*M! SELECT 1 */", nil, true},
{"delimiter", "DELIMITER $$\nSELECT 1", nil, true},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := splitSQLStatements(tt.sql)
if (err != nil) != tt.fail {
t.Fatalf("error=%v", err)
}
if !tt.fail && !reflect.DeepEqual(got, tt.want) {
t.Fatalf("got=%q want=%q", got, tt.want)
}
})
}
long := "SELECT '" + strings.Repeat("x", 2<<20) + ";y';"
if got, err := splitSQLStatements(long); err != nil || len(got) != 1 {
t.Fatalf("large statement: count=%d err=%v", len(got), err)
}
}
func TestEmbeddedServerMigrationSQL(t *testing.T) {
migrations, err := qfassets.MigrationsFS()
if err != nil {
t.Fatal(err)
}
files, err := listSQLMigrationFiles(migrations)
if err != nil {
t.Fatal(err)
}
for _, name := range files {
t.Run(name, func(t *testing.T) {
content, err := fs.ReadFile(migrations, name)
if err != nil {
t.Fatal(err)
}
statements, err := splitSQLStatements(string(content))
if err != nil || len(statements) == 0 {
t.Fatalf("count=%d err=%v", len(statements), err)
}
checks, err := parseMigrationVerifications(string(content))
if err != nil || len(checks) == 0 {
t.Fatalf("checks=%d err=%v", len(checks), err)
}
})
}
}