Documentation
¶
Overview ¶
Package migrate applies declarative Atlas HCL schemas to PostgreSQL, SQLite and SQL Server databases, and on SQL Server also the stored procedures and functions declared as .sql files (WithRoutines).
Index ¶
- func Apply(ctx context.Context, connection string, schemaFS fs.FS, opts ...Option) (returnErr error)
- func BookkeepingTables(dialect, schema string) ([]native.Name, error)
- func ConnectionForSchema(connection, schemaName string) (string, error)
- func ForgetRecordedSchemas(ctx context.Context, db *sql.DB, dialect string, schemas []string) error
- func PlanSchema(ctx context.Context, db *sql.DB, schemaFS fs.FS, opts ...Option) (engine.Artifact, error)
- func ValidateSchemaName(name string) error
- type Option
- func WithDB(db *sql.DB) Option
- func WithDir(dir string) Option
- func WithDrops() Option
- func WithExclude(patterns ...string) Option
- func WithName(name string) Option
- func WithReviewedPlan() Option
- func WithRoutines(dir string) Option
- func WithSchema(name string) Option
- func WithScriptPolicy(policy ScriptPolicy) Option
- func WithScriptSources(sources map[string]sharedscripts.Source) Option
- func WithVariables(input map[string]cty.Value) Option
- type PendingScript
- type Preview
- type SQLServerPending
- type SchemaProvisioner
- type ScriptAnalysis
- type ScriptPolicy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
func Apply(ctx context.Context, connection string, schemaFS fs.FS, opts ...Option) (returnErr error)
Apply resolves the database target from connection, or from the WithDB handle, and reconciles HCL schemas.
Apply is the unreviewed path: it plans the schema against the live database with the shared planner (engine.PlanLive) and applies the plan in the same call, acknowledging every destructive change of its own plan (engine.Acknowledgements). A table declaration is complete unless it says `partial = true`, so the members it omits are dropped: its indexes, keys and checks run, but the drop of a column does not, unless WithDrops. Each such column is reported as a warning every time, naming the three ways to settle it (declare it, mark the table partial, or WithDrops). The scope bounds the rest: declared by default, where an undeclared table or type is never dropped, and complete with WithDrops. Use the reviewed workflow when a person must approve the changes first.
On PostgreSQL and SQLite, Apply records the hash of the desired schema it brought the database to in schema_migration_schema, per bundle scope; the hash covers the evaluated HCL, dialect, schema, drop scope, exclusions and the bundle's script contents. The next Apply of an unchanged bundle skips inspecting and planning the schema (scripts still follow their own hashes, and a pre script that runs makes it plan), so a change made to the database by hand outside the bundle is not repaired, and an undeclared column is not reported again, until the bundle changes. The reviewed workflow forgets the ledger of the schemas it applies to (ForgetRecordedSchemas), so the next Apply after a reviewed deploy plans again.
PostgreSQL supports the complete HCL, SQL, security, and destructive-change lifecycle; the views that depend on a retyped column are dropped and re-created around the change from their live definitions. SQLite projects portable PostgreSQL-shaped HCL to tables, columns, and indexes, rebuilding tables when needed; undeclared tables are kept and undeclared columns are reported, not dropped, unless WithDrops is enabled. SQL Server (sqlserver:// URLs, ADO and odbc: strings) reconciles SQL Server HCL tables and the stored procedures and functions declared with WithRoutines, inspecting only the objects the bundle names; it refuses SQL scripts, security blocks, drops, rebuilds and schemas other than dbo.
func BookkeepingTables ¶ added in v1.7.1
BookkeepingTables names the tables a bundle keeps for itself in schema: the script and security ledgers and the schema ledger. A plan of a desired schema given outside a bundle (pasted HCL or a snapshot) never declares them, so it must leave them alone.
func ConnectionForSchema ¶
ConnectionForSchema returns a PostgreSQL URL whose connections select schemaName.
func ForgetRecordedSchemas ¶ added in v1.7.0
ForgetRecordedSchemas deletes the schema ledger of every bundle scope in the given schemas, so the next Apply of any bundle there plans against the database again instead of skipping. Every path that changes a PostgreSQL or SQLite schema without Apply (the reviewed workflow) calls it, before and after applying. SQL Server bundles keep no schema ledger.
func PlanSchema ¶ added in v1.3.8
func PlanSchema(ctx context.Context, db *sql.DB, schemaFS fs.FS, opts ...Option) (engine.Artifact, error)
PlanSchema is the reviewed schema plan of a SQL Server bundle on db: its HCL tables and WithRoutines routines against the live dbo schema, as the engine artifact Apply embeds in its plan file. It reads only the declared objects and what depends on the columns a change forces, never drops an object the bundle does not declare, and changes nothing. A table the bundle declares without `partial = true` is complete, so the plan holds the drops of its undeclared columns, indexes, keys and checks as destructive changes for a reviewer to acknowledge. The bundle's scripts are not loaded. The caller owns db.
func ValidateSchemaName ¶
ValidateSchemaName validates an application-normalized PostgreSQL schema identifier.
Types ¶
type Option ¶
type Option func(*options)
Option configures an HCL migration.
func WithDB ¶
WithDB runs the migration on an already open SQL Server, SQLite or PostgreSQL handle, with Apply's connection string left empty; the dialect comes from the handle's driver. Apply never closes db and never changes its pool settings. Every schema unit runs on a dedicated connection that sets and restores its own lock timeout, so a PostgreSQL handle needs no scoping; what it cannot do is move the script phases and metadata tables to another schema, so a PostgreSQL handle is accepted for the default schema only, and only when its current_schema() is that schema (WithSchema needs a connection string, which scopes search_path).
func WithDrops ¶
func WithDrops() Option
WithDrops makes the bundle the complete description of its schema: the plan is planned in the engine's complete scope, so anything the HCL does not declare (tables, types, columns, indexes, keys, checks) is dropped, and every drop it plans runs. Without it the scope is declared: only what the HCL names is managed, so an undeclared table or type is never dropped, and the columns a declared table lacks stay (they are reported, each time, as a warning). A table declared `partial = true` keeps every member it does not name, with or without WithDrops. The bundle's own metadata tables are never dropped.
func WithExclude ¶
WithExclude hides database objects from the plan: they are removed from the live schema and from the declared one before they are compared, so they are neither created, altered nor dropped. Values use Atlas's schema inspection patterns (for example "table" or "table.column"); SQLite accepts whole tables only, because it rebuilds a table from what the plan can see.
func WithName ¶
WithName sets the metadata scope used for SQL hashes and managed security. It should be stable and unique for each migration bundle sharing a database.
func WithReviewedPlan ¶ added in v1.3.8
func WithReviewedPlan() Option
WithReviewedPlan plans every change the scope allows, column drops included, for a reviewer to select and acknowledge. Without it a plan is what an unreviewed Apply runs, which leaves out the drop of a column of a declared table (unless WithDrops).
func WithRoutines ¶
WithRoutines declares stored procedures and functions from the .sql files under dir (relative to the schema filesystem root). Each file holds exactly one CREATE [OR ALTER] PROCEDURE|FUNCTION batch, optionally followed by GO, and is named after the routine it creates (mc_ClonePlan.sql creates mc_ClonePlan). The files are the routines' single source: they run as written, so the same file works in SSMS or sqlcmd, and they are never treated as hash-gated SQL scripts. Only SQL Server supports routines.
func WithSchema ¶
WithSchema selects the PostgreSQL schema used for the complete migration lifecycle. SQLite accepts only the default public schema declared by portable HCL bundles, and SQL Server only dbo.
func WithScriptPolicy ¶ added in v1.2.0
func WithScriptPolicy(policy ScriptPolicy) Option
WithScriptPolicy enables SQL Server dependency tracking for named script paths.
func WithScriptSources ¶ added in v1.3.3
func WithScriptSources(sources map[string]sharedscripts.Source) Option
WithScriptSources sets the metadata for scripts keyed by bundle-relative SQL path.
type PendingScript ¶ added in v1.1.2
type PendingScript struct {
Path string `json:"path"`
Phase string `json:"phase"`
Reason string `json:"reason"` // new | changed | dependency | always
Reasons []string `json:"reasons,omitempty"`
Blocked []string `json:"blocked,omitempty"`
Hash string `json:"hash,omitempty"`
}
PendingScript is one script Apply would run, and why: new or changed since it last ran, dependency (a script it depends on changed), or always (a `runs: always` script, which runs on every Apply and so is never a change by itself).
type Preview ¶
type Preview struct {
Artifact *engine.Artifact `json:"artifact,omitempty"`
Advisory []string `json:"advisory,omitempty"`
Scripts []ScriptAnalysis `json:"scripts,omitempty"`
ScriptPlan *scriptserver.PlanFile `json:"scriptPlan,omitempty"`
Warnings []string `json:"warnings,omitempty"`
Error string `json:"error,omitempty"`
}
Preview is what PreviewBundle plans. For PostgreSQL and SQLite, Artifact is the engine's reviewed artifact: its changes (forced rebuilds carry their cause), preserved drops, refusals, warnings and units with their ordered statements and preconditions. Without WithReviewedPlan it is what an unreviewed Apply would run: the drops of undeclared columns are not in it (Warnings names each one), so it holds no change when nothing else is left to run. A non-empty Advisory marks a bundle whose scripts or security blocks the artifact does not carry, so it must not be applied as the whole migration.
func PreviewBundle ¶
func PreviewBundle(ctx context.Context, db *sql.DB, dialect string, files fs.FS, opts ...Option) (result Preview, returnErr error)
PreviewBundle inspects and plans without executing scripts or creating metadata. Script and security bundles yield advisory schema plans until phased artifacts are supported.
type SQLServerPending ¶ added in v1.1.2
type SQLServerPending struct {
Statements []string `json:"statements,omitempty"`
Scripts []PendingScript `json:"scripts,omitempty"`
// Warnings name each column of a declared table that is in the database but
// not declared: Apply does not drop it and reports it every time.
Warnings []string `json:"warnings,omitempty"`
Plan *backend.PlanFile `json:"plan"`
}
SQLServerPending is what Apply would do to a SQL Server database: the statements that reconcile the declared tables and routines (every unit's statements in order, derived from the plan file's schema artifact), the scripts it would run, and the plan file holding both. It inspects only the objects the bundle names.
func PendingSQLServer ¶ added in v1.1.2
func PendingSQLServer(ctx context.Context, db *sql.DB, schemaFS fs.FS, opts ...Option) (SQLServerPending, error)
PendingSQLServer plans Apply on db without executing or recording anything.
func (SQLServerPending) Changed ¶ added in v1.1.2
func (p SQLServerPending) Changed() bool
Changed reports whether Apply would change anything beyond running its `runs: always` scripts.
type SchemaProvisioner ¶
type SchemaProvisioner struct {
// contains filtered or unexported fields
}
SchemaProvisioner prepares reusable dbtest templates and reconciles each cloned database using the same migration behavior as Apply.
func NewProvisioner ¶
func NewProvisioner(schemaFS fs.FS, opts ...Option) *SchemaProvisioner
NewProvisioner adapts an HCL migration bundle to dbtest.Provisioner without coupling the migrate package to dbtest. Options are frozen at construction.
func (*SchemaProvisioner) Fingerprint ¶
func (p *SchemaProvisioner) Fingerprint(ctx context.Context) (string, error)
Fingerprint returns a deterministic identity for all inputs that affect the database produced by this provisioner.
func (*SchemaProvisioner) PrepareInstance ¶
func (p *SchemaProvisioner) PrepareInstance(ctx context.Context, connection string) error
PrepareInstance reconciles instance-specific and always-run migrations after cloning a prepared template.
func (*SchemaProvisioner) PrepareTemplate ¶
func (p *SchemaProvisioner) PrepareTemplate(ctx context.Context, connection string) error
PrepareTemplate applies the full migration bundle to a new template.
type ScriptAnalysis ¶
type ScriptAnalysis struct {
Path string `json:"path,omitempty"`
Phase string `json:"phase,omitempty"`
Kind string `json:"kind"`
Complete bool `json:"complete"`
Diagnostics []sqlglot.ParseDiagnostic `json:"diagnostics,omitempty"`
Error string `json:"error,omitempty"`
}
func AnalyzeScript ¶
func AnalyzeScript(text, dialect string) ScriptAnalysis
type ScriptPolicy ¶ added in v1.2.0
Source Files
¶
- column_drops.go
- hcl_schema_blocks.go
- migrate.go
- postgres_target.go
- preview.go
- preview_sqlserver.go
- provisioner.go
- routines.go
- schema_ledger.go
- schema_scope.go
- schema_step.go
- script_analysis.go
- script_bundle.go
- script_policy.go
- scripts.go
- scripts_sqlserver.go
- security.go
- security_hcl.go
- sqlite_target.go
- sqlserver.go
- sqlserver_schema.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dialect is what the reviewed workflows need from the registered engine dialects besides planning: the desired schema a dialect reads from HCL, a schema snapshot, and the script a plan's statements make.
|
Package dialect is what the reviewed workflows need from the registered engine dialects besides planning: the desired schema a dialect reads from HCL, a schema snapshot, and the script a plan's statements make. |
|
atlasdriver
Package atlasdriver is the shared engine.Dialect implementation for the dialects that plan with an upstream Atlas driver (Postgres and SQLite).
|
Package atlasdriver is the shared engine.Dialect implementation for the dialects that plan with an upstream Atlas driver (Postgres and SQLite). |
|
postgres
Package postgres registers the "postgres" engine dialect, planned with the upstream Atlas PostgreSQL driver.
|
Package postgres registers the "postgres" engine dialect, planned with the upstream Atlas PostgreSQL driver. |
|
sqlite
Package sqlite registers the "sqlite" engine dialect, planned with the upstream Atlas SQLite driver.
|
Package sqlite registers the "sqlite" engine dialect, planned with the upstream Atlas SQLite driver. |
|
sqlserver
Package sqlserver is the database half of the SQL Server dialect of the schema engine: it reads capabilities, schemas and the live dependents of columns, and runs a unit on one connection.
|
Package sqlserver is the database half of the SQL Server dialect of the schema engine: it reads capabilities, schemas and the live dependents of columns, and runs a unit on one connection. |
|
Package scripts owns migration source identity, dependencies and consumed fingerprints.
|
Package scripts owns migration source identity, dependencies and consumed fingerprints. |
|
sqlserver
Package sqlserver plans and applies dependency-aware scripts on SQL Server through a self-contained plan file: Plan observes the target once and writes what apply will do, and Apply runs exactly that plan, refusing it as stale when the target moved since.
|
Package sqlserver plans and applies dependency-aware scripts on SQL Server through a self-contained plan file: Plan observes the target once and writes what apply will do, and Apply runs exactly that plan, refusing it as stale when the target moved since. |
|
Package sqlite reconciles SQLite tables with programmatic Atlas declarations.
|
Package sqlite reconciles SQLite tables with programmatic Atlas declarations. |