Files
pages/internal/db/migration_inspect.go
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

140 lines
4.5 KiB
Go

package db
import (
"context"
"database/sql"
"fmt"
"sort"
)
type MigrationPlanStep struct {
Version int `json:"version"`
Name string `json:"name"`
Statements int `json:"statements"`
AddColumns []SQLiteColumn `json:"add_columns,omitempty"`
}
type MigrationInspection struct {
Component string `json:"component"`
State string `json:"state"` // current, pending, blocked, unknown
AppliedVersion int `json:"applied_version"`
LatestVersion int `json:"latest_version"`
Pending []MigrationPlanStep `json:"pending,omitempty"`
ContractChecked bool `json:"contract_checked"`
Issues []string `json:"issues,omitempty"`
}
// InspectSQLiteMigrations takes a read transaction and never applies DDL/DML.
// Pending definitions are a plan, not a successful rehearsal: data-dependent
// DDL and assertions on not-yet-created objects require a disposable DB copy.
func InspectSQLiteMigrations(ctx context.Context, database *sql.DB, plans map[string][]SQLMigration) ([]MigrationInspection, error) {
tx, err := database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
var ledger bool
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='schema_migration_steps')`).Scan(&ledger); err != nil {
return nil, err
}
type record struct {
version int
name, checksum string
}
history := map[string][]record{}
if ledger {
rows, err := tx.QueryContext(ctx, `SELECT component,version,name,checksum FROM schema_migration_steps ORDER BY component,version`)
if err != nil {
return nil, err
}
for rows.Next() {
var component string
var r record
if err := rows.Scan(&component, &r.version, &r.name, &r.checksum); err != nil {
rows.Close()
return nil, err
}
history[component] = append(history[component], r)
}
if err := rows.Err(); err != nil {
rows.Close()
return nil, err
}
if err := rows.Close(); err != nil {
return nil, err
}
}
names := make([]string, 0, len(plans)+len(history))
for name := range plans {
names = append(names, name)
}
for name := range history {
if _, ok := plans[name]; !ok {
names = append(names, name)
}
}
sort.Strings(names)
result := make([]MigrationInspection, 0, len(names))
for _, name := range names {
if err := ctx.Err(); err != nil {
return nil, err
}
steps, known := plans[name]
item := MigrationInspection{Component: name, State: "current", LatestVersion: len(steps)}
for i, step := range steps {
if step.Version != i+1 || step.Name == "" || (len(step.Statements) == 0 && len(step.AddColumns) == 0 && len(step.Assertions) == 0) {
return nil, fmt.Errorf("invalid migration definition %s/%d", name, step.Version)
}
}
if !known {
item.State = "unknown"
item.Issues = []string{"component is not registered in this binary"}
}
for i, record := range history[name] {
item.AppliedVersion = record.version
if known && (record.version != i+1 || record.version > len(steps) || record.name != steps[record.version-1].Name || record.checksum != migrationChecksum(steps[record.version-1])) {
item.State = "blocked"
item.Issues = append(item.Issues, fmt.Sprintf("migration history mismatch at version %d", record.version))
}
}
if item.State == "current" {
for _, step := range steps[len(history[name]):] {
item.Pending = append(item.Pending, MigrationPlanStep{Version: step.Version, Name: step.Name, Statements: len(step.Statements), AddColumns: step.AddColumns})
}
if len(item.Pending) > 0 {
item.State = "pending"
}
if len(steps) > 0 && len(steps[len(steps)-1].Assertions) > 0 {
item.ContractChecked = true
for i, assertion := range steps[len(steps)-1].Assertions {
rows, err := tx.QueryContext(ctx, assertion)
if err != nil {
item.Issues = append(item.Issues, fmt.Sprintf("assertion %d: %v", i+1, err))
continue
}
violated := rows.Next()
err = rows.Err()
closeErr := rows.Close()
if err != nil {
return nil, err
}
if closeErr != nil {
return nil, closeErr
}
if violated {
item.Issues = append(item.Issues, fmt.Sprintf("assertion %d failed", i+1))
}
}
if len(item.Issues) > 0 && item.State == "current" {
item.State = "blocked"
}
}
}
result = append(result, item)
}
if err := ctx.Err(); err != nil {
return nil, err
}
return result, tx.Commit()
}