migrate-template/README.md

53 lines
1.1 KiB
Markdown
Raw Normal View History

2020-04-02 14:50:56 +03:00
# Templating for Migration Sources
migrate-template a wrapper for migration sources of library [golang-migrate/migrate](https://github.com/golang-migrate/migrate) that allows the use of variables in migrations files.
## Use in your Go project
```go
import (
2022-08-17 14:39:00 +03:00
template "github.com/mbk-lab/migrate-template"
2020-04-02 14:50:56 +03:00
"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,
2020-04-02 14:58:42 +03:00
template.WithVars(template.M{
2020-04-02 14:50:56 +03:00
"cluster": "cluster_name",
"replicated_path": "/clickhouse/tables/db_name",
}),
)
m, err := migrate.NewWithSourceInstance(
"template", templateInstance,
"clickhouse://...",
)
m.Steps(1)
}
```
Migration template using variables:
```sql
2022-08-29 09:36:28 +03:00
CREATE TABLE table_name ON CLUSTER {{.cluster}} (
2020-04-02 14:50:56 +03:00
foo Int64,
bar String
2022-08-29 09:36:28 +03:00
) ENGINE = ReplicatedReplacingMergeTree('{{.replicated_path}}.table_name', '{replica}')
2020-04-02 14:50:56 +03:00
```
Result:
```sql
CREATE TABLE table_name ON CLUSTER cluster_name (
foo Int64,
bar String
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/db_name.table_name', '{replica}')
```