Skip to content

Commit

Permalink
resource/schema: fixed schema for drop resources (#65)
Browse files Browse the repository at this point in the history
* resource/schema: fixed schema for drop resources

Signed-off-by: Giau. Tran Minh <[email protected]>

* chore: fixed test

Signed-off-by: Giau. Tran Minh <[email protected]>

---------

Signed-off-by: Giau. Tran Minh <[email protected]>
  • Loading branch information
giautm authored Apr 21, 2023
1 parent c983b10 commit 0096553
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
29 changes: 27 additions & 2 deletions internal/provider/atlas_schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"strings"

"ariga.io/atlas/sql/sqlclient"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -181,7 +182,10 @@ func (r *AtlasSchemaResource) Delete(ctx context.Context, req resource.DeleteReq
}
// Delete the resource by setting
// the HCL to an empty string
data.HCL = types.String{Null: true}
resp.Diagnostics.Append(emptySchema(ctx, data.URL.Value, &data.HCL)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(r.applySchema(ctx, data)...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -228,7 +232,10 @@ func (r *AtlasSchemaResource) ModifyPlan(ctx context.Context, req resource.Modif
plan = state.Clone()
// Delete the resource by setting
// the HCL to an empty string.
plan.HCL = types.String{Null: true}
resp.Diagnostics.Append(emptySchema(ctx, plan.URL.Value, &plan.HCL)...)
if resp.Diagnostics.HasError() {
return
}
}
resp.Diagnostics.Append(PrintPlanSQL(ctx, r.client, r.getDevURL(plan.DevURL, plan.DeprecatedDevURL), plan)...)
}
Expand Down Expand Up @@ -363,6 +370,24 @@ https://atlasgo.io/terraform-provider#working-with-an-existing-database`, string
return
}

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.String{Value: fmt.Sprintf("schema %q {}", name)}
return
}
*hcl = types.String{Null: true}
return diags
}

func nonEmptyStringSlice(in []string) []string {
out := make([]string, 0, len(in))
for _, s := range in {
Expand Down
48 changes: 42 additions & 6 deletions internal/provider/atlas_schema_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ func TestAccDestroySchemas(t *testing.T) {
EOT
url = "%s"
}`, mysqlURL)
// When the following destroys, it only deletes schema "test4".
tfSchema := fmt.Sprintf(`resource "atlas_schema" "testdb" {
schema := `resource "atlas_schema" "testdb" {
hcl = <<-EOT
table "orders" {
schema = schema.test4
Expand All @@ -422,8 +421,8 @@ func TestAccDestroySchemas(t *testing.T) {
schema "test4" {
}
EOT
url = "%s/test4"
}`, mysqlURL)
url = "%s"
}`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Expand All @@ -435,7 +434,9 @@ func TestAccDestroySchemas(t *testing.T) {
ExpectNonEmptyPlan: true,
},
{
Config: tfSchema,
// When the following destroys, it doesn't delete any schemas.
// It only deletes the tables in the schemas.
Config: fmt.Sprintf(schema, mysqlURL+"/test4"),
// ignore non-normalized schema
ExpectNonEmptyPlan: true,
},
Expand All @@ -452,8 +453,43 @@ func TestAccDestroySchemas(t *testing.T) {
if _, ok := realm.Schema("do-not-delete"); !ok {
return fmt.Errorf("schema 'do-not-delete' does not exist, but expected to not be destroyed.")
}
if _, ok := realm.Schema("test4"); !ok {
return fmt.Errorf("schema 'test4' does not exist.")
}
return nil
},
})
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: preExistingSchema,
Destroy: false,
// ignore non-normalized schema
ExpectNonEmptyPlan: true,
},
{
// When the following destroys, it deletes all schemas.
Config: fmt.Sprintf(schema, mysqlURL),
// ignore non-normalized schema
ExpectNonEmptyPlan: true,
},
},
CheckDestroy: func(s *terraform.State) error {
cli, err := sqlclient.Open(context.Background(), mysqlURL)
if err != nil {
return err
}
realm, err := cli.InspectRealm(context.Background(), nil)
if err != nil {
return err
}
if _, ok := realm.Schema("do-not-delete"); ok {
return fmt.Errorf("schema 'do-not-delete' exist, but expected to be destroyed.")
}
if _, ok := realm.Schema("test4"); ok {
return fmt.Errorf("schema 'test4' wasn't deleted.")
return fmt.Errorf("schema 'test4' exist, but expected to be destroyed.")
}
return nil
},
Expand Down

0 comments on commit 0096553

Please sign in to comment.