sqlserver

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 17 Imported by: 2 modules, 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 is the pure half of the SQL Server dialect: it compares two schemas, lowers the changes to statements and renders reviewed artifacts as scripts, all without a database connection.

Index

Constants

View Source
const FeatureTryConvert = "tryConvert"

FeatureTryConvert is the capability feature that says TRY_CONVERT exists, which is database compatibility level 110 and above.

View Source
const LockResource = "commons-db/migrate/schema"

LockResource is the applock that serialises every migration of a database, applied directly or through a rendered script.

View Source
const SessionOptions = "" /* 185-byte string literal not displayed */

SessionOptions are the SET options every migration statement runs under, whether the executor applies an artifact or a rendered script does.

Variables

This section is empty.

Functions

func FromAtlas added in v1.0.5

func FromAtlas(input *schema.Realm) (migrate.Schema, error)

FromAtlas projects supported declarative SQL Server tables and routines into the native model.

func RenderScript added in v1.1.3

func RenderScript(a engine.Artifact, options ScriptOptions) (string, error)

RenderScript renders a reviewed artifact as one T-SQL script, unit by unit in the artifact's order. The script is a single batch, so an error in a unit ends the run and the units before it stay committed, as engine.Apply does.

Each unit is one transaction (a unit with a non-transactional statement runs without one, under a session-owned lock) that takes the migration applock, then compares the unit's own changes with the catalog:

  • every change still at its baseline: the unit's preconditions run, each a THROW with the precondition's message when its query returns a row, then its statements, each in its own batch (sp_executesql), so module DDL starts a batch;
  • every change already in its desired state: the unit is skipped, which is what makes running the script again a no-op;
  • anything else: the script THROWs that the unit matches neither state.

A unit whose changes have no state to test (CREATE OR ALTER style module changes) runs every time, and is refused unless its statements are re-runnable. The guard covers the unit's own changes, not the dependents it drops and re-creates around them: those sit inside the same transaction. A dependent is dropped and created under the name recorded when the artifact was planned, so a script run on a database that names it differently fails and rolls its unit back. The statements are the artifact's, which are valid at the compatibility level the artifact was planned at; the script adds only THROW, sp_getapplock and sp_executesql, all available at level 100.

The dialect "sqlserver" must be registered (import the dialect package), as the artifact is re-planned to prove it is the one its planner wrote.

Types

type HCLFile added in v1.3.8

type HCLFile struct {
	Name    string
	Content string
}

HCLFile is one file ExportHCL writes: Name is relative and ends in .hcl.

func ExportHCL added in v1.3.8

func ExportHCL(s migrate.Schema, opts HCLOptions) ([]HCLFile, error)

ExportHCL writes the tables of s as SQL Server HCL that EvalHCL and FromAtlas read back to the same tables (members in a different order, and the names SQL Server gave its system-named defaults, apart). Objects other than tables are not part of the HCL; they and everything else the HCL cannot declare (ROWGUIDCOL, fill factor and the other index options, NOT FOR REPLICATION, a foreign key to a table outside s) fail the export unless HCLOptions OmitUnsupported is set.

type HCLLayout added in v1.3.8

type HCLLayout int

HCLLayout says how ExportHCL splits the tables it writes.

const (
	// HCLDocument writes every table into one file, with one schema block per schema.
	HCLDocument HCLLayout = iota
	// HCLFilePerTable writes one file per table, each starting with its own schema block.
	HCLFilePerTable
)

type HCLOptions added in v1.3.8

type HCLOptions struct {
	Layout HCLLayout
	// OmitUnsupported leaves out what the SQL Server HCL cannot declare, each
	// omission written as a comment line. Without it the export fails, naming
	// the table, the member and the attribute.
	OmitUnsupported bool
	// Columns declares only the named columns of a table, keyed by table name: a
	// partial declaration (`partial = true`), with no keys, indexes, foreign keys
	// or checks, for a table the declaration does not own. A table declared
	// without Columns is declared in full and carries no attribute, unless its
	// schema marks it Partial.
	Columns map[string][]string
	// DatabaseCollation is the database's default collation. A column carrying
	// it is written without a collation, so the HCL stays valid on a database
	// with another default. Empty writes every column's collation.
	DatabaseCollation string
	// Notes are comment lines written at the top of the output (of the first
	// file), for what the caller left out before the export, each without its
	// comment marker.
	Notes []string
}

HCLOptions configure ExportHCL.

type Planner added in v1.3.8

type Planner struct{}

Planner is the pure half of the SQL Server dialect: it diffs, lowers and guards without a database. The database half embeds it and overrides Capabilities, Inspect, Dependents (live), Open, Reconcile and Exec.

func (Planner) Capabilities added in v1.3.8

func (Planner) Capabilities(_ context.Context, q engine.Querier) (engine.Capabilities, error)

Capabilities answers only offline (q == nil): SQL Server DDL is transactional on every server, and none of the version- or compatibility-gated features (TRY_CONVERT among them) holds for all, so the planner keeps choosing the compatibility-level-100-safe forms.

func (Planner) Dependents added in v1.3.8

func (Planner) Dependents(_ context.Context, q engine.Querier, current migrate.Schema, columns []engine.ColumnRef) ([]engine.Dependent, error)

Dependents derives, from current alone, every index, key, foreign key and check that depends on the columns. It over-includes: an expression that names a column as a token is taken to depend on it. A column's own default is not listed: the column's change drops and re-binds it itself.

func (Planner) Diff added in v1.3.8

func (Planner) Diff(current, desired migrate.Schema) (engine.Diff, error)

Diff compares two schemas, which may be narrowed to some objects: a foreign key may reference a table the schema does not hold.

func (Planner) Exec added in v1.3.8

func (Planner) Inspect added in v1.3.8

func (Planner) Name added in v1.3.8

func (Planner) Name() string

func (Planner) Open added in v1.3.8

func (Planner) Preconditions added in v1.3.8

func (Planner) Preconditions(change migrate.Change, current, desired migrate.Schema, caps engine.Capabilities) ([]engine.Precondition, error)

Preconditions returns the guards a change needs: each is a query that returns a row when the data would make the change fail or lose data. They are table scans, and stop at the first offending row.

func (Planner) Reconcile added in v1.3.8

func (Planner) SameDependent added in v1.3.8

func (Planner) SameDependent(before, after engine.Dependent) (bool, error)

SameDependent is the strict comparison: SQL Server re-creates a dependent from the definition it recorded.

type ScriptOptions added in v1.1.3

type ScriptOptions struct {
	// Acknowledgements lists every destructive change ID exactly once.
	Acknowledgements []string
}

ScriptOptions are the reviewed inputs of RenderScript.

Jump to

Keyboard shortcuts

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