dataplane

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package dataplane defines the narrow machine-readable contracts consumed by the controller. It deliberately does not import the full Ptah module.

Index

Constants

View Source
const (
	PlanFormatVersion     = 1
	SchemaArtifactType    = "application/vnd.stokaro.ptah.schema.v1"
	MigrationArtifactType = "application/vnd.stokaro.ptah.migrations.v1"

	// DriftFindingVocabularyVersion identifies the closed category vocabulary
	// accepted from native drift reports. Extending this vocabulary is a runner
	// protocol change: an older controller must reject, rather than publish, a
	// category whose disclosure contract it does not understand.
	//
	// Nothing reads this number. The refusal it describes is delivered by the
	// vocabulary itself, at both ends: a controller decoding a report and a
	// controller parsing a result frame each ask IsKnownDriftFindingCategory,
	// and an unknown category is refused there rather than compared here.
	DriftFindingVocabularyVersion = 1
)
View Source
const (
	// SupportedMigrationStatusContract and SupportedMigrationRunContract are the
	// Ptah machine-contract versions this operator reads.
	//
	// A document announcing another version is refused rather than read: the
	// fields this operator knows may still be present and may already mean
	// something else, and a history read under the wrong contract is a history
	// nobody checked.
	SupportedMigrationStatusContract = 1
	SupportedMigrationRunContract    = 1

	// Migration states a status document reports per migration.
	MigrationStateApplied           = "applied"
	MigrationStateModified          = "modified"
	MigrationStateDirty             = "dirty"
	MigrationStatePending           = "pending"
	MigrationStateOutOfOrder        = "out-of-order"
	MigrationStateCheckpointCovered = "checkpoint-covered"
)
View Source
const (
	MigrationOutcomeUpToDate = "up-to-date"
	MigrationOutcomeApplied  = "applied"
	MigrationOutcomeDryRun   = "dry-run"
	MigrationOutcomeFailed   = "failed"
	MigrationOutcomePartial  = "partial"
	MigrationOutcomeUnknown  = "unknown"
)

Outcomes a run document reports.

View Source
const (
	// PrivilegeGrant is a privilege granted on an object, default privileges
	// included.
	PrivilegeGrant = "Grant"
	// PrivilegeRevoke is a privilege revoked from an object.
	PrivilegeRevoke = "Revoke"
	// PrivilegeRoleMembership is a role granted to, or revoked from, another
	// role or user.
	PrivilegeRoleMembership = "RoleMembership"
	// PrivilegeRole is a role, user or group created, altered or dropped -- its
	// attributes, its settings, its existence -- or assumed for the rest of the
	// session with SET ROLE or SET SESSION AUTHORIZATION.
	PrivilegeRole = "Role"
	// PrivilegeOwnership is an object given to another owner.
	PrivilegeOwnership = "Ownership"
	// PrivilegeRowSecurityPolicy is a row-security policy created, altered or
	// dropped, or row security switched off or unforced on a table.
	PrivilegeRowSecurityPolicy = "RowSecurityPolicy"
	// PrivilegeSecurityDefiner is code set to run with its owner's rights
	// rather than its caller's. A MySQL or MariaDB routine does that unless it
	// says SQL SECURITY INVOKER, so a routine that names no mode counts.
	PrivilegeSecurityDefiner = "SecurityDefiner"
	// PrivilegeDefiner is a MySQL DEFINER clause, which names the account a
	// view, routine, trigger or event runs as.
	PrivilegeDefiner = "Definer"
	// PrivilegeFunctionReplacement is CREATE OR REPLACE of a function or a
	// procedure, which can rewrite code that policies, triggers and other
	// roles already call.
	PrivilegeFunctionReplacement = "FunctionReplacement"
)

The kinds of authority a plan statement can change. Each names a kind of change and never its object, its grantee or its text: the kinds are copied into the plan and into status, where a role name or a function body does not belong.

Extending this list is an API change: the CRD enumerates the same values, and a stored plan that names a kind the schema does not list stops validating.

View Source
const (
	// SupportedSchemaPlanContract and SupportedSchemaApplyContract are the
	// versions of the documents `ptah schema plan --json` and `ptah schema
	// apply --json` print that this operator reads.
	//
	// Ptah raises a version when a field changes meaning or leaves, and when an
	// outcome gains a value. A document announcing another version is refused
	// rather than read for the fields this operator recognizes, because those
	// fields may already mean something else.
	SupportedSchemaPlanContract  = 1
	SupportedSchemaApplyContract = 1
)
View Source
const (
	SchemaPlanOutcomeChanges   = "changes"
	SchemaPlanOutcomeNoChanges = "no-changes"
	SchemaPlanOutcomeRefused   = "refused"
	SchemaPlanOutcomeFailed    = "failed"
)

Outcomes a plan document reports.

View Source
const (
	SchemaApplyOutcomeApplied   = "applied"
	SchemaApplyOutcomeNoChanges = "no-changes"
	SchemaApplyOutcomeDryRun    = "dry-run"
	SchemaApplyOutcomeCanceled  = "canceled"
	SchemaApplyOutcomeRefused   = "refused"
	SchemaApplyOutcomeFailed    = "failed"
	SchemaApplyOutcomeUnknown   = "unknown"
)

Outcomes an apply document reports. Refused and failed say that nothing reached the target; unknown says the statements were sent and the run returned an error.

View Source
const (
	SchemaRefusalStalePlan      = "stale-plan"
	SchemaRefusalProtectedTable = "protected-table"
)

Refusal codes this operator acts on. Ptah may add codes without raising the contract version, since every refusal means nothing reached the target, so a code outside this list is read as a refusal of no particular kind rather than refused as a document.

Variables

This section is empty.

Functions

func DialectMatches

func DialectMatches(engine, dialect string) bool

DialectMatches reports whether a Ptah report dialect belongs to the explicitly configured database engine.

func DriftFindingCategories

func DriftFindingCategories() []string

DriftFindingCategories returns a copy of the closed machine vocabulary.

It is the list the pinned Ptah can emit: support/ptah-drift-categories.json records that list, and hack/ptahdriftcategories holds the record to the pinned source. An unknown category is refused rather than published or dropped. The status enum could not store it, and leaving it out would understate the report's count and its highest severity.

Extending the list moves DriftFindingVocabularyVersion, the PtahSchema status enum and the CRD schema version together. The frame version stays: a controller that predates a category refuses a frame carrying it, which is the refusal a version mismatch would give.

func DriftReportDigest

func DriftReportDigest(report DriftReport) (string, error)

DriftReportDigest identifies the exact comparison result emitted by one drift read. It is deliberately not a database-schema fingerprint: only a native plan's from_fingerprint may authorize an Apply. The digest is useful for audit and change detection without inventing a second schema identity.

func IsKnownDriftFindingCategory

func IsKnownDriftFindingCategory(category string) bool

IsKnownDriftFindingCategory reports whether category belongs to the closed v1 machine vocabulary.

func IsKnownMigrationOutcome

func IsKnownMigrationOutcome(outcome string) bool

IsKnownMigrationOutcome reports whether outcome is one a run document may carry. The runner's termination summary repeats a run's outcome, and it is held to the same set the document was.

func IsKnownPrivilegeChange

func IsKnownPrivilegeChange(kind string) bool

IsKnownPrivilegeChange reports whether kind belongs to the vocabulary.

func PrivilegeChangeKinds

func PrivilegeChangeKinds() []string

PrivilegeChangeKinds returns a copy of the closed vocabulary, in the order a plan lists its kinds.

func StatementsSQL

func StatementsSQL(plan PlanFile) string

StatementsSQL renders a plan's statements as they were stored.

One statement per entry, in the order the plan holds them, each terminated once. Nothing is re-split on a semicolon: a statement may carry one inside a string, a comment or a function body, and splitting there would hand a reader SQL the plan never held.

Types

type DriftFinding

type DriftFinding struct {
	Category string `json:"category"`
	Count    int32  `json:"count"`
	Severity string `json:"severity"`
}

type DriftReport

type DriftReport struct {
	Drift            bool            `json:"drift"`
	Failed           bool            `json:"failed"`
	FailureThreshold string          `json:"failure_threshold"`
	HighestSeverity  string          `json:"highest_severity"`
	Dialect          string          `json:"dialect,omitempty"`
	Sources          string          `json:"sources,omitempty"`
	DatabaseURL      string          `json:"database_url,omitempty"`
	IgnoredTables    []string        `json:"ignored_tables,omitempty"`
	Findings         []DriftFinding  `json:"findings,omitempty"`
	Diff             json.RawMessage `json:"diff,omitempty"`
	Error            string          `json:"error,omitempty"`
}

func DecodeDrift

func DecodeDrift(data []byte, exitCode int) (DriftReport, error)

DecodeDrift maps the CLI's configured failure threshold into a domain result. Drift may be reported without failing the command.

type InspectReport

type InspectReport struct {
	Reference       string `json:"reference"`
	PinnedReference string `json:"pinned_reference"`
	Digest          string `json:"digest"`
	MediaType       string `json:"media_type"`
	Size            int64  `json:"size"`
	ArtifactType    string `json:"artifact_type"`
}

func DecodeInspect

func DecodeInspect(data []byte) (InspectReport, error)

type MigrationDirty

type MigrationDirty struct {
	Version int64  `json:"version"`
	Applied int    `json:"applied"`
	Total   int    `json:"total"`
	Error   string `json:"error,omitempty"`
}

MigrationDirty is the revision row a failed or interrupted run left behind.

type MigrationRecord

type MigrationRecord struct {
	Version         int64  `json:"version"`
	VersionKey      string `json:"version_key,omitempty"`
	Description     string `json:"description,omitempty"`
	Checksum        string `json:"checksum,omitempty"`
	AppliedChecksum string `json:"applied_checksum,omitempty"`
	Checkpoint      bool   `json:"checkpoint,omitempty"`
	TransactionMode string `json:"transaction_mode,omitempty"`
	State           string `json:"state"`
}

MigrationRecord is one migration of the artifact as the database accounts for it.

type MigrationRunReport

type MigrationRunReport struct {
	ContractVersion int                    `json:"contract_version"`
	Direction       string                 `json:"direction"`
	Outcome         string                 `json:"outcome"`
	Planned         []int64                `json:"planned,omitempty"`
	Applied         []int64                `json:"applied,omitempty"`
	Error           string                 `json:"error,omitempty"`
	Status          *MigrationStatusReport `json:"status,omitempty"`
}

MigrationRunReport is Ptah's `migrations up --json` document.

func DecodeMigrationRun

func DecodeMigrationRun(data []byte) (MigrationRunReport, error)

DecodeMigrationRun reads a run document, including the status it carries.

type MigrationStatusReport

type MigrationStatusReport struct {
	ContractVersion   int   `json:"contract_version"`
	CurrentVersion    int64 `json:"current_version"`
	CheckpointVersion int64 `json:"checkpoint_version,omitempty"`
	TotalMigrations   int   `json:"total_migrations"`
	HasPendingChanges bool  `json:"has_pending_changes"`
	// PendingMigrations is the selection Ptah itself made: the versions its
	// next run would execute, in the order it would execute them. It is not
	// the same question the per-migration states answer, and after a
	// checkpoint bootstrap the two disagree on purpose -- a version the
	// checkpoint replaced is reported pending once the bootstrap is no longer
	// what the database is doing, and Ptah's own floor still skips it.
	//
	// Carried without omitempty, because this report is decoded from Ptah and
	// encoded again into the runner's result frame, and an empty selection is
	// the answer that matters: it says the next run would execute nothing.
	// Dropped from the frame, it reaches the controller as an absent field,
	// which [MigrationStatusReport.Pending] reads as a build that reports no
	// selection at all and answers by recounting the states. Ptah declares the
	// field the same way for the same reason.
	PendingMigrations []int64           `json:"pending_migrations"`
	Migrations        []MigrationRecord `json:"migrations,omitempty"`
	DirtyRevision     *MigrationDirty   `json:"dirty_revision,omitempty"`
}

MigrationStatusReport is Ptah's `migrations status --json` document.

func DecodeMigrationStatus

func DecodeMigrationStatus(data []byte) (MigrationStatusReport, error)

DecodeMigrationStatus reads a status document and refuses everything it cannot fully account for.

func (MigrationStatusReport) LastVersion

func (r MigrationStatusReport) LastVersion() int64

LastVersion is the highest version the artifact carries, and zero for an artifact that carries none. It is the artifact's own extent: the document lists the artifact's migrations with what the database did about each, so a revision beyond this number is one the artifact cannot account for at all.

func (MigrationStatusReport) Modified

func (r MigrationStatusReport) Modified() []int64

Modified names the applied migrations whose files no longer account for them. Nothing may execute while one exists, and the controller never resolves it.

func (MigrationStatusReport) OutOfOrder

func (r MigrationStatusReport) OutOfOrder() []int64

OutOfOrder names the pending migrations that sort below a version the database has already applied. Ptah's linear execution order refuses a run while one exists, so a plan that carried them would be a decision nobody could execute.

func (MigrationStatusReport) Pending

func (r MigrationStatusReport) Pending() []int64

Pending names the versions the next run would execute, in the order Ptah would execute them.

The list is Ptah's, because deriving a second one from the per-migration states gets a database that bootstrapped from a checkpoint wrong: the versions the checkpoint replaced report themselves pending afterwards, while Ptah's own floor skips them and its selection is empty. A controller that recounted would publish a plan for migrations that cannot run, and publish it again after every run that correctly did nothing.

A document that carries no selection at all is read the old way, so a build that reports only states still resolves to a list rather than to silence.

type PlanFile

type PlanFile struct {
	FormatVersion   int      `json:"format_version"`
	Name            string   `json:"name"`
	Dialect         string   `json:"dialect"`
	FromFingerprint string   `json:"from_fingerprint"`
	ToFingerprint   string   `json:"to_fingerprint"`
	Exclude         []string `json:"exclude,omitempty"`
	// SchemasBeyondURL names the schemas the source fingerprint covers that the
	// connection URL's own scope does not: a desired state that names a schema
	// other than the one a schema-scoped URL connects to. Ptah reads the same
	// set again when it verifies the plan before applying it. The operator
	// carries the plan bytes as they are and reads nothing from this field; it
	// is declared so the strict decoder accepts a plan that has it.
	SchemasBeyondURL []string `json:"schemas_beyond_url,omitempty"`
	// ManagedRows and RowsFingerprint describe the declared row sets this plan
	// read, and the state it read them in. A plan that changes declared rows
	// carries both, and a plan that changes only structure carries neither.
	//
	// They are names and a digest: a table, its key columns and the columns the
	// declaration owns, plus one fingerprint over the rows as they were. No row
	// value appears here, which is what lets the operator read them, publish
	// them and report on them while no declared value reaches status, an Event
	// or an ordinary log.
	ManagedRows     []PlanRowSet    `json:"managed_rows,omitempty"`
	RowsFingerprint string          `json:"rows_fingerprint,omitempty"`
	Destructive     bool            `json:"destructive"`
	Statements      []PlanStatement `json:"statements"`

	// PrivilegeChanges are the kinds of authority the statements change, read
	// by the operator from the SQL: grants, role membership, ownership,
	// row-security policies and definer rights. Ptah's document has no such
	// field and cannot supply one, since a plan is decoded strictly; the list
	// is the operator's own, in the order PrivilegeChangeKinds gives.
	PrivilegeChanges []string `json:"-"`
}

func DecodePlan

func DecodePlan(data []byte, expectedDialect string) (PlanFile, error)

type PlanRowSet

type PlanRowSet struct {
	Schema  string   `json:"schema,omitempty"`
	Table   string   `json:"table"`
	Keys    []string `json:"keys"`
	Columns []string `json:"columns"`
}

PlanRowSet is one declared row set a plan read: the table it owns, the columns that identify a row in it, and the columns the declaration writes.

type PlanStatement

type PlanStatement struct {
	SQL      string `json:"sql"`
	Severity string `json:"severity"`
	Reason   string `json:"reason"`
}

type ResolveReport

type ResolveReport struct {
	Reference       string `json:"reference"`
	PinnedReference string `json:"pinned_reference"`
	Digest          string `json:"digest"`
	MediaType       string `json:"media_type"`
	Size            int64  `json:"size"`
}

func DecodeResolve

func DecodeResolve(data []byte) (ResolveReport, error)

type SchemaApplyReport

type SchemaApplyReport struct {
	ContractVersion int            `json:"contract_version"`
	Outcome         string         `json:"outcome"`
	PlanName        string         `json:"plan_name,omitempty"`
	PlanDigest      string         `json:"plan_digest,omitempty"`
	Statements      []string       `json:"statements,omitempty"`
	Refusal         *SchemaRefusal `json:"refusal,omitempty"`
	Error           string         `json:"error,omitempty"`
}

SchemaApplyReport is Ptah's `schema apply --json` document.

func DecodeSchemaApply

func DecodeSchemaApply(data []byte) (SchemaApplyReport, error)

DecodeSchemaApply reads one apply document under the rules DecodeSchemaPlan states.

type SchemaPlanReport

type SchemaPlanReport struct {
	ContractVersion int            `json:"contract_version"`
	Outcome         string         `json:"outcome"`
	PlanDigest      string         `json:"plan_digest,omitempty"`
	PlanPath        string         `json:"plan_path,omitempty"`
	Refusal         *SchemaRefusal `json:"refusal,omitempty"`
	// Error is the sentence Ptah printed on standard error. It may quote a
	// statement, so it is for a fence refusal's tables and nothing else.
	Error string `json:"error,omitempty"`
}

SchemaPlanReport is Ptah's `schema plan --json` document.

The plan document it embeds is not declared, so it is not read: the file --output wrote is the plan, and PlanDigest is what binds the report to it.

func DecodeSchemaPlan

func DecodeSchemaPlan(data []byte) (SchemaPlanReport, error)

DecodeSchemaPlan reads one plan document and refuses anything it cannot account for: another contract version, an outcome it does not know, a second JSON value, and an outcome whose fields contradict it.

Fields it does not declare are ignored, because Ptah adds fields without raising the version.

type SchemaRefusal

type SchemaRefusal struct {
	Code string `json:"code"`
	// Tables are the fenced tables a protected-table refusal names.
	Tables []string `json:"tables,omitempty"`
	// Changed is what moved under a stale-plan refusal: "schema" or "rows".
	Changed             string `json:"changed,omitempty"`
	PlanFingerprint     string `json:"plan_fingerprint,omitempty"`
	DatabaseFingerprint string `json:"database_fingerprint,omitempty"`
}

SchemaRefusal is why a plan or an apply refused. Only the fields its code describes are set.

type VerificationFinding

type VerificationFinding struct {
	Requirement string `json:"requirement"`
	Detail      string `json:"detail"`
}

type VerifyReport

type VerifyReport struct {
	Reference string                `json:"reference"`
	Digest    string                `json:"digest"`
	Satisfied []string              `json:"satisfied"`
	Findings  []VerificationFinding `json:"findings"`
}

func DecodeVerify

func DecodeVerify(data []byte) (VerifyReport, error)

Jump to

Keyboard shortcuts

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