diff --git a/go.mod b/go.mod index deee81d..e0f2328 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 22e6306..bdd3a4b 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/internal/provider/atlas_schema_resource.go b/internal/provider/atlas_schema_resource.go index 3a9e77d..d0a3ef9 100644 --- a/internal/provider/atlas_schema_resource.go +++ b/internal/provider/atlas_schema_resource.go @@ -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. @@ -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 { @@ -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", @@ -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", diff --git a/internal/provider/atlas_schema_resource_test.go b/internal/provider/atlas_schema_resource_test.go index e9484cc..3527100 100644 --- a/internal/provider/atlas_schema_resource_test.go +++ b/internal/provider/atlas_schema_resource_test.go @@ -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) }) } diff --git a/internal/provider/empty_schema.go b/internal/provider/empty_schema.go deleted file mode 100644 index 25d146f..0000000 --- a/internal/provider/empty_schema.go +++ /dev/null @@ -1,39 +0,0 @@ -package provider - -import ( - "context" - "fmt" - - // The following imports are required to register the SQL drivers. - // For emptySchema() function to work. - _ "ariga.io/atlas/sql/mysql" - _ "ariga.io/atlas/sql/postgres" - _ "ariga.io/atlas/sql/sqlite" - _ "github.com/go-sql-driver/mysql" - _ "github.com/lib/pq" - _ "github.com/mattn/go-sqlite3" - - "ariga.io/atlas/sql/sqlclient" - "github.com/hashicorp/terraform-plugin-framework/diag" - "github.com/hashicorp/terraform-plugin-framework/types" -) - -// emptySchema returns an empty schema block if the URL is connected to a schema. -// Otherwise, it returns a null string for schema. -func emptySchema(ctx context.Context, url string, hcl *types.String) (diags diag.Diagnostics) { - s, err := sqlclient.Open(ctx, url) - if err != nil { - diags.AddError("Atlas Plan Error", - fmt.Sprintf("Unable to connect to database, got error: %s", err), - ) - return - } - defer s.Close() - name := s.URL.Schema - if name != "" { - *hcl = types.StringValue(fmt.Sprintf("schema %q {}", name)) - return - } - *hcl = types.StringNull() - return diags -} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index f94e41c..13772c1 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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)