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

atlas/schema: delete resource with schema clean command #153

Merged
merged 1 commit into from
Sep 23, 2024
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
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23

require (
ariga.io/atlas v0.27.0
ariga.io/atlas-go-sdk v0.5.9-0.20240912095114-8fc188ca2398
ariga.io/atlas-go-sdk v0.6.2
github.com/go-sql-driver/mysql v1.7.1
github.com/hashicorp/hcl/v2 v2.21.0
github.com/hashicorp/terraform-plugin-docs v0.16.0
Expand All @@ -14,8 +14,6 @@ require (
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.17
github.com/mitchellh/go-homedir v1.1.0
github.com/stretchr/testify v1.8.4
github.com/zclconf/go-cty v1.14.4
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ariga.io/atlas v0.27.0 h1:UHUQMTRx2Vz8/acJxEfcC/zL2wY739/vkZzt7L8HL8I=
ariga.io/atlas v0.27.0/go.mod h1:KPLc7Zj+nzoXfWshrcY1RwlOh94dsATQEy4UPrF2RkM=
ariga.io/atlas-go-sdk v0.5.9-0.20240912095114-8fc188ca2398 h1:u6mxEYmTYxmeyOuXi1C1pU6uyaqb2uQwWJE/P8k1qEw=
ariga.io/atlas-go-sdk v0.5.9-0.20240912095114-8fc188ca2398/go.mod h1:9Q+/04PVyJHUse1lEE9Kp6E18xj/6mIzaUTcWYSjSnQ=
ariga.io/atlas-go-sdk v0.6.2 h1:u7IAH6FqXRLpbW67Zw3jTy3Mz7lCB/rhbdPCorFhYTY=
ariga.io/atlas-go-sdk v0.6.2/go.mod h1:9Q+/04PVyJHUse1lEE9Kp6E18xj/6mIzaUTcWYSjSnQ=
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 Expand Up @@ -143,8 +143,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
Expand Down
86 changes: 61 additions & 25 deletions internal/provider/atlas_schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,36 @@ func (r *AtlasSchemaResource) Delete(ctx context.Context, req resource.DeleteReq
if resp.Diagnostics.HasError() {
return
}
// Delete the resource by setting
// the HCL to an empty string
resp.Diagnostics.Append(emptySchema(ctx, data.URL.ValueString(), &data.HCL)...)
if resp.Diagnostics.HasError() {
cfg, wd, err := data.Workspace(ctx, &r.ProviderData)
if err != nil {
resp.Diagnostics.AddError("Generate config failure",
fmt.Sprintf("Failed to create workspace: %s", err.Error()))
return
}
defer func() {
if err := wd.Close(); err != nil {
tflog.Debug(ctx, "Failed to cleanup working directory", map[string]any{
"error": err,
})
}
}()
c, err := r.Client(wd.Path(), cfg.Cloud)
if err != nil {
resp.Diagnostics.AddError("Client Error",
fmt.Sprintf("Unable to create client, got error: %s", err),
)
return
}
_, err = c.SchemaClean(ctx, &atlas.SchemaCleanParams{
Env: cfg.EnvName,
AutoApprove: true,
})
if err != nil {
resp.Diagnostics.AddError("Apply Error",
fmt.Sprintf("Unable to apply changes, got error: %s", err),
)
return
}
resp.Diagnostics.Append(r.applySchema(ctx, data)...)
}

// ValidateConfig implements resource.ResourceWithValidateConfig.
Expand Down Expand Up @@ -273,6 +296,7 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif
// drops schema resources by accident
resp.Diagnostics.Append(r.firstRunCheck(ctx, plan)...)
}
var isDelete bool
if plan == nil {
// This is a delete operation
if state == nil {
Expand All @@ -281,17 +305,12 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif
return
}
plan = state.Clone()
// Delete the resource by setting
// the HCL to an empty string.
resp.Diagnostics.Append(emptySchema(ctx, plan.URL.ValueString(), &plan.HCL)...)
if resp.Diagnostics.HasError() {
return
}
isDelete = true
}
resp.Diagnostics.Append(PrintPlanSQL(ctx, &r.ProviderData, plan)...)
resp.Diagnostics.Append(PrintPlanSQL(ctx, &r.ProviderData, plan, isDelete)...)
}

func PrintPlanSQL(ctx context.Context, p *ProviderData, data *AtlasSchemaResourceModel) (diags diag.Diagnostics) {
func PrintPlanSQL(ctx context.Context, p *ProviderData, data *AtlasSchemaResourceModel, delete bool) (diags diag.Diagnostics) {
cfg, wd, err := data.Workspace(ctx, p)
if err != nil {
diags.AddError("Generate config failure",
Expand All @@ -312,20 +331,37 @@ func PrintPlanSQL(ctx context.Context, p *ProviderData, data *AtlasSchemaResourc
)
return
}
result, err := c.SchemaApply(ctx, &atlas.SchemaApplyParams{
Env: cfg.EnvName,
TxMode: data.TxMode.ValueString(),
DryRun: true,
})
if err != nil {
diags.AddError("Atlas Plan Error",
fmt.Sprintf("Unable to generate migration plan, got error: %s", err),
)
return
var appliedFile *atlas.AppliedFile
if delete {
result, err := c.SchemaClean(ctx, &atlas.SchemaCleanParams{
Env: cfg.EnvName,
DryRun: true,
})
if err != nil {
diags.AddError("Atlas Plan Error",
fmt.Sprintf("Unable to generate migration plan, got error: %s", err),
)
return
}
appliedFile = result.Applied
} else {
result, err := c.SchemaApply(ctx, &atlas.SchemaApplyParams{
Env: cfg.EnvName,
TxMode: data.TxMode.ValueString(),
DryRun: true,
})
if err != nil {
diags.AddError("Atlas Plan Error",
fmt.Sprintf("Unable to generate migration plan, got error: %s", err),
)
return
}
appliedFile = result.Applied

}
if len(result.Changes.Pending) > 0 {
if appliedFile != nil && len(appliedFile.Applied) > 0 {
buf := &strings.Builder{}
for _, stmt := range result.Changes.Pending {
for _, stmt := range appliedFile.Applied {
fmt.Fprintln(buf, stmt)
}
diags.AddWarning("Atlas Plan",
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/atlas_schema_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ table "orders" {
return atlas.NewClient(wd, "atlas")
},
DevURL: mysqlDevURL,
}, tt.args.data)
}, tt.args.data, false)
require.Equal(t, tt.wantDiags, gotDiags)
})
}
Expand Down
39 changes: 0 additions & 39 deletions internal/provider/empty_schema.go

This file was deleted.

1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type (

SchemaInspect(context.Context, *atlas.SchemaInspectParams) (string, error)
SchemaApply(context.Context, *atlas.SchemaApplyParams) (*atlas.SchemaApply, error)
SchemaClean(context.Context, *atlas.SchemaCleanParams) (*atlas.SchemaClean, error)

Version(context.Context) (*atlas.Version, error)

Expand Down
Loading