From cbcb8a24b43a15a6386b00fdbef70e3e6f5aa4f5 Mon Sep 17 00:00:00 2001 From: Anya Tokugawa Date: Tue, 1 Dec 2026 07:45:22 +0900 Subject: [PATCH] fix(schema): distinguish optional ML budget installation from corruption --- .../operations/releases/2026-12-01-v0.19.0.md | 1 + internal/db/migration_inspect.go | 20 ++++++++++ internal/store/schema_inspect.go | 4 +- internal/store/schema_inspect_test.go | 40 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/docs/operations/releases/2026-12-01-v0.19.0.md b/docs/operations/releases/2026-12-01-v0.19.0.md index 098cc4a..4af0546 100644 --- a/docs/operations/releases/2026-12-01-v0.19.0.md +++ b/docs/operations/releases/2026-12-01-v0.19.0.md @@ -13,6 +13,7 @@ - MySQL 8.4向けにML予算、Campus/Domain、ユーザー/グループ、認証情報保存の限定実装を追加。 schemaのstatus/check/apply/resumeは明示CLIのみ。本体のMySQL起動は未対応。 - Pages Machine Learningは任意の独立サービス。通常の配備には含めず、自動起動しない。 +- DB検査では未導入のML予算schemaを任意機能として扱う。部分導入・導入後のテーブル欠落は引き続き異常として検出する。 - ファイル検査のpolicy/plugin実行基盤は未接続。今回の更新で検査済み保証を追加しない。 - CLIスキル初版、展示マニフェスト・合成データ・紹介資料を保存。展示クラスタは配備対象外。 diff --git a/internal/db/migration_inspect.go b/internal/db/migration_inspect.go index b5f9af6..9012e49 100644 --- a/internal/db/migration_inspect.go +++ b/internal/db/migration_inspect.go @@ -28,6 +28,13 @@ type MigrationInspection struct { // 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) { + return InspectSQLiteMigrationsWithOptional(ctx, database, plans, nil) +} + +// Optional components are omitted only when neither a migration record nor any +// owned schema object exists. Detection and inspection share one read snapshot; +// partial installation or a missing table after installation is never ignored. +func InspectSQLiteMigrationsWithOptional(ctx context.Context, database *sql.DB, plans map[string][]SQLMigration, optional map[string][]string) ([]MigrationInspection, error) { tx, err := database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) if err != nil { return nil, err @@ -80,6 +87,19 @@ func InspectSQLiteMigrations(ctx context.Context, database *sql.DB, plans map[st return nil, err } steps, known := plans[name] + if objects, opt := optional[name]; opt && len(history[name]) == 0 && len(objects) > 0 { + present := false + for _, object := range objects { + var exists bool + if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name=?)`, object).Scan(&exists); err != nil { + return nil, err + } + present = present || exists + } + if !present { + continue + } + } 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) { diff --git a/internal/store/schema_inspect.go b/internal/store/schema_inspect.go index 21a20ce..5963c8e 100644 --- a/internal/store/schema_inspect.go +++ b/internal/store/schema_inspect.go @@ -21,7 +21,9 @@ func InspectSchema(ctx context.Context, database *sql.DB) (SchemaInspection, err plans[name] = steps } plans["document-store"] = documentBackfillMigrations - components, err := db.InspectSQLiteMigrations(ctx, database, plans) + components, err := db.InspectSQLiteMigrationsWithOptional(ctx, database, plans, map[string][]string{ + "machine-learning-budget": {"ml_daily_budget", "ml_budget_receipts"}, + }) return SchemaInspection{Coverage: "managed-components-only", Components: components, LegacyAreas: []string{ "core-bootstrap", "campuses/domains/federation", "auth/ACL/user-space", "outbox/notifications/publication-approval", "City", "tasks/calendars", "periodicals", "search/object-listing/assets/GC", diff --git a/internal/store/schema_inspect_test.go b/internal/store/schema_inspect_test.go index 1304a38..6c98ffc 100644 --- a/internal/store/schema_inspect_test.go +++ b/internal/store/schema_inspect_test.go @@ -7,6 +7,46 @@ import ( "pageswiki/internal/db" ) +func TestSchemaInspectionOptionalMLBudget(t *testing.T) { + ctx := context.Background() + for _, mode := range []string{"absent", "partial", "installed", "missing-tables"} { + t.Run(mode, func(t *testing.T) { + s := newSchemaTestStore(t) + if mode == "partial" { + if _, err := s.DB.Exec(mlBudgetStatements[0]); err != nil { + t.Fatal(err) + } + } else if mode != "absent" { + if err := s.EnsureMLBudgetSchema(ctx); err != nil { + t.Fatal(err) + } + if mode == "missing-tables" { + if _, err := s.DB.Exec(`DROP TABLE ml_daily_budget; DROP TABLE ml_budget_receipts`); err != nil { + t.Fatal(err) + } + } + } + report, err := InspectSchema(ctx, s.DB) + if err != nil { + t.Fatal(err) + } + found := false + for _, component := range report.Components { + if component.Component != "machine-learning-budget" { + continue + } + found = true + if (component.State == "current") != (mode == "installed") { + t.Fatalf("%s: %+v", mode, component) + } + } + if found != (mode != "absent") { + t.Fatalf("optional component visibility: %s %v", mode, found) + } + }) + } +} + func TestSchemaInspectionDoesNotRecoverJobsOrBackfill(t *testing.T) { st := newSchemaTestStore(t) for name := range managedStoreSchemas {