Skip to content

Commit

Permalink
atlas/migration: remove remote_dir datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Jul 25, 2024
1 parent 5f46547 commit c6e8be2
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 66 deletions.
17 changes: 12 additions & 5 deletions internal/provider/atlas_migration_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ func (d *MigrationDataSource) Read(ctx context.Context, req datasource.ReadReque
func (d *MigrationDataSourceModel) AtlasHCL(path string, cloud *AtlasCloudBlock) error {
cfg := templateData{
URL: d.URL.ValueString(),
DirURL: d.DirURL.ValueStringPointer(),
RevisionsSchema: d.RevisionsSchema.ValueString(),
}
if d.Cloud != nil && d.Cloud.Token.ValueString() != "" {
Expand All @@ -189,11 +188,19 @@ func (d *MigrationDataSourceModel) AtlasHCL(path string, cloud *AtlasCloudBlock)
URL: cloud.URL.ValueStringPointer(),
}
}
if d := d.RemoteDir; d != nil {
cfg.RemoteDir = &remoteDir{
Name: d.Name.ValueString(),
Tag: d.Tag.ValueStringPointer(),
switch {
case d.RemoteDir != nil:
if cfg.Cloud == nil {
return fmt.Errorf("cloud configuration is not set")
}
cfg.DirURL = "atlas://" + d.RemoteDir.Name.ValueString()
if !d.RemoteDir.Tag.IsNull() {
cfg.DirURL += "?tag=" + d.RemoteDir.Tag.ValueString()
}
case !d.DirURL.IsNull():
cfg.DirURL = fmt.Sprintf("file://%s", d.DirURL.ValueString())
default:
cfg.DirURL = "file://migrations"
}
return cfg.CreateFile(path)
}
17 changes: 12 additions & 5 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,6 @@ func (d *MigrationResourceModel) AtlasHCL(name string, devURL string, cloud *Atl
cfg := templateData{
URL: d.URL.ValueString(),
DevURL: defaultString(d.DevURL, devURL),
DirURL: d.DirURL.ValueStringPointer(),
Baseline: d.Baseline.ValueString(),
RevisionsSchema: d.RevisionsSchema.ValueString(),
ExecOrder: d.ExecOrder.ValueString(),
Expand All @@ -737,11 +736,19 @@ func (d *MigrationResourceModel) AtlasHCL(name string, devURL string, cloud *Atl
URL: cloud.URL.ValueStringPointer(),
}
}
if d := d.RemoteDir; d != nil {
cfg.RemoteDir = &remoteDir{
Name: d.Name.ValueString(),
Tag: d.Tag.ValueStringPointer(),
switch {
case d.RemoteDir != nil:
if cfg.Cloud == nil {
return fmt.Errorf("cloud configuration is not set")
}
cfg.DirURL = "atlas://" + d.RemoteDir.Name.ValueString()
if !d.RemoteDir.Tag.IsNull() {
cfg.DirURL += "?tag=" + d.RemoteDir.Tag.ValueString()
}
case !d.DirURL.IsNull():
cfg.DirURL = fmt.Sprintf("file://%s", d.DirURL.ValueString())
default:
cfg.DirURL = "file://migrations"
}
return cfg.CreateFile(name)
}
10 changes: 2 additions & 8 deletions internal/provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ type (
Project *string
URL *string
}
remoteDir struct {
Name string
Tag *string
}
templateData struct {
URL string
DevURL string
Schemas []string
Exclude []string

Cloud *cloudConfig
DirURL *string
RemoteDir *remoteDir

Cloud *cloudConfig
DirURL string
Baseline string
ExecOrder string
RevisionsSchema string
Expand Down
22 changes: 7 additions & 15 deletions internal/provider/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestTemplate(t *testing.T) {
Cloud: &cloudConfig{
Token: "token+%=_-",
},
DirURL: "file://migrations",
}},
{name: "cloud", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
Expand All @@ -29,37 +30,28 @@ func TestTemplate(t *testing.T) {
URL: ptr("url"),
Project: ptr("project"),
},
RemoteDir: &remoteDir{
Name: "tf-dir",
},
DirURL: "atlas://tf-dir?tag=latest",
}},
{name: "local", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
URL: "mysql://user:pass@localhost:3306/tf-db",
DirURL: "file://migrations",
}},
{name: "local-exec-order", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
DirURL: "file://migrations",
ExecOrder: "linear-skip",
}},
{name: "baseline", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
DirURL: "file://migrations",
Baseline: "100000",
}},
{name: "cloud-no-token", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
RemoteDir: &remoteDir{
Name: "tf-dir",
},
DirURL: ptr("dir-url"),
}},
{name: "cloud-tag", data: templateData{
URL: "mysql://user:pass@localhost:3306/tf-db",
Cloud: &cloudConfig{
Token: "token",
},
RemoteDir: &remoteDir{
Name: "tf-dir",
Tag: ptr("tag"),
},
DirURL: "atlas://tf-dir?tag=tag",
}},
}
for _, tt := range tests {
Expand Down
14 changes: 1 addition & 13 deletions internal/provider/templates/atlas_migration.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ atlas {
}
}
{{- end }}
{{- with .RemoteDir }}
data "remote_dir" "this" {
name = "{{ .Name }}"
{{- if .Tag }}
tag = "{{ .Tag }}"
{{- end }}
}
{{- end }}
env {
name = atlas.env
url = "{{ .URL }}"
Expand All @@ -40,11 +32,7 @@ env {
]
{{- end }}
migration {
{{- if and .Cloud .RemoteDir }}
dir = data.remote_dir.this.url
{{- else }}
dir = "file://{{ or .DirURL "migrations" }}"
{{- end }}
dir = "{{ .DirURL }}"
{{- if .Baseline }}
baseline = "{{ .Baseline }}"
{{- end }}
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/testdata/TestTemplate/cloud-cfg.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ atlas {
url = "url"
}
}
data "remote_dir" "this" {
name = "tf-dir"
}
env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
migration {
dir = data.remote_dir.this.url
dir = "atlas://tf-dir?tag=latest"
}
}
11 changes: 0 additions & 11 deletions internal/provider/testdata/TestTemplate/cloud-no-token-cfg.hcl

This file was deleted.

6 changes: 1 addition & 5 deletions internal/provider/testdata/TestTemplate/cloud-tag-cfg.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ atlas {
token = "token"
}
}
data "remote_dir" "this" {
name = "tf-dir"
tag = "tag"
}
env {
name = atlas.env
url = "mysql://user:pass@localhost:3306/tf-db"
migration {
dir = data.remote_dir.this.url
dir = "atlas://tf-dir?tag=tag"
}
}

0 comments on commit c6e8be2

Please sign in to comment.