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
- Variables
- func CatalogType(c mssqlcatalog.Column) (schema.Type, error)
- func CreateOrAlter(definition string) (string, error)
- func FormatType(t schema.Type) (string, error)
- func Open(db schema.ExecQuerier) (migrate.Driver, error)
- func OpenClient(ctx context.Context, db *sql.DB) (*sqlclient.Client, error)
- func OpenTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sqlclient.Tx, error)
- func ParseType(s string) (schema.Type, error)
- type Clustered
- type Codec
- type DefaultConstraint
- type Definition
- type Driver
- func (d *Driver) CheckClean(context.Context, *migrate.TableIdent) error
- func (d *Driver) CompatibilityLevel() int
- func (*Driver) FormatType(t schema.Type) (string, error)
- func (d *Driver) Lock(ctx context.Context, name string, timeout time.Duration) (schema.UnlockFunc, error)
- func (*Driver) ParseType(s string) (schema.Type, error)
- func (d *Driver) Snapshot(context.Context) (migrate.RestoreFunc, error)
- func (*Driver) StmtBuilder(opts migrate.PlanOptions) *sqlx.Builder
- func (d *Driver) Version() string
- type Identity
- type IndexInclude
- type IndexPredicate
- type Routine
- type SpecialType
- type UniqueConstraint
Constants ¶
const Dialect = "tsql"
Dialect is the sqlglot dialect the driver parses T-SQL with.
const DriverName = "sqlserver"
DriverName holds the name used for registration.
Variables ¶
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) )
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.
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.
var TypeRegistry = schemahcl.NewRegistry( schemahcl.WithFormatter(FormatType), schemahcl.WithParser(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)
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 ¶
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 ¶
FormatType writes a schema.Type in its canonical T-SQL spelling, so two spellings of one type format the same.
func Open ¶
func Open(db schema.ExecQuerier) (migrate.Driver, error)
Open opens a new SQL Server driver, reading the engine version and the database compatibility level.
func OpenClient ¶
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.
Types ¶
type Codec ¶
Codec for schemahcl.
func (*Codec) EvalOptions ¶
EvalOptions decodes the HCL with the given options into a *schema.Realm or a *schema.Schema.
type DefaultConstraint ¶
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 ¶
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 ¶
CheckClean implements migrate.CleanChecker and is not supported.
func (*Driver) CompatibilityLevel ¶
CompatibilityLevel is the connected database's compatibility_level.
func (*Driver) FormatType ¶
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) 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.
type IndexInclude ¶
IndexInclude lists the non-key columns an index INCLUDEs.
type IndexPredicate ¶
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 ¶
ParseRoutine parses a single CREATE [OR ALTER] PROCEDURE|FUNCTION batch. A batch holding anything else, or more than one statement, is an error.
type SpecialType ¶
SpecialType is a SQL Server type with no generic Atlas counterpart: money, smallmoney, xml, sql_variant and rowversion.
type UniqueConstraint ¶
UniqueConstraint marks a live unique index that backs a UNIQUE constraint, which is dropped with ALTER TABLE ... DROP CONSTRAINT.