Documentation
¶
Index ¶
- func Prefix(name string) string
- func ValidateClickHouseMigrations(ctx context.Context, config *ClickHouseConfig, fs embed.FS) error
- func ValidatePostgresMigrations(ctx context.Context, db *sql.DB, sources ...MigrationSource) error
- type ClickHouse
- func (c *ClickHouse) Applied(ctx context.Context) ([]string, error)
- func (c *ClickHouse) Apply(ctx context.Context, m Migration) error
- func (c *ClickHouse) ApplyMigrations(ctx context.Context, migrations []Migration) error
- func (c *ClickHouse) Close() error
- func (c *ClickHouse) Lock(ctx context.Context) error
- func (c *ClickHouse) Setup(ctx context.Context) error
- func (c *ClickHouse) Unlock(ctx context.Context) error
- func (c *ClickHouse) ValidateAllApplied(ctx context.Context, migrations []Migration) error
- type ClickHouseConfig
- type Migration
- type MigrationSource
- type Postgres
- func (p *Postgres) Applied(ctx context.Context) ([]string, error)
- func (p *Postgres) Apply(ctx context.Context, m Migration) error
- func (p *Postgres) ApplyMigrations(ctx context.Context, migrations []Migration) error
- func (p *Postgres) Close() error
- func (p *Postgres) Lock(ctx context.Context) error
- func (p *Postgres) Setup(ctx context.Context) error
- func (p *Postgres) Unlock(ctx context.Context) error
- func (p *Postgres) ValidateAllApplied(ctx context.Context, migrations []Migration) error
- func (p *Postgres) WithSchema(schema string) *Postgres
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Prefix ¶
Prefix extracts numeric prefix from migration filenames and normalizes it. Supports both underscore and hyphen separators. Examples:
"001_create_users.up.sql" -> "1" "1-create-users.up.sql" -> "1" "0042_add_field.up.sql" -> "42"
func ValidateClickHouseMigrations ¶
ValidateClickHouseMigrations validates ClickHouse migrations. Returns an error if any migrations are pending.
func ValidatePostgresMigrations ¶
ValidatePostgresMigrations validates multiple Postgres migration sources at once. Returns an error if any migrations are pending.
Types ¶
type ClickHouse ¶
type ClickHouse struct {
// contains filtered or unexported fields
}
ClickHouse handles ClickHouse migrations via native protocol
func NewClickHouse ¶
func NewClickHouse(config *ClickHouseConfig) *ClickHouse
NewClickHouse creates a ClickHouse migrator from config. Uses native protocol for all connections.
func (*ClickHouse) Applied ¶
func (c *ClickHouse) Applied(ctx context.Context) ([]string, error)
Applied returns list of applied migrations
func (*ClickHouse) Apply ¶
func (c *ClickHouse) Apply(ctx context.Context, m Migration) error
Apply applies a migration with exponential backoff retry for transient errors
func (*ClickHouse) ApplyMigrations ¶
func (c *ClickHouse) ApplyMigrations(ctx context.Context, migrations []Migration) error
ApplyMigrations applies all unapplied migrations (only locks if needed) Automatically calls Setup() to ensure migration tables exist before proceeding.
func (*ClickHouse) Lock ¶
func (c *ClickHouse) Lock(ctx context.Context) error
Lock acquires a global database-wide migration lock All apps share the same lock (lock_name='global') to prevent concurrent ClickHouse migrations This is necessary because ON CLUSTER operations modify distributed DDL queue across all nodes
func (*ClickHouse) Setup ¶
func (c *ClickHouse) Setup(ctx context.Context) error
Setup ensures database and tables exist
func (*ClickHouse) Unlock ¶
func (c *ClickHouse) Unlock(ctx context.Context) error
Unlock releases the global lock
func (*ClickHouse) ValidateAllApplied ¶
func (c *ClickHouse) ValidateAllApplied(ctx context.Context, migrations []Migration) error
ValidateAllApplied checks if all provided migrations have been applied. Returns an error listing any pending migrations if validation fails. This is intended for use during application startup to ensure the database schema is up-to-date before the app starts serving requests.
type ClickHouseConfig ¶
type ClickHouseConfig struct {
ClientAddr string // Native protocol address (e.g., clickhouse:9000)
Database string
Username string
Password string
App string
Cluster string // Optional; if specified, uses ON CLUSTER for DDL statements
// Required. ClickHouse migration tracking + locking is done in Postgres:
// - tracking: Postgres public.migrations with database='clickhouse'
// - locking: Postgres advisory locks
//
// This intentionally avoids ClickHouse-based migration tables (`migrations`, `migration_locks`)
// which are awkward to restore/merge and are not a good fit for authoritative state.
PostgresDB *sql.DB
}
ClickHouseConfig holds configuration for ClickHouse migrations
type MigrationSource ¶
MigrationSource represents a migration source with an app name and embedded filesystem
type Postgres ¶
type Postgres struct {
// contains filtered or unexported fields
}
Postgres handles PostgreSQL migrations
func NewPostgres ¶
NewPostgres creates a Postgres migrator
func (*Postgres) ApplyMigrations ¶
ApplyMigrations applies all unapplied migrations (only locks if needed) Automatically calls Setup() to ensure migration tables exist before proceeding.
func (*Postgres) Lock ¶
Lock acquires a global advisory lock for migrations This blocks until the lock is available (no polling needed) The lock is automatically released when the connection closes
func (*Postgres) ValidateAllApplied ¶
ValidateAllApplied checks if all provided migrations have been applied. Returns an error listing any pending migrations if validation fails. This is intended for use during application startup to ensure the database schema is up-to-date before the app starts serving requests.
func (*Postgres) WithSchema ¶
WithSchema configures the schema to target for unqualified DDL/DML in migrations (via SET LOCAL search_path). This allows embedded subsystems to create tables in the host application's schema (River-style).