plan

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package plan defines the machine-readable dry-run plan report: the stable JSON contract an operator or orchestrator consumes to decide whether and how a change would execute. Both front doors emit it — the imperative migrate --dry-run path and the declarative diff path — so a consumer parses one shape regardless of how the plan was derived.

Index

Constants

View Source
const FormatVersion = 19

FormatVersion identifies the report contract. A consumer must reject a report whose version it does not understand instead of guessing at the field semantics. Version 2 added the guidance field on rewrite-required statements. Version 3 added the cause field on greenfield statements the create path refuses by shape. Version 4 added the class and owner fields on the report and on refused statements, and closed their vocabularies. Version 5 added blocking_passthrough_eligible to refused statements. Version 6 added the add-foreign-key and drop-foreign-key change kinds and the foreign-key create-shape cause. Version 7 added the set-generated-expression change kind. Version 8 added the replica-identity planner reason. Version 9 added the validate-constraint and validate-foreign-key change kinds. Version 10 added the dependent-view planner reason and the dependent_views decision field. Version 11 added the cascading-drop planner reason. Version 12 routes an app-breaking rename to the rewrite-required disposition and added the expand-contract-column and coordinated-table-rename guidance codes. Version 13 classifies the rename of an index that backs a constraint as app-breaking-rename and added the coordinated-constraint-rename guidance code. Version 14 added the alter-column-type-in-place guidance code, which a destructive-change refusal verdict carries; no report emits it, but the report embeds the whole guidance vocabulary. Version 15 added the report-level advisories field and its closed kind vocabulary for the invalid indexes a diff observes and declines to drop. Version 16 added the dependent-policy planner reason and the dependent_policies decision field. Version 17 added the not-null-without-default planner reason. Version 18 added the dependent-trigger, dependent-generated-column, and partition-key planner reasons and the dependent_triggers, dependent_generated_columns, and partition_key_tables decision fields.

Variables

This section is empty.

Functions

func CreateShapeRefusal added in v0.3.3

func CreateShapeRefusal(cause executor.CreateShapeCause) (verdict.Refusal, bool)

CreateShapeRefusal classifies the create path's shape refusal of a statement in a greenfield plan. The closed key set is executor.CreateShapeCauses(); ok is false for a cause outside it, which callers treat as an invariant violation rather than classify.

func DestructiveChangeRefusal added in v0.4.0

func DestructiveChangeRefusal() verdict.Refusal

DestructiveChangeRefusal classifies a change that would discard live data or structure: desired-state admission of a destructive plan, and an imperative destructive statement without the destructive acknowledgement. Both front doors share it, so they report one contract.

func DiscloseGreenfieldExecution added in v0.3.0

func DiscloseGreenfieldExecution(report *Report)

DiscloseGreenfieldExecution makes executable statements describe the plain, bounded builds used for a table born in this run. The report must describe a table that does not exist yet; the function returns without mutation unless the report establishes that precondition. Each create step commits in its own transaction under the brief lock_timeout and statement_timeout budget, so CREATE TABLE is visible before its indexes build and a concurrent writer that already knows the name makes the step fail fast rather than block. The build is classified metadata-only because its cost is bounded by that budget on a table born in the run. Reclassifying keeps the statement in a state router.Route can produce: an execute disposition never carries a safer-idiom decision without its rewrite.

func Fingerprint

func Fingerprint(statements []Statement, extensionSchemas ...string) string

Fingerprint computes the plan's stable identity: "sha256:" plus the hex digest over what would execute — each statement's canonical SQL, route, backend, disposition, and exec_sql, in plan order. Explanatory fields (decisions, kind, destructive, reason, cause, guidance) are excluded, so a reworded reason does not change identity but a rerouted or resequenced plan does. The exact serialization is part of the contract (docs/plan-report.md) and changes only with a format_version bump. This is a plan identity, not a schema fingerprint: it never participates in schema-state comparison.

extensionSchemas are the report's ExtensionSchemas. When there are any, a group separator and each schema follow the statements, so a plan derived without them keeps the identity it always had.

func ForeignKeyChangeRefusal added in v0.4.0

func ForeignKeyChangeRefusal() verdict.Refusal

ForeignKeyChangeRefusal classifies a desired-state foreign-key change: declarative foreign-key execution is a planned capability. The plan keeps the statement, so the diff stays visible while execution refuses it.

func PartitionRefusal added in v0.3.3

func PartitionRefusal(cause preflight.PartitionRefusalCause) (verdict.Refusal, bool)

PartitionRefusal classifies a partitioned-parent refusal by its cause. The closed key set is preflight.PartitionRefusalCauses(); ok is false outside it. The four causes span three classes, which is why the plan carries the cause rather than a bare refused flag. The refusal keeps its typed cause, so every consumer of the proof — both front doors and the accepted-blocking eligibility registry — reads one narrowing instead of re-deriving it from the preflight error.

func RefuseDestructive added in v0.4.0

func RefuseDestructive(report *Report)

RefuseDestructive marks the whole desired-state preview non-executable when any step is destructive. It preserves existing refusals, withdraws execution metadata, and fingerprints the resulting plan. Execution still enforces its own admission.

func RefuseForeignKeyChanges added in v0.4.0

func RefuseForeignKeyChanges(report *Report)

RefuseForeignKeyChanges marks every statement that adds or drops a foreign key as refused. Desired files may declare foreign keys so a plan can show whether the live table matches them, but desired-state execution does not run foreign-key DDL yet: the plan reports the change and nothing executes it. The rest of the plan keeps its routing; admission is all-or-nothing, so one refused statement keeps the whole plan from running.

func RefuseUnsupportedCreateShape added in v0.3.0

func RefuseUnsupportedCreateShape(report *Report, refused []error) error

RefuseUnsupportedCreateShape marks the create-path statements whose connection-free shape checks refuse them and stamps each one with the executor's typed cause. refused is positional over report.Statements — one entry per planned statement, nil where the statement is admitted. A length mismatch means the two sides no longer agree on what the plan contains, so no positional marking is safe and the report is left untouched. A refusal the cause vocabulary does not name is a contract violation: the report would carry a refusal it cannot explain, so it fails closed before any statement is marked.

func RefuseUnsupportedPartitionedParent

func RefuseUnsupportedPartitionedParent(report *Report, causes []preflight.PartitionRefusalCause) error

RefuseUnsupportedPartitionedParent marks statements as refused when partition-aware admission rejects their execution steps. causes is positional over report.Statements — one entry per planned statement, empty where the statement is admitted — because the class of the refusal is a property of the cause (docs/refusal-classes.md), not of the reason. A length mismatch, or a cause the classification registry does not name, is a contract violation: the report would carry a refusal it cannot explain, so it fails closed before any statement is marked.

func RewriteRequiredGuidance added in v0.4.0

func RewriteRequiredGuidance(rs router.Statement) (suggest.Guidance, error)

RewriteRequiredGuidance derives the typed manual path for a statement the router marked rewrite-required, for the plan report and the run verdict alike: the first decision that is not executable as submitted — a safer-idiom decision or an app-breaking rename — names the operation the manual path is for. A multi-operation statement always advises splitting first (ManualGuidance's contract), which covers the case where the rewrite exists but could not be applied to a compound statement. The operation list and decision list are index-aligned by the planner's contract (one decision per operation, in order); a mismatch, or a rewrite-required statement whose every decision is executable as submitted, is a contract violation and fails closed.

func RouteRefusal added in v0.3.3

func RouteRefusal() verdict.Refusal

RouteRefusal classifies a routed statement the planner has no safe path for: an admitted ALTER TABLE operation with no route. The key is the site.

func RowSecurityMissingTableRefusal added in v0.4.0

func RowSecurityMissingTableRefusal() verdict.Refusal

RowSecurityMissingTableRefusal classifies an absent RLS target in preview and apply. The operator must create the table before managing its policies.

func RowSecurityReviewRefusal added in v0.4.0

func RowSecurityReviewRefusal() verdict.Refusal

RowSecurityReviewRefusal keeps policy deltas review-only in diff output.

Types

type Report

type Report struct {
	// FormatVersion is the report contract version; always FormatVersion.
	FormatVersion int `json:"format_version"`
	// Source is the front door that derived the plan.
	Source Source `json:"source"`
	// Schema is the target schema; empty when the submitted statement did
	// not qualify one.
	Schema string `json:"schema,omitempty"`
	// Table is the target table; empty when the statement has no single
	// table target (index maintenance).
	Table string `json:"table,omitempty"`
	// ServerVersion is the PostgreSQL server_version the plan was derived
	// against. Classification is version-sensitive, so a stored or
	// forwarded report names the server whose rules produced it; empty
	// only for sources that never connected.
	ServerVersion string `json:"server_version,omitempty"`
	// TableExists reports whether the live table was found. Set by every
	// source that introspects the target (diff, and the alter dry run);
	// nil means the plan has no single table target to introspect. For
	// diff, false means the statements are the full desired schema; for
	// an alter dry run, false means the plan was classified from zero
	// facts and executing it would fail.
	TableExists *bool `json:"table_exists,omitempty"`
	// Disposition is the aggregate disposition across all statements:
	// what would happen if the engine executed this plan now.
	Disposition router.Disposition `json:"disposition"`
	// Reason is the typed refusal cause when target facts make an otherwise
	// executable routed plan unsafe.
	Reason verdict.Reason `json:"reason,omitempty"`
	// Class identifies how a consumer routes an aggregate refusal.
	Class verdict.Class `json:"class,omitempty"`
	// Owner identifies who owns aggregate work with no online-safety problem.
	Owner verdict.Owner `json:"owner,omitempty"`
	// Fingerprint is the plan's stable identity (see ComputeFingerprint). An
	// approver pins it when the plan is reviewed; an executor recomputes it
	// at apply time and refuses on mismatch — that is how "the plan a
	// reviewer approves is the plan that executes" is enforced across
	// storage and forwarding.
	Fingerprint string `json:"fingerprint"`
	// ExtensionSchemas are the schemas the desired file's unqualified names
	// also resolved against (statement.DesiredSchema.WithExtensionSchemas),
	// in the order they were searched. A table that does not exist yet is
	// created from the file's own statements under that search path, so the
	// fingerprint covers them: the same SQL resolved against other schemas
	// can bind other types and functions. Empty when the file named none.
	ExtensionSchemas []string `json:"extension_schemas,omitempty"`
	// Statements is the ordered plan; empty means there is nothing to do.
	Statements []Statement `json:"statements"`
	// Advisories are the typed observations the diff made about the live
	// table that produced no statement (schemadiff.Advisory): the invalid
	// indexes it saw and declined to drop. Present only on a diff-source
	// plan whose table exists, and only when there is something to report.
	// Advisory, not executable — the change list and the destructive gate
	// are unaffected, and an empty Statements with an advisory present
	// means the table is not clean even though there is nothing to run.
	// Explanatory, so excluded from the fingerprint.
	Advisories []schemadiff.Advisory `json:"advisories,omitempty"`
}

Report is the dry-run plan for one change against one table.

func NewReport

func NewReport(source Source) Report

NewReport returns an empty report for source with the contract version stamped and Statements non-nil, so an empty plan serializes as [] rather than null.

func (Report) ComputeFingerprint added in v0.5.1

func (r Report) ComputeFingerprint() string

ComputeFingerprint returns the Fingerprint of everything in r that is part of the plan's identity: its statements and its extension schemas. Every path that builds a report sets r.Fingerprint from this method, so a field added to the identity cannot be left out at one call site.

type Source

type Source string

Source identifies which front door derived the plan.

const (
	// SourceAlter marks a plan derived from a submitted DDL statement
	// (migrate --alter --dry-run): the classify-and-route pipeline with
	// the diff step skipped.
	SourceAlter Source = "alter"
	// SourceDiff marks a plan derived from a desired-state schema diff
	// (diff --desired): the ordered statements that converge the live
	// table on the desired schema.
	SourceDiff Source = "diff"
)

func Sources

func Sources() []Source

Sources returns the closed set of Source values. It is part of the plan-report contract (docs/plan-report.md): the set changes only with a format_version bump, and a consumer that meets an unrecognized value must treat the report as unknown and refuse it.

type Statement

type Statement struct {
	// SQL is the statement in the engine's canonical rendering: parsed and
	// reprinted through the PostgreSQL deparser, whichever front door
	// derived it. It is never a verbatim echo of the submitted text, so
	// the same change carries the same string through either door.
	SQL string `json:"sql"`
	// Kind classifies a diff-derived statement so a consumer can gate
	// whole classes of change (see schemadiff.ChangeKind). Empty for the
	// alter source: a submitted statement may carry several operations and
	// has no single kind.
	Kind schemadiff.ChangeKind `json:"kind,omitempty"`
	// Adopts is the index a diff-derived ADD CONSTRAINT ... USING INDEX
	// takes over and the definition it must still have when execution
	// attaches it (see schemadiff.Change.Adopts). Nil otherwise, and out
	// of the JSON: the SQL already identifies the change.
	Adopts *schemadiff.IndexAdoption `json:"-"`
	// Validates is the constraint a diff-derived VALIDATE CONSTRAINT
	// validates and the definition it must still have when execution
	// validates it (see schemadiff.Change.Validates). Nil otherwise, and
	// out of the JSON for the same reason.
	Validates *schemadiff.ConstraintValidation `json:"-"`
	// Destructive marks statements that discard live structure — a dropped
	// column, constraint, index, or NOT NULL. It is derived from the classifier's
	// decisions, so both sources report it identically; it is always
	// emitted, never omitted, because a safety flag a consumer gates on
	// must be explicit even when false.
	Destructive bool `json:"destructive"`
	// Route is the planner's aggregate route for the statement.
	Route planner.Route `json:"route"`
	// Backend is the assigned execution strategy; empty for refusals.
	Backend router.Backend `json:"backend,omitempty"`
	// Disposition is what execution would do with the statement now.
	Disposition router.Disposition `json:"disposition"`
	// Reason is the typed cause when target facts refuse this statement.
	Reason verdict.Reason `json:"reason,omitempty"`
	// Class identifies how a consumer routes a refused statement.
	Class verdict.Class `json:"class,omitempty"`
	// Owner identifies who owns work with no online-safety problem.
	Owner verdict.Owner `json:"owner,omitempty"`
	// BlockingPassthroughEligible reports whether an operator may request the
	// bounded blocking path for this refusal. Present exactly on refusals.
	BlockingPassthroughEligible *bool `json:"blocking_passthrough_eligible,omitempty"`
	// Cause is the create path's typed shape refusal for a statement of a
	// greenfield plan (executor.CreateShapeCause): why a table born in the
	// run cannot carry this statement. Present exactly when the create path
	// refused the statement by shape — on a diff-source plan whose table
	// does not exist, that is every statement with Disposition refuse and
	// Reason unsupported-statement. Absent for every other refusal,
	// including an alter-source refusal against an absent table. Stored
	// rather than derived because a JSON consumer has no desired schema to
	// recompute the shape check from; it is the executor's own explanation,
	// so a renderer prints Description() instead. Explanatory, so it is
	// excluded from the fingerprint. Route on Class, not Cause.Class(): Class
	// is the producer's answer, while Cause.Class() is the reading library's
	// and answers "" for a cause added in a newer engine.
	Cause executor.CreateShapeCause `json:"cause,omitempty"`
	// Decisions are the planner's per-operation classifications.
	Decisions []planner.Decision `json:"decisions"`
	// ExecSQL is the ordered SQL the native backend would run — the safer
	// sequence when the planner constructed one. Empty for non-native
	// routes.
	ExecSQL []string `json:"exec_sql,omitempty"`
	// Execution is the typed execution contract for ExecSQL
	// (planner.Execution), present exactly when ExecSQL is. A consumer
	// that runs the statements itself branches on it — it is what says
	// each step runs in its own implicit transaction, never inside an
	// enclosing transaction block. It is derived from ExecSQL's presence,
	// so it is excluded from the fingerprint like the other explanatory
	// fields.
	Execution planner.Execution `json:"execution,omitempty"`
	// Guidance is the typed manual path for a rewrite-required refusal,
	// drawn from the suggest contract's Guidance vocabulary
	// (docs/suggest-report.md): the engine will not run the statement, and
	// this names what to do instead. Present exactly when Disposition is
	// rewrite-required. Explanatory, so it is excluded from the
	// fingerprint.
	Guidance suggest.Guidance `json:"guidance,omitempty"`
}

Statement is one planned statement: the SQL, its classification, and what execution would do with it now.

func FromRouted

func FromRouted(rs router.Statement) (Statement, error)

FromRouted converts one routed statement into a plan statement. Destructive is derived from the classifier's decisions — one destructive operation makes the statement destructive — so every source that routes through the planner reports it identically by construction. A rewrite-required statement additionally carries the typed manual path (Guidance), derived through the same mapping the suggest report uses; a rewrite-required decision with no known guidance is a contract violation and fails closed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL