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

sqlserver

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 20 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.

Pure diffing and planning live in sqlglot-go/migrate/atlas/sqlserver. This driver supplies inspection, connection management, locks and SQL execution.

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 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 DefaultPlan migrate.PlanApplier = &planApply{conn: &conn{ExecQuerier: sqlx.NoRows}}

DefaultPlan plans without a connection and refuses statement execution.

View Source
var TypeRegistry = schemahcl.NewRegistry(
	schemahcl.WithFormatter(planner.FormatType),
	schemahcl.WithParser(planner.ParseType),
	schemahcl.WithSpecs(
		schemahcl.NewTypeSpec("bit"), schemahcl.NewTypeSpec("tinyint"), schemahcl.NewTypeSpec("smallint"),
		schemahcl.NewTypeSpec("int"), schemahcl.NewTypeSpec("bigint"),
		schemahcl.NewTypeSpec("decimal", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec("numeric", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
		schemahcl.NewTypeSpec("money"), schemahcl.NewTypeSpec("smallmoney"),
		schemahcl.NewTypeSpec("float", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())), schemahcl.NewTypeSpec("real"),
		sizedSpec("char"), sizedSpec("varchar"), sizedSpec("nchar"), sizedSpec("nvarchar"),
		schemahcl.NewTypeSpec("text"), schemahcl.NewTypeSpec("ntext"),
		sizedSpec("binary"), sizedSpec("varbinary"), schemahcl.NewTypeSpec("image"),
		schemahcl.NewTypeSpec("date"), schemahcl.NewTypeSpec("datetime"), schemahcl.NewTypeSpec("smalldatetime"),
		schemahcl.NewTypeSpec("time", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec("datetime2", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec("datetimeoffset", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr())),
		schemahcl.NewTypeSpec("uniqueidentifier"), schemahcl.NewTypeSpec("xml"),
		schemahcl.NewTypeSpec("sql_variant"), schemahcl.NewTypeSpec("rowversion"),
	),
)

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)

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.

Types

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

Jump to

Keyboard shortcuts

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