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: don't deps on URL for cleanup schema #153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 13 additions & 5 deletions internal/provider/atlas_schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,14 @@ func (r *AtlasSchemaResource) Delete(ctx context.Context, req resource.DeleteReq
}
// 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() {
empty, err := emptySchemas(data.HCL.ValueString())
if err != nil {
resp.Diagnostics.AddError("HCL Error",
fmt.Sprintf("Unable to generate empty state, got error: %s", err),
)
return
}
data.HCL = types.StringValue(empty)
resp.Diagnostics.Append(r.applySchema(ctx, data)...)
}

Expand Down Expand Up @@ -277,11 +281,15 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif
}
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() {
// the HCL to an empty string
empty, err := emptySchemas(plan.HCL.ValueString())
if err != nil {
resp.Diagnostics.AddError("HCL Error",
fmt.Sprintf("Unable to generate empty state, got error: %s", err),
)
return
}
plan.HCL = types.StringValue(empty)
}
resp.Diagnostics.Append(PrintPlanSQL(ctx, r.client, r.getDevURL(plan.DevURL), plan)...)
}
Expand Down
55 changes: 24 additions & 31 deletions internal/provider/empty_schema.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,32 @@
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"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclwrite"
)

// 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
// emptySchemas removes all all blocks except the schema block from the given schema.
func emptySchemas(schema string) (string, error) {
f, diags := hclwrite.ParseConfig([]byte(schema), "schema.hcl", hcl.InitialPos)
if diags.HasErrors() {
return "", diags
}
root := f.Body()
schemas := 0
for _, blk := range root.Blocks() {
if blk.Type() == "schema" {
schemas++
// Clear the schema block to get an empty schema.
blk.Body().Clear()
} else {
// Remove all other blocks.
root.RemoveBlock(blk)
}
}
defer s.Close()
name := s.URL.Schema
if name != "" {
*hcl = types.StringValue(fmt.Sprintf("schema %q {}", name))
return
if schemas > 1 {
// If there are more than one schema blocks, return an empty string.
// To clear the whole realm
return "", nil
}
*hcl = types.StringNull()
return diags
return string(f.Bytes()), nil
}
Loading