sqlserver

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 23 Imported by: 1 module, 0 packages

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 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

View Source
const (
	// PlanFileKind identifies a plan file among migration artifacts.
	PlanFileKind = "sqlglot-go/script-plan"
	// PlanFileVersion is the plan file format this package writes. Version 3
	// carries the declared-schema step as an engine.Artifact.
	PlanFileVersion = 3
)

Variables

This section is empty.

Functions

func Batches

func Batches(name, content string) ([]string, error)

Batches splits plain T-SQL at GO and rejects unsupported sqlcmd directives.

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.

func WithSchemaLockTimeout added in v1.3.8

func WithSchemaLockTimeout(timeout time.Duration) ApplyOption

WithSchemaLockTimeout bounds how long each unit of the schema step waits for the migration lock and for a blocked statement, before failing it. It defaults to 15 seconds, the scripts' own statement lock timeout, and must be positive: the dialect reads zero as "do not wait".

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

type ExecutionError struct {
	Outcome string
	Err     error
	// contains filtered or unexported fields
}

func (*ExecutionError) Error

func (e *ExecutionError) Error() string

func (*ExecutionError) Unwrap

func (e *ExecutionError) Unwrap() error

type Function

type Function struct {
	Name          string   `json:"name"`
	Type          string   `json:"type"`
	Nullable      bool     `json:"nullable"`
	Prerequisites []string `json:"prerequisites,omitempty"`
}

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 reviewed declared-schema reconciliation, an engine artifact
	// of the "sqlserver" dialect, that runs between the pre and post scripts;
	// nil when the bundle declares no objects.
	Schema *engine.Artifact
	// 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    *engine.Artifact `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, a reviewed engine artifact.

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

func Plan(ctx context.Context, opts Options) (PlanFile, error)

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

func (p PlanFile) Changed() bool

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

func (p PlanFile) Pretty() api.Text

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.

func (PlanFile) SchemaStatements added in v1.3.8

func (p PlanFile) SchemaStatements() []SchemaStatement

SchemaStatements lists the statements the schema step runs, units in artifact order and statements in op order; nil when the step plans none.

func (PlanFile) Selected added in v1.3.0

func (p PlanFile) Selected() []string

Selected lists the scripts the plan runs, in order.

func (*PlanFile) UnmarshalJSON added in v1.3.8

func (p *PlanFile) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a plan file strictly. A version 2 file that carries a schema step is refused by name: its statements are not a v3 artifact, and decoding it as one would silently drop them.

func (PlanFile) Verify added in v1.3.0

func (p PlanFile) Verify() error

Verify checks the file's kind, version and integrity hash.

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 committed units, partial when it
	// committed some and then failed: a re-plan covers the rest.
	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 SchemaStatement struct {
	Unit        string
	Change      string
	SQL         string
	Destructive bool
}

SchemaStatement is one statement of the schema step as the artifact plans it.

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

type SkippedScript struct {
	Script     string `json:"script"`
	Dependency string `json:"dependency"`
}

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

Jump to

Keyboard shortcuts

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