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
- func BuildArtifact(input migrate.ArtifactRequest) (migrate.Artifact, error)
- func FromAtlas(input *schema.Realm) (migrate.Schema, error)
- func Plan(request migrate.Request) (migrate.Plan, error)
- func RenderScript(a engine.Artifact, options ScriptOptions) (string, error)
- func ValidateArtifact(artifact migrate.Artifact) error
- type HCLFile
- type HCLLayout
- type HCLOptions
- type Planner
- func (Planner) Capabilities(_ context.Context, q engine.Querier) (engine.Capabilities, error)
- func (Planner) Dependents(_ context.Context, q engine.Querier, current migrate.Schema, ...) ([]engine.Dependent, error)
- func (Planner) Diff(current, desired migrate.Schema) (engine.Diff, error)
- func (Planner) Exec(context.Context, engine.Querier, engine.Statement) error
- func (Planner) Inspect(context.Context, engine.Querier, engine.Scope) (engine.Inspection, error)
- func (Planner) Name() string
- func (Planner) Open(context.Context, *sql.Conn, engine.SessionOptions) (engine.UnitSession, error)
- func (Planner) Preconditions(change migrate.Change, current, desired migrate.Schema, ...) ([]engine.Precondition, error)
- func (Planner) Reconcile(context.Context, engine.Querier, migrate.Schema, migrate.Schema) (migrate.Schema, error)
- func (Planner) SameDependent(before, after engine.Dependent) (bool, error)
- type ScriptOptions
Constants ¶
const FeatureTryConvert = "tryConvert"
FeatureTryConvert is the capability feature that says TRY_CONVERT exists, which is database compatibility level 110 and above.
const LockResource = "commons-db/migrate/schema"
LockResource is the applock that serialises every migration of a database, applied directly or through a rendered script.
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 BuildArtifact ¶ added in v1.0.5
func BuildArtifact(input migrate.ArtifactRequest) (migrate.Artifact, error)
BuildArtifact plans a v1 artifact whose snapshots are limited to input.Schemas and binds the plan to them with a hash.
func FromAtlas ¶ added in v1.0.5
FromAtlas projects supported declarative SQL Server tables and routines into the native model.
func Plan ¶
Plan is the v1 offline planner: it plans the request with the engine and the pure Planner, so no dialect has to be registered, and flattens the ordered units into one step per statement.
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.
func ValidateArtifact ¶ added in v1.0.5
ValidateArtifact checks the version and hash, then rebuilds the artifact from its own snapshots and requires the same hash, so edited SQL is refused.
Types ¶
type HCLFile ¶ added in v1.3.8
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.
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
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
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) Open ¶ added in v1.3.8
func (Planner) Open(context.Context, *sql.Conn, engine.SessionOptions) (engine.UnitSession, error)
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.
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.
Source Files
¶
- changes.go
- convert.go
- dependencies.go
- dialect.go
- dialect_dependents.go
- dialect_diff.go
- dialect_forcing.go
- dialect_lower.go
- dialect_order.go
- dialect_preconditions.go
- dialect_rebuild.go
- dialect_render.go
- dialect_stub.go
- export_hcl.go
- from_atlas.go
- guard.go
- guard_state.go
- modules.go
- plan_v1.go
- planner.go
- script.go
- script_session.go
- script_statement.go
- sqltext.go