Go to file
Dmitriy Chertkov 5e028187a8 Added LICENSE 2020-04-02 17:26:18 +05:00
LICENSE Added LICENSE 2020-04-02 17:26:18 +05:00
README.md Fix readme 2020-04-02 16:58:42 +05:00
go.mod first commit 2020-04-02 16:52:36 +05:00
go.sum first commit 2020-04-02 16:52:36 +05:00
template.go first commit 2020-04-02 16:52:36 +05:00

README.md

Templating for Migration Sources

migrate-template a wrapper for migration sources of library golang-migrate/migrate that allows the use of variables in migrations files.

Use in your Go project


import (
	template "github.com/FindMyKids/migrate-template"
	"github.com/golang-migrate/migrate/v4"
	"github.com/golang-migrate/migrate/v4/source"
)

func main() {
	sourceInstance, err := source.Open("file://migration")
	
	templateInstance := template.Wrap(
		sourceInstance,
		template.WithVars(template.M{
			"cluster": "cluster_name",
			"replicated_path": "/clickhouse/tables/db_name",
		}),
	)

	m, err := migrate.NewWithSourceInstance(
		"template", templateInstance,
		"clickhouse://...",
	)
	m.Steps(1)
}

Migration template using variables:


CREATE TABLE table_name ON CLUSTER {{cluster}} (
	foo Int64,
	bar String
) ENGINE = ReplicatedReplacingMergeTree('{{replicated_path}}.table_name', '{replica}')

Result:


CREATE TABLE table_name ON CLUSTER cluster_name (
	foo Int64,
	bar String
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/db_name.table_name', '{replica}')