package main import ( "context" "encoding/json" "errors" "flag" "fmt" "io" "os" "path/filepath" "time" "pageswiki/internal/db" "pageswiki/internal/store" ) var errSchemaPending = errors.New("schema migrations are pending; no changes were applied") var errSchemaBlocked = errors.New("schema history or contract is incompatible; no changes were applied") func runSchema(args []string, out io.Writer) error { const usage = "pageswiki schema {status|check} [--db FILE | --data-dir DIR] [--json] [--timeout 30s]\nRead-only inspection of managed components; legacy migrations and pending DDL are not rehearsed.\ncheck exits 2 for pending migrations, 1 for incompatible state, 0 when managed components are current." if len(args) == 0 || args[0] == "--help" { _, err := fmt.Fprintln(out, usage) return err } if args[0] != "status" && args[0] != "check" { return errors.New(usage) } f := flag.NewFlagSet("schema "+args[0], flag.ContinueOnError) f.SetOutput(out) defaultDir := os.Getenv("PAGESWIKI_DATA_DIR") if defaultDir == "" { defaultDir = "./data" } dir := f.String("data-dir", defaultDir, "instance data directory (not created)") path := f.String("db", "", "explicit existing SQLite database path") jsonOutput := f.Bool("json", false, "JSON report") timeout := f.Duration("timeout", 30*time.Second, "inspection timeout") if err := f.Parse(args[1:]); err != nil { if errors.Is(err, flag.ErrHelp) { return nil } return err } if f.NArg() != 0 || *timeout <= 0 { return errors.New("unexpected arguments or invalid timeout") } explicitDir := false f.Visit(func(flag *flag.Flag) { if flag.Name == "data-dir" { explicitDir = true } }) if *path != "" && explicitDir { return errors.New("choose --db or --data-dir, not both") } if *path == "" { *path = filepath.Join(*dir, "pages.db") } database, err := db.OpenReadOnly(*path) if err != nil { return err } defer database.Close() ctx, cancel := context.WithTimeout(context.Background(), *timeout) defer cancel() report, err := store.InspectSchema(ctx, database) if err != nil { return err } if *jsonOutput { encoder := json.NewEncoder(out) encoder.SetIndent("", " ") err = encoder.Encode(report) } else { _, err = fmt.Fprintf(out, "Coverage: %s (not a full startup or data migration rehearsal)\n", report.Coverage) for _, component := range report.Components { if err != nil { break } _, err = fmt.Fprintf(out, "%-24s %-8s %d -> %d\n", component.Component, component.State, component.AppliedVersion, component.LatestVersion) for _, step := range component.Pending { if err != nil { break } _, err = fmt.Fprintf(out, " pending v%d %s (%d SQL statements, %d conditional columns)\n", step.Version, step.Name, step.Statements, len(step.AddColumns)) } for _, issue := range component.Issues { if err != nil { break } _, err = fmt.Fprintf(out, " %s\n", issue) } } if err == nil { _, err = fmt.Fprintf(out, "Legacy areas not covered: %v\n", report.LegacyAreas) } } if err != nil || args[0] == "status" { return err } pending := false for _, component := range report.Components { if component.State == "blocked" || component.State == "unknown" { return errSchemaBlocked } pending = pending || component.State == "pending" } if pending { return errSchemaPending } return nil }