goose_clickhouse_store

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Goose ClickHouse Store

Go Test codecov Go Version Release GoDoc License

This package provides a Goose store implementation for ClickHouse database and add a distributed migrations table feature.

Local migrations table:

CREATE TABLE IF NOT EXISTS db.migrations_part ON CLUSTER default (
	version_id Int64, 
	is_applied UInt8, 
	date Date DEFAULT now(), 
	tstamp DateTime DEFAULT now()
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/dbname/migrations', '{replica}') ORDER BY version_id

Distributed migrations table:

CREATE TABLE IF NOT EXISTS db.migrations ON CLUSTER default AS db.migrations_part
    ENGINE = Distributed(default, 'db', 'migrations_part', rand())

Reads and inserts go through the distributed table. Deletes (goose down) run against the local table on every cluster node, because ClickHouse does not support mutations on the Distributed engine:

ALTER TABLE db.migrations_part ON CLUSTER default DELETE WHERE version_id = ? SETTINGS mutations_sync = 2

mutations_sync is set from DistributedMigrationsTableConfig.MutationsSync (0 - async, 1 - wait for the current server, 2 - wait for all replicas; default 2).

Download

go get -u github.com/Kachit/goose-clickhouse-store

Usage

You can find an example here.

Coverage

go test --coverprofile=coverage.out ./... ; go tool cover -func coverage.out ; go tool cover --html=coverage.out -o coverage.html

Linters

golangci-lint run

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DistributedMigrationsTableConfigByDefault = DistributedMigrationsTableConfig{
	Cluster:       "default",
	ShardingKey:   "rand()",
	MutationsSync: 2,
}
View Source
var LocalMigrationsTableConfigByDefault = LocalMigrationsTableConfig{
	ReplicaName: "{replica}",
}

Functions

This section is empty.

Types

type DistributedMigrationsTableConfig

type DistributedMigrationsTableConfig struct {
	Cluster     string
	Database    string
	TableName   string
	ShardingKey string
	// MutationsSync controls the mutations_sync setting applied to the ALTER TABLE ... DELETE
	// statement in Delete: 0 - async, 1 - wait for the current server, 2 - wait for all replicas.
	MutationsSync uint8
}

type LocalMigrationsTableConfig

type LocalMigrationsTableConfig struct {
	ZooKeeperPath string
	ReplicaName   string
	Database      string
	TableName     string
}

type Store

type Store struct {
	// contains filtered or unexported fields
}

func NewStore

func NewStore(
	distributedMigrationsTableConfig DistributedMigrationsTableConfig,
	localMigrationsTableConfig LocalMigrationsTableConfig,
) (*Store, error)

func (*Store) CreateVersionTable

func (s *Store) CreateVersionTable(ctx context.Context, tx database.DBTxConn) error

CreateVersionTable creates the version table, which is used to track migrations.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, tx database.DBTxConn, version int64) error

Delete removes a version id from the version table.

func (*Store) GetLatestVersion

func (s *Store) GetLatestVersion(ctx context.Context, tx database.DBTxConn) (int64, error)

GetLatestVersion retrieves the last applied migration version. If no migrations exist, this method must return [ErrVersionNotFound].

func (*Store) GetMigration

func (s *Store) GetMigration(
	ctx context.Context,
	tx database.DBTxConn,
	version int64,
) (*database.GetMigrationResult, error)

GetMigration retrieves a single migration by version id. If the query succeeds, but the version is not found, this method must return [ErrVersionNotFound].

func (*Store) Insert

Insert a version id into the version table.

func (*Store) ListMigrations

func (s *Store) ListMigrations(ctx context.Context, tx database.DBTxConn) ([]*database.ListMigrationsResult, error)

ListMigrations retrieves all migrations sorted in descending order by id or timestamp. If there are no migrations, return an empty slice with no error. Typically, this method will return at least one migration because the initial version (0) is always inserted into the version table when it is created.

func (*Store) Tablename

func (s *Store) Tablename() string

func (*Store) TablenameFullDistributed added in v0.0.3

func (s *Store) TablenameFullDistributed() string

func (*Store) TablenameFullLocal added in v0.0.3

func (s *Store) TablenameFullLocal() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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