refactor: make migration process more intelligent, add test coverage

This commit is contained in:
2026-09-19 21:36:31 -07:00
parent ff7f0a57c8
commit 40bc0db036
6 changed files with 361 additions and 32 deletions

42
pkg/db/pragma_fk_check.go Normal file
View File

@@ -0,0 +1,42 @@
package db
import (
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
)
// PragmaFKViolation is a result row of SQLite's `pragma foreign_key_check`.
type PragmaFKViolation struct {
SrcTable string `db:"table"`
SrcRowid int `db:"rowid"`
DstTable string `db:"parent"`
ForeignKeyIdx int `db:"fkid"`
}
// PragmaFKViolations is a slice of FK violations. It also implements the `error` interface.
type PragmaFKViolations []PragmaFKViolation
func (v PragmaFKViolations) Error() string {
var b strings.Builder
for i, fkv := range v {
if i > 0 {
b.WriteString("\n")
}
fmt.Fprintf(&b, "Item #%d in table %q has invalid reference to table %q", fkv.SrcRowid, fkv.SrcTable, fkv.DstTable)
}
return b.String()
}
// PragmaForeignKeyCheck uses `pragma foreign_key_check` to do a full database check for any FK violations.
func PragmaForeignKeyCheck(q sqlx.Queryer) error {
var ret PragmaFKViolations
must.Do(sqlx.Select(q, &ret, `pragma foreign_key_check`))
if len(ret) == 0 {
return nil
}
return ret
}