Skip to content

Commit

Permalink
chore: upgrade atlas-go-sdk (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Jan 2, 2024
1 parent 1de7b9f commit 6a354b6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 52 deletions.
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
ariga.io/atlas-go-sdk v0.3.0
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 h1:eEOXfxFfWbs8a4+R/i7lSwIchzz6C8Kq4QPBs6YGpdQ=
ariga.io/atlas-go-sdk v0.2.2/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU=
ariga.io/atlas-go-sdk v0.3.0 h1:19hxr3Fm/Jg/pNSLq3mpTQs1k1sPNhQXpLgVtIDQ0J8=
ariga.io/atlas-go-sdk v0.3.0/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
2 changes: 1 addition & 1 deletion internal/provider/atlas_migration_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (d *MigrationDataSource) Read(ctx context.Context, req datasource.ReadReque
Env: "tf",
})
if err != nil {
resp.Diagnostics.Append(errorDiagnostic(err, "Failed to read migration status"))
resp.Diagnostics.AddError("Failed to read migration status", err.Error())
return
}
data.Status = types.StringValue(r.Status)
Expand Down
49 changes: 20 additions & 29 deletions internal/provider/atlas_migration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (r *MigrationResource) ModifyPlan(ctx context.Context, req resource.ModifyP
Env: defaultString(plan.EnvName, "tf"),
})
if err != nil {
resp.Diagnostics.Append(errorDiagnostic(err, "Failed to read migration status"))
resp.Diagnostics.AddError("Failed to read migration status", err.Error())
return
}
if plan.Version.ValueString() == "" {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (r *MigrationResource) ModifyPlan(ctx context.Context, req resource.ModifyP
Latest: pendingCount,
})
if err != nil {
resp.Diagnostics.Append(errorDiagnostic(err, "Failed to lint migration"))
resp.Diagnostics.AddError("Failed to lint migration", err.Error())
return
}
for _, f := range lint.Files {
Expand All @@ -370,26 +370,24 @@ func (r *MigrationResource) ModifyPlan(ctx context.Context, req resource.ModifyP
func (r *MigrationResource) migrate(ctx context.Context, data *MigrationResourceModel) (diags diag.Diagnostics) {
dir, err := os.MkdirTemp(os.TempDir(), "tf-atlas-*")
if err != nil {
return diag.Diagnostics{
diag.NewErrorDiagnostic("Generate config failure",
fmt.Sprintf("Failed to create temporary directory: %s", err.Error())),
}
diags.AddError("Generate config failure",
fmt.Sprintf("Failed to create temporary directory: %s", err.Error()))
return
}
defer os.RemoveAll(dir)
cfgPath := filepath.Join(dir, "atlas.hcl")
err = data.AtlasHCL(cfgPath, r.devURL, r.cloud)
if err != nil {
return diag.Diagnostics{
diag.NewErrorDiagnostic("Generate config failure",
fmt.Sprintf("Failed to create atlas.hcl: %s", err.Error())),
}
diags.AddError("Generate config failure",
fmt.Sprintf("Failed to create atlas.hcl: %s", err.Error()))
return
}
statusReport, err := r.client.Status(ctx, &atlas.MigrateStatusParams{
ConfigURL: fmt.Sprintf("file://%s", cfgPath),
Env: defaultString(data.EnvName, "tf"),
})
if err != nil {
diags.Append(errorDiagnostic(err, "Failed to read migration status"))
diags.AddError("Failed to read migration status", err.Error())
return
}
amount, synced := statusReport.Amount(data.Version.ValueString())
Expand All @@ -402,7 +400,7 @@ func (r *MigrationResource) migrate(ctx context.Context, data *MigrationResource
)
return
}
report, err := r.client.Apply(ctx, &atlas.MigrateApplyParams{
_, err := r.client.MigrateApply(ctx, &atlas.MigrateApplyParams{
ConfigURL: fmt.Sprintf("file://%s", cfgPath),
Env: defaultString(data.EnvName, "tf"),
Amount: amount,
Expand All @@ -412,43 +410,36 @@ func (r *MigrationResource) migrate(ctx context.Context, data *MigrationResource
},
})
if err != nil {
diags.Append(errorDiagnostic(err, "Failed to apply migrations"))
return
}
if report.Error != "" {
diags.AddError("Failed to apply migration", report.Error)
diags.AddError("Failed to apply migrations", err.Error())
return
}
}
data.Status, diags = r.buildStatus(ctx, data)
return
}

func (r *MigrationResource) buildStatus(ctx context.Context, data *MigrationResourceModel) (types.Object, diag.Diagnostics) {
func (r *MigrationResource) buildStatus(ctx context.Context, data *MigrationResourceModel) (obj types.Object, diags diag.Diagnostics) {
obj = types.ObjectNull(statusObjectAttrs)

dir, err := os.MkdirTemp(os.TempDir(), "tf-atlas-*")
if err != nil {
return types.ObjectNull(statusObjectAttrs), diag.Diagnostics{
diag.NewErrorDiagnostic("Generate config failure",
fmt.Sprintf("Failed to create temporary directory: %s", err.Error())),
}
diags.AddError("Generate config failure", fmt.Sprintf("Failed to create temporary directory: %s", err.Error()))
return
}
defer os.RemoveAll(dir)
cfgPath := filepath.Join(dir, "atlas.hcl")
err = data.AtlasHCL(cfgPath, r.devURL, r.cloud)
if err != nil {
return types.ObjectNull(statusObjectAttrs), diag.Diagnostics{
diag.NewErrorDiagnostic("Generate config failure",
fmt.Sprintf("Failed to create atlas.hcl: %s", err.Error())),
}
diags.AddError("Generate config failure", fmt.Sprintf("Failed to create atlas.hcl: %s", err.Error()))
return
}
report, err := r.client.Status(ctx, &atlas.MigrateStatusParams{
ConfigURL: fmt.Sprintf("file://%s", cfgPath),
Env: defaultString(data.EnvName, "tf"),
})
if err != nil {
return types.ObjectNull(statusObjectAttrs), diag.Diagnostics{
errorDiagnostic(err, "Failed to read migration status"),
}
diags.AddError("Failed to read migration status", err.Error())
return
}
current := types.StringNull()
if !(report.Status == "PENDING" && report.Current == noMigration) {
Expand Down
19 changes: 0 additions & 19 deletions internal/provider/tf_diag.go

This file was deleted.

0 comments on commit 6a354b6

Please sign in to comment.