Documentation
¶
Index ¶
- Constants
- func RegisterMigration(up, down GoMigrationFunc)
- func RegisterNamedMigration(filename string, up, down GoMigrationFunc)
- func ResetGlobalMigrations()
- type GoMigrationFunc
- type Loader
- type Migration
- type MigrationSource
- type MigrationSourceType
- type Migrator
- func (m *Migrator) Down(ctx context.Context) error
- func (m *Migrator) DownTo(ctx context.Context, target uint64) error
- func (m *Migrator) Reset(ctx context.Context) error
- func (m *Migrator) SetDryRun(enabled bool)
- func (m *Migrator) Status(ctx context.Context) error
- func (m *Migrator) Up(ctx context.Context) error
- func (m *Migrator) UpTo(ctx context.Context, target uint64) error
- type Store
- type StoreConfig
Constants ¶
const ( MigrationDirectionUp string = "up" MigrationDirectionDown string = "down" )
const (
DefaultTableName = "migration_versions"
)
Variables ¶
This section is empty.
Functions ¶
func RegisterMigration ¶
func RegisterMigration(up, down GoMigrationFunc)
RegisterMigration registers a Go migration using the caller's filename to derive the version number. The filename must start with a numeric prefix (e.g. 20250317141923_create_users.go).
Panics if the version conflicts with an already-registered migration.
func RegisterNamedMigration ¶
func RegisterNamedMigration(filename string, up, down GoMigrationFunc)
RegisterNamedMigration registers a Go migration with an explicit filename. The version is parsed from the leading numeric component of the base filename (e.g. "20250317141923_create_users.go" → version 20250317141923).
Panics if the version conflicts with an already-registered migration.
func ResetGlobalMigrations ¶
func ResetGlobalMigrations()
ResetGlobalMigrations clears all registered Go migrations. Intended for use in tests.
Types ¶
type GoMigrationFunc ¶
type GoMigrationFunc func(ctx context.Context, conn clickhouse.Conn) error
GoMigrationFunc is a Go migration function that receives a ClickHouse connection. ClickHouse has no transaction support, so the function operates directly on the connection.
type Loader ¶
Loader loads migration definitions from a source.
func NewGoLoader ¶
func NewGoLoader() Loader
NewGoLoader returns a Loader that reads migrations from the global Go migration registry populated by RegisterMigration / RegisterNamedMigration.
func NewSQLLoader ¶
NewSQLLoader returns a clicko.Loader that reads SQL migration files from dir.
type Migration ¶
type Migration struct {
Version uint64
Description string
AppliedAt time.Time
Source MigrationSource
}
Migration represents a single migration.
type MigrationSource ¶
type MigrationSource struct {
Type MigrationSourceType
UpSQL string
DownSQL string
UpFunc GoMigrationFunc
DownFunc GoMigrationFunc
}
MigrationSource holds the migration's executable content — either SQL strings loaded from files, Go functions registered programmatically, or both. When both are set, Go functions take precedence.
func (MigrationSource) HasDown ¶
func (s MigrationSource) HasDown() bool
HasDown reports whether this migration source has a down (rollback) definition. Migrations without a down definition are treated as forward-only and skipped during rollback operations without error.
type MigrationSourceType ¶
type MigrationSourceType string
const ( MigrationSourceTypeSQL MigrationSourceType = "sql" MigrationSourceTypeGo MigrationSourceType = "go" )
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator orchestrates loading migrations (SQL files or Go functions), comparing them against the applied state in ClickHouse, and executing them.
func New ¶
func New(conn clickhouse.Conn, cfg StoreConfig) (*Migrator, error)
New creates a Migrator for Go-based migrations. It sets up the version-tracking store and Go migration loader internally.
func NewMigrator ¶
func NewMigrator(conn clickhouse.Conn, loader Loader, store Store) *Migrator
NewMigrator creates a Migrator with the given connection, loader, and store. For most use cases, prefer New, which wires up the store and Go loader automatically.
func (*Migrator) DownTo ¶
DownTo reverts all applied migrations down to (but not including) the target version.
func (*Migrator) SetDryRun ¶
SetDryRun enables or disables dry-run mode. When enabled, commands print the SQL each migration would execute instead of running it.
func (*Migrator) Status ¶
Status prints a table showing each migration's version, description, status, and when it was applied.
type Store ¶
type Store interface {
EnsureTable(ctx context.Context) error
GetAppliedVersions(ctx context.Context) (map[uint64]*Migration, error)
Add(ctx context.Context, version uint64, description string) error
Remove(ctx context.Context, version uint64) error
}
Store provides read/write access to the migration state stored in ClickHouse.
func NewStore ¶
func NewStore(conn clickhouse.Conn, config StoreConfig) (Store, error)
NewStore creates a Store backed by the given ClickHouse connection. Returns an error if any config value fails validation.
type StoreConfig ¶
type StoreConfig struct {
TableName string
Cluster string
CustomEngine string
// InsertQuorum controls the insert_quorum setting for migration writes in cluster mode.
// Set this to the total number of nodes in the cluster (shards × replicas per shard)
// so every node must acknowledge the write before it is considered successful.
// This is necessary because the migration table is replicated across all nodes via a single
// ZooKeeper path — a node that missed the write would report the migration as not applied.
// Accepts a positive integer (e.g. "6" for 3 shards × 2 replicas) or "auto".
// Has no effect when Cluster is not set.
// https://clickhouse.com/docs/operations/settings/settings#insert_quorum
InsertQuorum string
}
StoreConfig holds configuration for the migration state store. All string values that are interpolated into SQL (TableName, Cluster, InsertQuorum) are validated in NewStore to prevent injection.
func (StoreConfig) IsCluster ¶
func (c StoreConfig) IsCluster() bool
func (StoreConfig) ResolveEngine ¶
func (c StoreConfig) ResolveEngine() string
ResolveEngine returns the engine clause to use when creating the migration table. Priority: CustomEngine > ReplicatedMergeTree (cluster, with warning) > MergeTree.