Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlserver

package
v0.1.61 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package sqlserver is an Atlas driver for Microsoft SQL Server.

It covers what declarative migrations of a shared database need: tables, columns, primary keys, indexes, foreign keys and checks from HCL, and stored procedures and functions from their T-SQL definitions, parsed and compared through sqlglot. Views, triggers, sequences, user-defined types, partitioning and computed columns are refused with an error, and versioned migration directories (revisions, snapshots, clean checks) are not supported.

Inspection is scoped: InspectOptions.Tables names the objects to read -- tables and routines alike, since SQL Server keeps both in one namespace per schema -- and Mode picks tables (InspectTables) and/or routines (InspectFuncs). Generated SQL runs at compatibility level 100.

Index

Constants

View Source
const Dialect = "tsql"

Dialect is the sqlglot dialect the driver parses T-SQL with.

View Source
const DriverName = "sqlserver"

DriverName holds the name used for registration.

Variables

View Source
var (

	// MarshalHCL marshals v into an Atlas HCL DDL document.
	MarshalHCL = schemahcl.MarshalerFunc(codec.MarshalSpec)
	// EvalHCL implements the schemahcl.Evaluator interface.
	EvalHCL = schemahcl.EvalFunc(codec.Eval)
	// EvalHCLBytes evaluates an HCL document from a byte slice.
	EvalHCLBytes = specutil.HCLBytesFunc(EvalHCL)
)
View Source
var DefaultDiff schema.Differ = &sqlx.Diff{DiffDriver: &diff{}}

DefaultDiff provides basic diffing capabilities for SQL Server. A Driver opened on a connection exposes the same Differ.

View Source
var DefaultPlan migrate.PlanApplier = &planApply{conn: &conn{ExecQuerier: sqlx.NoRows}}

DefaultPlan provides basic planning capabilities for SQL Server. A Driver opened on a connection exposes the same PlanApplier.

TypeRegistry holds the SQL Server HCL type specs. A (max) size is written with sql(), e.g. `type = sql("nvarchar(max)")`.

Functions

func CatalogType

func CatalogType(c mssqlcatalog.Column) (schema.Type, error)

CatalogType maps a catalog column to its schema.Type from the raw sys.columns widths: bytes for string and binary types (halved for the n-types), -1 for (max), precision and scale for decimals and floats, and the fractional-second digits as the scale of time types.

func CreateOrAlter

func CreateOrAlter(definition string) (string, error)

CreateOrAlter rewrites a CREATE PROCEDURE|FUNCTION definition to CREATE OR ALTER, splicing the two words in after the CREATE keyword so every other byte of the definition -- comments, layout, literals -- is kept.

func FormatType

func FormatType(t schema.Type) (string, error)

FormatType writes a schema.Type in its canonical T-SQL spelling, so two spellings of one type format the same.

func Open

Open opens a new SQL Server driver, reading the engine version and the database compatibility level.

func OpenClient

func OpenClient(ctx context.Context, db *sql.DB) (*sqlclient.Client, error)

OpenClient wraps an open database/sql handle, whatever DSN form it was opened with (URL, ADO or ODBC), in an Atlas client. Closing the client closes db.

func OpenTx

func OpenTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sqlclient.Tx, error)

OpenTx opens a transaction.

func ParseType

func ParseType(s string) (schema.Type, error)

ParseType reads a declared T-SQL data type (`nvarchar(max)`, `decimal(18,2)`) through sqlglot into its schema.Type.

Types

type Clustered

type Clustered struct {
	schema.Attr
}

Clustered marks a clustered index or primary key. Its absence means nonclustered.

type Codec

type Codec struct {
	State *schemahcl.State
}

Codec for schemahcl.

func (*Codec) Eval

func (c *Codec) Eval(p *hclparse.Parser, v any, input map[string]cty.Value) error

Eval evaluates an Atlas DDL document into v using the input.

func (*Codec) EvalOptions

func (c *Codec) EvalOptions(p *hclparse.Parser, v any, opts *schemahcl.EvalOptions) error

EvalOptions decodes the HCL with the given options into a *schema.Realm or a *schema.Schema.

func (*Codec) MarshalSpec

func (c *Codec) MarshalSpec(v any) ([]byte, error)

MarshalSpec marshals v into an Atlas DDL document. Routines are left out: their single source is the .sql definition.

type DefaultConstraint

type DefaultConstraint struct {
	schema.Attr
	Name string
}

DefaultConstraint names the constraint holding a column default. The inspector sets it from the catalog; the planner names new defaults DF_<table>_<column>.

type Definition

type Definition struct {
	schema.Attr
	Text string
}

Definition is the T-SQL text a routine is created from: the declared .sql batch for a desired routine, sys.sql_modules.definition for a live one. The differ compares definitions by their sqlglot canonical form, and the planner executes a declared definition as written.

type Driver

type Driver struct {
	schema.Differ
	schema.Inspector
	migrate.PlanApplier
	// contains filtered or unexported fields
}

Driver represents a SQL Server driver for introspecting database schemas, generating diff between schema elements and applying migration changes.

func (*Driver) CheckClean

func (d *Driver) CheckClean(context.Context, *migrate.TableIdent) error

CheckClean implements migrate.CleanChecker and is not supported.

func (*Driver) CompatibilityLevel

func (d *Driver) CompatibilityLevel() int

CompatibilityLevel is the connected database's compatibility_level.

func (*Driver) FormatType

func (*Driver) FormatType(t schema.Type) (string, error)

FormatType converts schema type to its column form in the database.

func (*Driver) Lock

func (d *Driver) Lock(ctx context.Context, name string, timeout time.Duration) (schema.UnlockFunc, error)

Lock implements schema.Locker with a session-owned sp_getapplock on a dedicated connection. A negative timeout waits forever and zero tries once.

func (*Driver) ParseType

func (*Driver) ParseType(s string) (schema.Type, error)

ParseType returns the schema.Type value represented by the given string.

func (*Driver) Snapshot

func (d *Driver) Snapshot(context.Context) (migrate.RestoreFunc, error)

Snapshot implements migrate.Snapshoter and is not supported.

func (*Driver) StmtBuilder

func (*Driver) StmtBuilder(opts migrate.PlanOptions) *sqlx.Builder

StmtBuilder is a helper method used to build statements with SQL Server formatting: bracket-quoted identifiers.

func (*Driver) Version

func (d *Driver) Version() string

Version is the engine's ProductVersion, e.g. 16.0.4135.4.

type Identity

type Identity struct {
	schema.Attr
	Seed, Increment int64
}

Identity is an IDENTITY(seed, increment) column.

type IndexInclude

type IndexInclude struct {
	schema.Attr
	Columns []string
}

IndexInclude lists the non-key columns an index INCLUDEs.

type IndexPredicate

type IndexPredicate struct {
	schema.Attr
	P string
	// contains filtered or unexported fields
}

IndexPredicate is a filtered index's WHERE clause.

type Routine

type Routine struct {
	// Kind is PROCEDURE or FUNCTION.
	Kind string
	// Schema is the schema the definition names, empty when unqualified.
	Schema string
	Name   string
	// Text is the definition as written.
	Text string
	// contains filtered or unexported fields
}

Routine is one CREATE PROCEDURE or CREATE FUNCTION read from its definition.

func ParseRoutine

func ParseRoutine(definition string) (*Routine, error)

ParseRoutine parses a single CREATE [OR ALTER] PROCEDURE|FUNCTION batch. A batch holding anything else, or more than one statement, is an error.

func (*Routine) AddTo

func (r *Routine) AddTo(s *schema.Schema) error

AddTo adds the routine to s as a schema.Proc or schema.Func carrying its Definition. A routine qualified with another schema is an error.

type SpecialType

type SpecialType struct {
	schema.Type
	T string
}

SpecialType is a SQL Server type with no generic Atlas counterpart: money, smallmoney, xml, sql_variant and rowversion.

type UniqueConstraint

type UniqueConstraint struct {
	schema.Attr
}

UniqueConstraint marks a live unique index that backs a UNIQUE constraint, which is dropped with ALTER TABLE ... DROP CONSTRAINT.

Jump to

Keyboard shortcuts

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