From 09d4b37aee0bcc9154f08f6b4b7c2ee48d7984da Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" <12751435+giautm@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:48:51 +0700 Subject: [PATCH] migration: added exec_order attribute (#103) * migration: added exec_order attribute * chore: reduce new line --- docs/resources/migration.md | 1 + go.mod | 2 +- go.sum | 4 ++-- internal/provider/atlas_migration_resource.go | 11 +++++++++++ internal/provider/template.go | 18 ++++++++++++------ internal/provider/template_test.go | 4 ++++ .../provider/templates/atlas_migration.tmpl | 7 +++++-- .../testdata/TestTemplate/baseline-cfg.hcl | 1 - .../testdata/TestTemplate/cloud-cfg.hcl | 2 -- .../TestTemplate/cloud-no-token-cfg.hcl | 1 - .../testdata/TestTemplate/cloud-tag-cfg.hcl | 2 -- .../testdata/TestTemplate/local-cfg.hcl | 1 - .../TestTemplate/local-exec-order-cfg.hcl | 9 +++++++++ .../testdata/TestTemplate/token-cfg.hcl | 2 -- 14 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 internal/provider/testdata/TestTemplate/local-exec-order-cfg.hcl diff --git a/docs/resources/migration.md b/docs/resources/migration.md index b8080ec..d038ae9 100644 --- a/docs/resources/migration.md +++ b/docs/resources/migration.md @@ -39,6 +39,7 @@ resource "atlas_migration" "hello" { - `dev_url` (String, Sensitive) The url of the dev-db see https://atlasgo.io/cli/url - `dir` (String) the URL of the migration directory. dir or remote_dir block is required - `env_name` (String) The name of the environment used for reporting runs to Atlas Cloud. Default: tf +- `exec_order` (String) How Atlas computes and executes pending migration files to the database. One of `linear`,`linear-skip` or `non-linear`. See https://atlasgo.io/versioned/apply#execution-order - `remote_dir` (Block, Optional) (see [below for nested schema](#nestedblock--remote_dir)) - `revisions_schema` (String) The name of the schema the revisions table resides in - `version` (String) The version of the migration to apply, if not specified the latest version will be applied diff --git a/go.mod b/go.mod index 47acd9f..6078c9e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( ariga.io/atlas v0.15.1-0.20231104142243-962427888bf2 - ariga.io/atlas-go-sdk v0.2.2-0.20231126180634-71349491e677 + ariga.io/atlas-go-sdk v0.2.2 github.com/go-sql-driver/mysql v1.7.1 github.com/hashicorp/terraform-plugin-docs v0.16.0 github.com/hashicorp/terraform-plugin-framework v1.4.2 diff --git a/go.sum b/go.sum index a746c24..fb32384 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ ariga.io/atlas v0.15.1-0.20231104142243-962427888bf2 h1:uw5P4SCzPO3dACfbtt6TuwrWY72B4EKrNck+hKPM6dg= ariga.io/atlas v0.15.1-0.20231104142243-962427888bf2/go.mod h1:isZrlzJ5cpoCoKFoY9knZug7Lq4pP1cm8g3XciLZ0Pw= -ariga.io/atlas-go-sdk v0.2.2-0.20231126180634-71349491e677 h1:/KgcE4O6MgyZSH1rQMMC17VGEjsSOCoBKFLC4kXr19I= -ariga.io/atlas-go-sdk v0.2.2-0.20231126180634-71349491e677/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU= +ariga.io/atlas-go-sdk v0.2.2 h1:eEOXfxFfWbs8a4+R/i7lSwIchzz6C8Kq4QPBs6YGpdQ= +ariga.io/atlas-go-sdk v0.2.2/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU= dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= diff --git a/internal/provider/atlas_migration_resource.go b/internal/provider/atlas_migration_resource.go index 2f0d504..24c888c 100644 --- a/internal/provider/atlas_migration_resource.go +++ b/internal/provider/atlas_migration_resource.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" @@ -14,6 +15,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" @@ -33,6 +35,7 @@ type ( RevisionsSchema types.String `tfsdk:"revisions_schema"` Version types.String `tfsdk:"version"` Baseline types.String `tfsdk:"baseline"` + ExecOrder types.String `tfsdk:"exec_order"` Cloud *AtlasCloudBlock `tfsdk:"cloud"` RemoteDir *RemoteDirBlock `tfsdk:"remote_dir"` @@ -104,6 +107,13 @@ func (r *MigrationResource) Schema(_ context.Context, _ resource.SchemaRequest, Description: "An optional version to start the migration history from. See https://atlasgo.io/versioned/apply#existing-databases", Optional: true, }, + "exec_order": schema.StringAttribute{ + Description: "How Atlas computes and executes pending migration files to the database. One of `linear`,`linear-skip` or `non-linear`. See https://atlasgo.io/versioned/apply#execution-order", + Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf("linear", "linear-skip", "non-linear"), + }, + }, "revisions_schema": schema.StringAttribute{ Description: "The name of the schema the revisions table resides in", Optional: true, @@ -482,6 +492,7 @@ func (d *MigrationResourceModel) AtlasHCL(name string, devURL string, cloud *Atl DirURL: d.DirURL.ValueStringPointer(), Baseline: d.Baseline.ValueString(), RevisionsSchema: d.RevisionsSchema.ValueString(), + ExecOrder: d.ExecOrder.ValueString(), } if d.Cloud != nil && d.Cloud.Token.ValueString() != "" { // Use the data source cloud block if it is set diff --git a/internal/provider/template.go b/internal/provider/template.go index 5d4a900..de205c2 100644 --- a/internal/provider/template.go +++ b/internal/provider/template.go @@ -31,6 +31,7 @@ type ( RemoteDir *remoteDir Baseline string + ExecOrder string RevisionsSchema string } schemaData struct { @@ -48,12 +49,7 @@ var ( tmpls embed.FS tmpl = template.Must(template.New("terraform"). Funcs(template.FuncMap{ - "hclValue": func(s string) string { - if s == "" { - return s - } - return strings.ReplaceAll(strings.ToUpper(s), "-", "_") - }, + "hclValue": hclValue, "slides": func(s []string) (string, error) { b := &strings.Builder{} b.WriteRune('[') @@ -92,3 +88,13 @@ func (d *schemaData) render(w io.Writer) error { } return tmpl.ExecuteTemplate(w, "atlas_schema.tmpl", d) } + +// hclValue returns the given string in +// HCL format. For example, linear-skip becomes +// LINEAR_SKIP. +func hclValue(s string) string { + if s == "" { + return "" + } + return strings.ReplaceAll(strings.ToUpper(s), "-", "_") +} diff --git a/internal/provider/template_test.go b/internal/provider/template_test.go index 4c6def5..82cf4f7 100644 --- a/internal/provider/template_test.go +++ b/internal/provider/template_test.go @@ -36,6 +36,10 @@ func TestTemplate(t *testing.T) { {name: "local", data: templateData{ URL: "mysql://user:pass@localhost:3306/tf-db", }}, + {name: "local-exec-order", data: templateData{ + URL: "mysql://user:pass@localhost:3306/tf-db", + ExecOrder: "linear-skip", + }}, {name: "baseline", data: templateData{ URL: "mysql://user:pass@localhost:3306/tf-db", Baseline: "100000", diff --git a/internal/provider/templates/atlas_migration.tmpl b/internal/provider/templates/atlas_migration.tmpl index 0800db7..1bd769f 100644 --- a/internal/provider/templates/atlas_migration.tmpl +++ b/internal/provider/templates/atlas_migration.tmpl @@ -1,4 +1,4 @@ -{{- with .Cloud }} +{{- with .Cloud -}} atlas { cloud { token = "{{ .Token }}" @@ -11,7 +11,7 @@ atlas { } } {{- end }} -{{ with .RemoteDir }} +{{- with .RemoteDir }} data "remote_dir" "this" { name = "{{ .Name }}" {{- if .Tag }} @@ -48,6 +48,9 @@ env { {{- if .Baseline }} baseline = "{{ .Baseline }}" {{- end }} +{{- if .ExecOrder }} + exec_order = {{ hclValue .ExecOrder }} +{{- end }} {{- if .RevisionsSchema }} revisions_schema = "{{ .RevisionsSchema }}" {{- end }} diff --git a/internal/provider/testdata/TestTemplate/baseline-cfg.hcl b/internal/provider/testdata/TestTemplate/baseline-cfg.hcl index abc2155..6bb2213 100644 --- a/internal/provider/testdata/TestTemplate/baseline-cfg.hcl +++ b/internal/provider/testdata/TestTemplate/baseline-cfg.hcl @@ -1,5 +1,4 @@ - env { name = atlas.env url = "mysql://user:pass@localhost:3306/tf-db" diff --git a/internal/provider/testdata/TestTemplate/cloud-cfg.hcl b/internal/provider/testdata/TestTemplate/cloud-cfg.hcl index 8f3ee57..37103c1 100644 --- a/internal/provider/testdata/TestTemplate/cloud-cfg.hcl +++ b/internal/provider/testdata/TestTemplate/cloud-cfg.hcl @@ -1,4 +1,3 @@ - atlas { cloud { token = "token" @@ -6,7 +5,6 @@ atlas { url = "url" } } - data "remote_dir" "this" { name = "tf-dir" } diff --git a/internal/provider/testdata/TestTemplate/cloud-no-token-cfg.hcl b/internal/provider/testdata/TestTemplate/cloud-no-token-cfg.hcl index f22255d..a9f60ce 100644 --- a/internal/provider/testdata/TestTemplate/cloud-no-token-cfg.hcl +++ b/internal/provider/testdata/TestTemplate/cloud-no-token-cfg.hcl @@ -1,5 +1,4 @@ - data "remote_dir" "this" { name = "tf-dir" } diff --git a/internal/provider/testdata/TestTemplate/cloud-tag-cfg.hcl b/internal/provider/testdata/TestTemplate/cloud-tag-cfg.hcl index 1d6c27c..239b41b 100644 --- a/internal/provider/testdata/TestTemplate/cloud-tag-cfg.hcl +++ b/internal/provider/testdata/TestTemplate/cloud-tag-cfg.hcl @@ -1,10 +1,8 @@ - atlas { cloud { token = "token" } } - data "remote_dir" "this" { name = "tf-dir" tag = "tag" diff --git a/internal/provider/testdata/TestTemplate/local-cfg.hcl b/internal/provider/testdata/TestTemplate/local-cfg.hcl index 5be954a..f98f8b3 100644 --- a/internal/provider/testdata/TestTemplate/local-cfg.hcl +++ b/internal/provider/testdata/TestTemplate/local-cfg.hcl @@ -1,5 +1,4 @@ - env { name = atlas.env url = "mysql://user:pass@localhost:3306/tf-db" diff --git a/internal/provider/testdata/TestTemplate/local-exec-order-cfg.hcl b/internal/provider/testdata/TestTemplate/local-exec-order-cfg.hcl new file mode 100644 index 0000000..6ec492b --- /dev/null +++ b/internal/provider/testdata/TestTemplate/local-exec-order-cfg.hcl @@ -0,0 +1,9 @@ + +env { + name = atlas.env + url = "mysql://user:pass@localhost:3306/tf-db" + migration { + dir = "file://migrations" + exec_order = LINEAR_SKIP + } +} diff --git a/internal/provider/testdata/TestTemplate/token-cfg.hcl b/internal/provider/testdata/TestTemplate/token-cfg.hcl index 48f2881..e61b0f7 100644 --- a/internal/provider/testdata/TestTemplate/token-cfg.hcl +++ b/internal/provider/testdata/TestTemplate/token-cfg.hcl @@ -1,10 +1,8 @@ - atlas { cloud { token = "token+%=_-" } } - env { name = atlas.env url = "mysql://user:pass@localhost:3306/tf-db"