Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration: added exec_order attribute #103

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ 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"
"github.com/hashicorp/terraform-plugin-framework/resource"
"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"

Expand All @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions internal/provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type (
RemoteDir *remoteDir

Baseline string
ExecOrder string
RevisionsSchema string
}
schemaData struct {
Expand All @@ -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('[')
Expand Down Expand Up @@ -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), "-", "_")
}
4 changes: 4 additions & 0 deletions internal/provider/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions internal/provider/templates/atlas_migration.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- with .Cloud }}
{{- with .Cloud -}}
atlas {
cloud {
token = "{{ .Token }}"
Expand All @@ -11,7 +11,7 @@ atlas {
}
}
{{- end }}
{{ with .RemoteDir }}
{{- with .RemoteDir }}
data "remote_dir" "this" {
name = "{{ .Name }}"
{{- if .Tag }}
Expand Down Expand Up @@ -48,6 +48,9 @@ env {
{{- if .Baseline }}
baseline = "{{ .Baseline }}"
{{- end }}
{{- if .ExecOrder }}
exec_order = {{ hclValue .ExecOrder }}
{{- end }}
{{- if .RevisionsSchema }}
revisions_schema = "{{ .RevisionsSchema }}"
{{- end }}
Expand Down
1 change: 0 additions & 1 deletion internal/provider/testdata/TestTemplate/baseline-cfg.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/testdata/TestTemplate/cloud-cfg.hcl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

atlas {
cloud {
token = "token"
project = "project"
url = "url"
}
}

data "remote_dir" "this" {
name = "tf-dir"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


data "remote_dir" "this" {
name = "tf-dir"
}
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/testdata/TestTemplate/cloud-tag-cfg.hcl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

atlas {
cloud {
token = "token"
}
}

data "remote_dir" "this" {
name = "tf-dir"
tag = "tag"
Expand Down
1 change: 0 additions & 1 deletion internal/provider/testdata/TestTemplate/local-cfg.hcl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

giautm marked this conversation as resolved.
Show resolved Hide resolved
env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
migration {
dir = "file://migrations"
exec_order = LINEAR_SKIP
}
}
2 changes: 0 additions & 2 deletions internal/provider/testdata/TestTemplate/token-cfg.hcl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

atlas {
cloud {
token = "token+%=_-"
}
}

env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
Expand Down