fix(schema): distinguish optional ML budget installation from corruption

This commit is contained in:
Anya Tokugawa committed 2026-12-01 07:45:22 +09:00
1 parent 367bfa3a2a
commit 06b8f75c15
4 files changed
+64 -1

No files matched your search

@@ -13,6 +13,7 @@
- MySQL 8.4向けにML予算、Campus/Domain、ユーザー/グループ、認証情報保存の限定実装を追加。 - MySQL 8.4向けにML予算、Campus/Domain、ユーザー/グループ、認証情報保存の限定実装を追加。
schemaのstatus/check/apply/resumeは明示CLIのみ。本体のMySQL起動は未対応。 schemaのstatus/check/apply/resumeは明示CLIのみ。本体のMySQL起動は未対応。
- Pages Machine Learningは任意の独立サービス。通常の配備には含めず、自動起動しない。 - Pages Machine Learningは任意の独立サービス。通常の配備には含めず、自動起動しない。
- DB検査では未導入のML予算schemaを任意機能として扱う。部分導入・導入後のテーブル欠落は引き続き異常として検出する。
- ファイル検査のpolicy/plugin実行基盤は未接続。今回の更新で検査済み保証を追加しない。 - ファイル検査のpolicy/plugin実行基盤は未接続。今回の更新で検査済み保証を追加しない。
- CLIスキル初版、展示マニフェスト・合成データ・紹介資料を保存。展示クラスタは配備対象外。 - CLIスキル初版、展示マニフェスト・合成データ・紹介資料を保存。展示クラスタは配備対象外。
+20
View File
@@ -28,6 +28,13 @@ type MigrationInspection struct {
// Pending definitions are a plan, not a successful rehearsal: data-dependent // Pending definitions are a plan, not a successful rehearsal: data-dependent
// DDL and assertions on not-yet-created objects require a disposable DB copy. // 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) { 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}) tx, err := database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil { if err != nil {
return nil, err return nil, err
@@ -80,6 +87,19 @@ func InspectSQLiteMigrations(ctx context.Context, database *sql.DB, plans map[st
return nil, err return nil, err
} }
steps, known := plans[name] 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)} item := MigrationInspection{Component: name, State: "current", LatestVersion: len(steps)}
for i, step := range 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) { if step.Version != i+1 || step.Name == "" || (len(step.Statements) == 0 && len(step.AddColumns) == 0 && len(step.Assertions) == 0) {
+3 -1
View File
@@ -21,7 +21,9 @@ func InspectSchema(ctx context.Context, database *sql.DB) (SchemaInspection, err
plans[name] = steps plans[name] = steps
} }
plans["document-store"] = documentBackfillMigrations 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{ return SchemaInspection{Coverage: "managed-components-only", Components: components, LegacyAreas: []string{
"core-bootstrap", "campuses/domains/federation", "auth/ACL/user-space", "outbox/notifications/publication-approval", "core-bootstrap", "campuses/domains/federation", "auth/ACL/user-space", "outbox/notifications/publication-approval",
"City", "tasks/calendars", "periodicals", "search/object-listing/assets/GC", "City", "tasks/calendars", "periodicals", "search/object-listing/assets/GC",
+40
View File
@@ -7,6 +7,46 @@ import (
"pageswiki/internal/db" "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) { func TestSchemaInspectionDoesNotRecoverJobsOrBackfill(t *testing.T) {
st := newSchemaTestStore(t) st := newSchemaTestStore(t)
for name := range managedStoreSchemas { for name := range managedStoreSchemas {