Documentation
¶
Overview ¶
Package sqlserver plans and applies dependency-aware scripts on SQL Server through a self-contained plan file: Plan observes the target once and writes what apply will do, and Apply runs exactly that plan, refusing it as stale when the target moved since.
Index ¶
- Constants
- func Batches(name, content string) ([]string, error)
- type ApplyOption
- type Connection
- type DependencyError
- type ExecutionError
- type Function
- type Options
- type PlanBundle
- type PlanFile
- type PlanTarget
- type Progress
- type ProgressStep
- type Result
- type SchemaStatement
- type SchemaStep
- type ScriptFailure
- type SkippedScript
- type StalePlanError
Constants ¶
const ( // PlanFileKind identifies a plan file among migration artifacts. PlanFileKind = "sqlglot-go/script-plan" // PlanFileVersion is the plan file format this package writes and reads. PlanFileVersion = 2 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ApplyOption ¶ added in v1.3.0
type ApplyOption func(*applyConfig)
ApplyOption configures Apply.
func WithAnalysisCache ¶ added in v1.3.0
func WithAnalysisCache(store cache.Store, ttl time.Duration) ApplyOption
WithAnalysisCache reuses script and module analyses across Plan and Apply, as Options.Analysis does for Plan.
func WithBarrier ¶ added in v1.3.0
func WithBarrier(fn func(context.Context, Connection) error) ApplyOption
WithBarrier runs fn on the locked apply connection after the schema step and before the post scripts, for deployment steps that belong between the phases. Its writes are not part of the plan.
func WithPrimaryKeyWarnings ¶ added in v1.3.3
func WithPrimaryKeyWarnings() ApplyOption
WithPrimaryKeyWarnings continues after a script's primary-key violation when the script rolled back and its failure was recorded. Scripts that depend on it are skipped.
func WithProgress ¶ added in v1.3.2
func WithProgress(fn func(Progress)) ApplyOption
WithProgress calls fn, on the applying goroutine, as each step of the apply starts and ends. A step the plan does not reach is never reported.
type Connection ¶
type Connection interface {
// contains filtered or unexported methods
}
Connection is the locked SQL session a barrier runs on.
type DependencyError ¶ added in v1.3.2
type DependencyError struct{ SkippedScript }
DependencyError is why a script was skipped, as its Progress reports it.
func (*DependencyError) Error ¶ added in v1.3.2
func (e *DependencyError) Error() string
type ExecutionError ¶
func (*ExecutionError) Error ¶
func (e *ExecutionError) Error() string
func (*ExecutionError) Unwrap ¶
func (e *ExecutionError) Unwrap() error
type Options ¶
type Options struct {
DB *sql.DB
Scope string
// AbsorbScopes, when set, merges the ledgers of the other scopes it accepts
// into Scope: Plan reads the newest row of each path across Scope and those
// scopes, and Apply moves them into Scope. The plan file records the
// scopes it absorbed.
AbsorbScopes func(scope string) bool
Scripts []scripts.Script
Functions []Function
// Schema is the declared-schema reconciliation that runs between the pre
// and post scripts; nil when the bundle declares no objects.
Schema *SchemaStep
// Warnings are the caller's own warnings about the bundle; the plan file
// carries them before the plan's.
Warnings []string
// Analysis caches script and module-definition analyses by content
// (scripts.AnalysisKey); nil analyses every body on every call.
Analysis cache.Store
// AnalysisTTL is the expiry of each analysis written to Analysis; zero
// writes without expiry.
AnalysisTTL time.Duration
// contains filtered or unexported fields
}
type PlanBundle ¶ added in v1.3.0
type PlanBundle struct {
Scripts []scripts.Script `json:"scripts"`
Functions []Function `json:"functions,omitempty"`
Schema *SchemaStep `json:"schema,omitempty"`
}
PlanBundle is everything apply needs to run the plan without the sources: the prepared scripts with their SQL and effects, the registered functions and the declared-schema step.
type PlanFile ¶ added in v1.3.0
type PlanFile struct {
Kind string `json:"kind"`
Version int `json:"version"`
Target PlanTarget `json:"target"`
Bundle PlanBundle `json:"bundle"`
Ledger map[string]scripts.State `json:"ledger"`
// Absorbed are the other ledger scopes the plan read, newest row per path
// with Target.Scope, and apply moves into it.
Absorbed []string `json:"absorbed,omitempty"`
Observations map[string]string `json:"observations"`
Actions []scripts.Pending `json:"actions"`
Warnings []string `json:"warnings,omitempty"`
Hash string `json:"hash"`
}
PlanFile is a self-contained, terraform-style plan: what the target looked like (its ledger and every observed input) and the ordered actions apply takes. Hash covers every other field; it guards the file's integrity and nothing else.
func Plan ¶ added in v1.3.0
Plan observes the target once, without creating metadata or running anything, and returns the plan file Apply runs: every script of the bundle, pre scripts before post scripts, planned in one pass, and the schema step between them. What runs is decided here and nowhere else: a script is selected for its own source, inputs and reads as they are now, never for what an earlier script in the plan will write.
func (PlanFile) Changed ¶ added in v1.3.0
Changed reports whether applying the plan changes anything beyond running its `runs: always` scripts.
func (PlanFile) Functions ¶ added in v1.3.2
func (p PlanFile) Functions() []scripts.FunctionInput
Functions are the functions and built-ins the plan selects scripts by.
func (PlanFile) Pretty ¶ added in v1.3.0
Pretty renders the plan terraform-style: each phase's scripts as + run (new), ~ re-run with its reasons, ! blocked and ? effects unknown, the schema step between the phases, the warnings and the totals.
type PlanTarget ¶ added in v1.3.0
type PlanTarget struct {
Server string `json:"server"`
Database string `json:"database"`
Scope string `json:"scope"`
}
PlanTarget is the database a plan was made for.
func (PlanTarget) String ¶ added in v1.3.0
func (t PlanTarget) String() string
type Progress ¶ added in v1.3.2
type Progress struct {
Step ProgressStep
Script string
Done bool
Err error
Warning error
}
Progress is one step of an Apply starting or, when Done, ending with an error or warning.
type ProgressStep ¶ added in v1.3.2
type ProgressStep string
ProgressStep is what a Progress reports on.
const ( // ProgressCheck is planning the bundle again to compare it with the plan // file: before anything runs, and after Script when its effects are unknown. ProgressCheck ProgressStep = "check" // ProgressScript is running Script and recording it. ProgressScript ProgressStep = "script" // ProgressSchema is running the schema step. ProgressSchema ProgressStep = "schema" )
type Result ¶
type Result struct {
// Outcome is applied, already-applied, stale, or the worst outcome of the
// scripts that failed (failed, partial or commit-unknown). A completed
// apply with tolerated script failures is applied-with-warnings.
Outcome string `json:"outcome"`
Applied []string `json:"applied"`
// Schema is applied when the schema step ran statements.
Schema string `json:"schema,omitempty"`
// Blocked lists the scripts the plan left blocked.
Blocked []string `json:"blocked,omitempty"`
// Failed are the scripts that failed; Skipped the selected ones that did
// not run because of them.
Failed []ScriptFailure `json:"failed,omitempty"`
Skipped []SkippedScript `json:"skipped,omitempty"`
Warnings []ScriptFailure `json:"warnings,omitempty"`
}
func Apply ¶ added in v1.3.0
func Apply(ctx context.Context, db *sql.DB, file PlanFile, options ...ApplyOption) (result Result, returnErr error)
Apply runs file on db: it verifies the file's integrity and target, re-plans its bundle against the target and refuses the file with a StalePlanError when the ledger, an observed input or a selection moved since, before anything runs. It then runs the selected pre scripts, the schema step, any WithBarrier step and the selected post scripts, on one locked connection. An input an upstream script in the plan writes is re-observed once after that writer; every other input holds its planned value. A script with unknown effects is followed by a re-plan, and work the file did not plan stops the apply with a StalePlanError: the scripts it already committed stay recorded, and a new plan covers the rest. A script that fails does not stop the apply: the scripts that directly depend on it are skipped, the rest run, and Apply returns every failure. It does stop when the session is lost and, after a pre script failed, before the schema step or a barrier.
type SchemaStatement ¶ added in v1.3.0
type SchemaStep ¶ added in v1.3.0
type SchemaStep struct {
// Objects are the declared tables and routines, schema-qualified.
Objects []string `json:"objects"`
Statements []SchemaStatement `json:"statements"`
}
SchemaStep is the reviewed reconciliation of the bundle's declared tables and routines: the statements apply runs between the pre and post scripts, planned against the declared objects as the plan observed them.
type ScriptFailure ¶ added in v1.3.2
type ScriptFailure struct {
Script string `json:"script"`
Outcome string `json:"outcome"`
Error string `json:"error"`
}
ScriptFailure is a script whose run failed: Outcome is failed when it was rolled back, partial when a nontransactional script's writes may remain, and commit-unknown when its own transaction control leaves that open. A partial or commit-unknown script is blocked until it is reconciled.
type SkippedScript ¶ added in v1.3.2
SkippedScript is a selected script that did not run because Dependency, a script it directly depends on, failed or was itself skipped.
type StalePlanError ¶ added in v1.3.0
type StalePlanError struct {
Moved []string `json:"moved"`
}
StalePlanError refuses a plan whose target moved since it was made.
func (*StalePlanError) Error ¶ added in v1.3.0
func (e *StalePlanError) Error() string