164 lines
5.0 KiB
Go
164 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"pageswiki/internal/db"
|
|
"pageswiki/internal/metadata"
|
|
"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")
|
|
var errPostgreSQLNotReady = errors.New("core-only inspection cannot certify application migration; use migrate-postgres; no changes were applied")
|
|
|
|
func runSchema(args []string, out io.Writer) error {
|
|
backend, err := configuredDatabaseBackend()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if backend != "sqlite" {
|
|
return errors.New("legacy schema inspection requires SQLite; PostgreSQL startup validates native migrations and import receipt")
|
|
}
|
|
const usage = "pageswiki schema {status|check|portability} [--db FILE | --data-dir DIR] [--json] [--timeout 30s]\nRead-only inspection; no migration is performed.\ncheck exits 2 for pending migrations, 1 for incompatible state, 0 when managed components are current.\nportability reports PostgreSQL migration blockers and exits 1 while full application support is incomplete."
|
|
if len(args) == 0 || args[0] == "--help" {
|
|
_, err := fmt.Fprintln(out, usage)
|
|
return err
|
|
}
|
|
if args[0] != "status" && args[0] != "check" && args[0] != "portability" {
|
|
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()
|
|
if args[0] == "portability" {
|
|
repository, err := metadata.New(database, metadata.SQLite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
report, err := repository.InspectPortability(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if *jsonOutput {
|
|
encoder := json.NewEncoder(out)
|
|
encoder.SetIndent("", " ")
|
|
if err := encoder.Encode(report); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if _, err := fmt.Fprintf(out, "PostgreSQL application ready: %t\nPortable core rehearsal compatible: %t\nScope: %s\n", report.ApplicationReady, report.CoreCompatible, report.Scope); err != nil {
|
|
return err
|
|
}
|
|
for _, table := range report.Tables {
|
|
if _, err := fmt.Fprintf(out, "%-28s %-20s %s\n", table.Name, table.Component, table.State); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for _, blocker := range report.Blockers {
|
|
if _, err := fmt.Fprintln(out, "blocked:", blocker); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if !report.ApplicationReady {
|
|
if _, err := fmt.Fprintln(out, "This legacy diagnostic covers core rehearsal only. Use migrate-postgres for full application preflight and transfer."); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if !report.ApplicationReady {
|
|
return errPostgreSQLNotReady
|
|
}
|
|
return nil
|
|
}
|
|
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
|
|
}
|