{"modulePath":"github.com/flanksource/sqlglot-go","version":"v1.3.3","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqlserver\n\nPackage 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.\n\n## Constants\n\n```go\nconst (\n\t// PlanFileKind identifies a plan file among migration artifacts.\n\tPlanFileKind = \"sqlglot-go/script-plan\"\n\t// PlanFileVersion is the plan file format this package writes and reads.\n\tPlanFileVersion = 2\n)\n```\n\n## Functions\n\n```go\nfunc Batches(name, content string) ([]string, error)\n```\nBatches splits plain T-SQL at GO and rejects unsupported sqlcmd directives.\n\n## Types\n\n```go\ntype ApplyOption func(*applyConfig)\n```\nApplyOption configures Apply.\n\n```go\nfunc WithAnalysisCache(store cache.Store, ttl time.Duration) ApplyOption\n```\nWithAnalysisCache reuses script and module analyses across Plan and Apply, as Options.Analysis does for Plan.\n\n```go\nfunc WithBarrier(fn func(context.Context, Connection) error) ApplyOption\n```\nWithBarrier 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.\n\n```go\nfunc WithPrimaryKeyWarnings() ApplyOption\n```\nWithPrimaryKeyWarnings 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.\n\n```go\nfunc WithProgress(fn func(Progress)) ApplyOption\n```\nWithProgress 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.\n\n```go\ntype Connection interface {\n\t// contains filtered or unexported methods\n}\n```\nConnection is the locked SQL session a barrier runs on.\n\n```go\ntype DependencyError struct{ SkippedScript }\n```\nDependencyError is why a script was skipped, as its Progress reports it.\n\n```go\nfunc (e *DependencyError) Error() string\n```\n\n```go\ntype ExecutionError struct {\n\tOutcome string\n\tErr     error\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc (e *ExecutionError) Error() string\n```\n\n```go\nfunc (e *ExecutionError) Unwrap() error\n```\n\n```go\ntype Function struct {\n\tName          string   `json:\"name\"`\n\tType          string   `json:\"type\"`\n\tNullable      bool     `json:\"nullable\"`\n\tPrerequisites []string `json:\"prerequisites,omitempty\"`\n}\n```\n\n```go\ntype Options struct {\n\tDB    *sql.DB\n\tScope string\n\t// AbsorbScopes, when set, merges the ledgers of the other scopes it accepts\n\t// into Scope: Plan reads the newest row of each path across Scope and those\n\t// scopes, and Apply moves them into Scope. The plan file records the\n\t// scopes it absorbed.\n\tAbsorbScopes func(scope string) bool\n\tScripts      []scripts.Script\n\tFunctions    []Function\n\t// Schema is the declared-schema reconciliation that runs between the pre\n\t// and post scripts; nil when the bundle declares no objects.\n\tSchema *SchemaStep\n\t// Warnings are the caller's own warnings about the bundle; the plan file\n\t// carries them before the plan's.\n\tWarnings []string\n\t// Analysis caches script and module-definition analyses by content\n\t// (scripts.AnalysisKey); nil analyses every body on every call.\n\tAnalysis cache.Store\n\t// AnalysisTTL is the expiry of each analysis written to Analysis; zero\n\t// writes without expiry.\n\tAnalysisTTL time.Duration\n\t// contains filtered or unexported fields\n}\n```\n\n```go\ntype PlanBundle struct {\n\tScripts   []scripts.Script `json:\"scripts\"`\n\tFunctions []Function       `json:\"functions,omitempty\"`\n\tSchema    *SchemaStep      `json:\"schema,omitempty\"`\n}\n```\nPlanBundle 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.\n\n```go\ntype PlanFile struct {\n\tKind    string                   `json:\"kind\"`\n\tVersion int                      `json:\"version\"`\n\tTarget  PlanTarget               `json:\"target\"`\n\tBundle  PlanBundle               `json:\"bundle\"`\n\tLedger  map[string]scripts.State `json:\"ledger\"`\n\t// Absorbed are the other ledger scopes the plan read, newest row per path\n\t// with Target.Scope, and apply moves into it.\n\tAbsorbed     []string          `json:\"absorbed,omitempty\"`\n\tObservations map[string]string `json:\"observations\"`\n\tActions      []scripts.Pending `json:\"actions\"`\n\tWarnings     []string          `json:\"warnings,omitempty\"`\n\tHash         string            `json:\"hash\"`\n}\n```\nPlanFile 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.\n\n```go\nfunc Plan(ctx context.Context, opts Options) (PlanFile, error)\n```\nPlan 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.\n\n```go\nfunc (p PlanFile) Changed() bool\n```\nChanged reports whether applying the plan changes anything beyond running its \\`runs: always\\` scripts.\n\n```go\nfunc (p PlanFile) Functions() []scripts.FunctionInput\n```\nFunctions are the functions and built-ins the plan selects scripts by.\n\n```go\nfunc (p PlanFile) Pretty() api.Text\n```\nPretty 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.\n\n```go\nfunc (p PlanFile) Selected() []string\n```\nSelected lists the scripts the plan runs, in order.\n\n```go\nfunc (p PlanFile) Verify() error\n```\nVerify checks the file's kind, version and integrity hash.\n\n```go\ntype PlanTarget struct {\n\tServer   string `json:\"server\"`\n\tDatabase string `json:\"database\"`\n\tScope    string `json:\"scope\"`\n}\n```\nPlanTarget is the database a plan was made for.\n\n```go\nfunc (t PlanTarget) String() string\n```\n\n```go\ntype Progress struct {\n\tStep    ProgressStep\n\tScript  string\n\tDone    bool\n\tErr     error\n\tWarning error\n}\n```\nProgress is one step of an Apply starting or, when Done, ending with an error or warning.\n\n```go\ntype ProgressStep string\n```\nProgressStep is what a Progress reports on.\n\n```go\nconst (\n\t// ProgressCheck is planning the bundle again to compare it with the plan\n\t// file: before anything runs, and after Script when its effects are unknown.\n\tProgressCheck ProgressStep = \"check\"\n\t// ProgressScript is running Script and recording it.\n\tProgressScript ProgressStep = \"script\"\n\t// ProgressSchema is running the schema step.\n\tProgressSchema ProgressStep = \"schema\"\n)\n```\n\n```go\ntype Result struct {\n\t// Outcome is applied, already-applied, stale, or the worst outcome of the\n\t// scripts that failed (failed, partial or commit-unknown). A completed\n\t// apply with tolerated script failures is applied-with-warnings.\n\tOutcome string   `json:\"outcome\"`\n\tApplied []string `json:\"applied\"`\n\t// Schema is applied when the schema step ran statements.\n\tSchema string `json:\"schema,omitempty\"`\n\t// Blocked lists the scripts the plan left blocked.\n\tBlocked []string `json:\"blocked,omitempty\"`\n\t// Failed are the scripts that failed; Skipped the selected ones that did\n\t// not run because of them.\n\tFailed   []ScriptFailure `json:\"failed,omitempty\"`\n\tSkipped  []SkippedScript `json:\"skipped,omitempty\"`\n\tWarnings []ScriptFailure `json:\"warnings,omitempty\"`\n}\n```\n\n```go\nfunc Apply(ctx context.Context, db *sql.DB, file PlanFile, options ...ApplyOption) (result Result, returnErr error)\n```\nApply 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.\n\n```go\ntype SchemaStatement struct {\n\tSQL         string `json:\"sql\"`\n\tComment     string `json:\"comment,omitempty\"`\n\tDestructive bool   `json:\"destructive,omitempty\"`\n}\n```\n\n```go\ntype SchemaStep struct {\n\t// Objects are the declared tables and routines, schema-qualified.\n\tObjects    []string          `json:\"objects\"`\n\tStatements []SchemaStatement `json:\"statements\"`\n}\n```\nSchemaStep 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.\n\n```go\ntype ScriptFailure struct {\n\tScript  string `json:\"script\"`\n\tOutcome string `json:\"outcome\"`\n\tError   string `json:\"error\"`\n}\n```\nScriptFailure 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.\n\n```go\ntype SkippedScript struct {\n\tScript     string `json:\"script\"`\n\tDependency string `json:\"dependency\"`\n}\n```\nSkippedScript is a selected script that did not run because Dependency, a script it directly depends on, failed or was itself skipped.\n\n```go\ntype StalePlanError struct {\n\tMoved []string `json:\"moved\"`\n}\n```\nStalePlanError refuses a plan whose target moved since it was made.\n\n```go\nfunc (e *StalePlanError) Error() string\n```\n\n","path":"github.com/flanksource/sqlglot-go/database/migrate/scripts/sqlserver","name":"sqlserver","synopsis":"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.","isRedistributable":true}
