migrate

package
v0.1.25 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

README

HCL migrations

migrate.Apply reconciles PostgreSQL tables with Atlas HCL and runs colocated SQL migrations. Every bundle should use a stable WithName when more than one bundle can share a database.

SQL files default to the post-Atlas phase. Directives are read from the leading comment header:

-- phase: pre
-- dependsOn: 001_extensions.sql
-- runs: always
-- transaction: false
  • phase is pre or post; omitted means post.
  • dependsOn uses paths relative to the migration root. Dependencies are topologically ordered and changes rerun all transitive dependents.
  • scripts are transactional by default; the SQL and its SHA-256 migration log commit atomically.
  • runs: always re-runs a script on every apply, including a plain process boot against an unchanged database. It is an escape hatch, not the norm. Normal scripts are hash-gated: they run once and re-run only when their content changes. Views are restored automatically when Atlas reshapes a base table they depend on — migrate.Apply drops the dependent view and re-runs the script that creates it — so a view file never needs runs: always to stay current. Reserving always for the rare script that must reconcile on every apply keeps steady-state boots DDL-free; a bundle full of always scripts re-issues ACCESS EXCLUSIVE-locking DDL on every connection and will deadlock live readers. Prefer splitting DDL into small hash-gated files — one file per view (or per shared dependency), triggers and constraints in their own run-once files — so a table reshape only re-creates the views that actually depend on it and never disturbs unrelated triggers or constraints.

Atlas-style PostgreSQL role and permission blocks may live in the same HCL files as tables. The OSS migration layer supports roles and memberships plus schema, table, column, and sequence permissions. Managed grants are reconciled exactly within the bundle's metadata scope; unrelated roles and grants are left untouched. Different scopes must not manage the same role or grantee/object pair.

Documentation

Overview

Package migrate applies declarative Atlas HCL schemas to PostgreSQL databases.

Index

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) error

Apply loads colocated HCL and SQL migrations from schemaFS. SQL scripts marked phase pre run before the Atlas realm diff; all other SQL defaults to the post phase. Declared PostgreSQL roles and permissions are reconciled last. Tables absent from a partial schema bundle are never dropped unless WithTableDrops is supplied.

Types

type Option

type Option func(*options)

Option configures an HCL migration.

func WithDir

func WithDir(dir string) Option

WithDir selects the root containing colocated HCL and SQL migrations.

func WithExclude

func WithExclude(patterns ...string) Option

WithExclude excludes database objects from Atlas inspection. Values use Atlas's schema inspection patterns (for example "table.column").

func WithName

func WithName(name string) Option

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 WithTableDrops

func WithTableDrops() Option

WithTableDrops allows HCL files to remove tables. Drops are suppressed by default so a partial schema bundle cannot delete tables owned by consumers.

func WithVariables

func WithVariables(input map[string]cty.Value) Option

WithVariables supplies values for HCL variable blocks and security expressions.

Directories

Path Synopsis
Package viewdeps finds, drops, and restores the views and materialized views that transitively depend on a set of tables, so DDL PostgreSQL refuses while a dependent view exists — DROP TABLE, DROP COLUMN, ALTER COLUMN TYPE — can proceed.
Package viewdeps finds, drops, and restores the views and materialized views that transitively depend on a set of tables, so DDL PostgreSQL refuses while a dependent view exists — DROP TABLE, DROP COLUMN, ALTER COLUMN TYPE — can proceed.

Jump to

Keyboard shortcuts

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