Skip to content

Commit

Permalink
implement schema delete (#10)
Browse files Browse the repository at this point in the history
* implement schema delete

* comment

* gen docs

* Update atlas/resource_atlas_schema_test.go

Co-authored-by: Ariel Mashraki <[email protected]>

* Update atlas/resource_atlas_schema_test.go

Co-authored-by: Ariel Mashraki <[email protected]>

* CR

Co-authored-by: Ariel Mashraki <[email protected]>
  • Loading branch information
hedwigz and a8m authored May 8, 2022
1 parent 7f7c213 commit 8cd5081
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 24 deletions.
38 changes: 33 additions & 5 deletions atlas/resource_atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func newSchemaResource() *schema.Resource {
CreateContext: applySchema,
UpdateContext: applySchema,
ReadContext: readSchema,
DeleteContext: readSchema,
DeleteContext: deleteSchema,
Schema: map[string]*schema.Schema{
"hcl": {
Type: schema.TypeString,
Expand All @@ -41,6 +41,33 @@ func newSchemaResource() *schema.Resource {
}
}

func deleteSchema(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
url := d.Get("url").(string)

cli, err := sqlclient.Open(ctx, url)
if err != nil {
return diag.FromErr(err)
}
var schemas []string
if cli.URL.Schema != "" {
schemas = append(schemas, cli.URL.Schema)
}
realm, err := cli.InspectRealm(ctx, &atlaschema.InspectRealmOption{Schemas: schemas})
if err != nil {
return diag.FromErr(err)
}
desired := &atlaschema.Realm{}
changes, err := cli.RealmDiff(realm, desired)
if err != nil {
return diag.FromErr(err)
}
if err = cli.ApplyChanges(ctx, changes); err != nil {
return diag.FromErr(err)
}
return diags
}

func readSchema(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
url := d.Get("url").(string)
Expand Down Expand Up @@ -72,17 +99,18 @@ func applySchema(ctx context.Context, d *schema.ResourceData, m interface{}) dia
if err != nil {
return diag.FromErr(err)
}

realm, err := cli.InspectRealm(ctx, nil)
var schemas []string
if cli.URL.Schema != "" {
schemas = append(schemas, cli.URL.Schema)
}
realm, err := cli.InspectRealm(ctx, &atlaschema.InspectRealmOption{Schemas: schemas})
if err != nil {
return diag.FromErr(err)
}

desired := &atlaschema.Realm{}
if err = cli.Evaluator.Eval([]byte(hcl), desired, nil); err != nil {
return diag.FromErr(err)
}

if devURL, ok := d.GetOk("dev_db_url"); ok {
dev, err := sqlclient.Open(ctx, devURL.(string))
if err != nil {
Expand Down
74 changes: 68 additions & 6 deletions atlas/resource_atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

const testAccActionConfigCreate = `
data "atlas_schema" "market" {
dev_db_url = "mysql://root:pass@localhost:3307/test"
dev_db_url = "mysql://root:pass@localhost:3307"
src = <<-EOT
schema "test" {
charset = "utf8mb4"
Expand All @@ -34,13 +34,13 @@ data "atlas_schema" "market" {
}
resource "atlas_schema" "testdb" {
hcl = data.atlas_schema.market.hcl
url = "mysql://root:pass@localhost:3306/test"
url = "mysql://root:pass@localhost:3306"
}
`

const testAccActionConfigUpdate = `
data "atlas_schema" "market" {
dev_db_url = "mysql://root:pass@localhost:3307/test"
dev_db_url = "mysql://root:pass@localhost:3307"
src = <<-EOT
schema "test" {
charset = "utf8mb4"
Expand All @@ -65,7 +65,7 @@ data "atlas_schema" "market" {
}
resource "atlas_schema" "testdb" {
hcl = data.atlas_schema.market.hcl
url = "mysql://root:pass@localhost:3306/test"
url = "mysql://root:pass@localhost:3306"
}
`

Expand All @@ -78,13 +78,13 @@ func TestAccAtlasDatabase(t *testing.T) {
{
Config: testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("atlas_schema.testdb", "id", "mysql://root:pass@localhost:3306/test"),
resource.TestCheckResourceAttr("atlas_schema.testdb", "id", "mysql://root:pass@localhost:3306"),
),
},
{
Config: testAccActionConfigUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("atlas_schema.testdb", "id", "mysql://root:pass@localhost:3306/test"),
resource.TestCheckResourceAttr("atlas_schema.testdb", "id", "mysql://root:pass@localhost:3306"),
func(s *terraform.State) error {
res := s.RootModule().Resources["atlas_schema.testdb"]
cli, err := sqlclient.Open(context.TODO(), res.Primary.ID)
Expand All @@ -106,3 +106,65 @@ func TestAccAtlasDatabase(t *testing.T) {
},
})
}

func TestAccDestroySchemas(t *testing.T) {
// Create schemas "main" and "do-not-delete".
preExistingSchema := `resource "atlas_schema" "testdb" {
hcl = <<-EOT
schema "do-not-delete" {}
schema "main" {}
EOT
url = "mysql://root:pass@localhost:3306"
}`
// When the following destroys, it only deletes schema "main".
tfSchema := `resource "atlas_schema" "testdb" {
hcl = <<-EOT
table "orders" {
schema = schema.main
column "id" {
null = true
type = int
}
}
schema "main" {
}
EOT
url = "mysql://root:pass@localhost:3306/main"
}`
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"atlas": Provider(),
},
Steps: []resource.TestStep{
{
Config: preExistingSchema,
Destroy: false,
// ignore non-normalized schema
ExpectNonEmptyPlan: true,
},
{
Config: tfSchema,
// ignore non-normalized schema
ExpectNonEmptyPlan: true,
},
},
CheckDestroy: func(s *terraform.State) error {
url := "mysql://root:pass@localhost:3306"
cli, err := sqlclient.Open(context.Background(), url)
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' does not exist, but expected to not be destroyed.")
}
if _, ok := realm.Schema("main"); ok {
return fmt.Errorf("schema 'main' wasn't deleted.")
}
return nil
},
})
}
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ Use the navigation to the left to read about the available resources.
provider "atlas" {}
data "atlas_schema" "market" {
dev_db_url = "mysql://root:pass@localhost:3307/test"
dev_db_url = "mysql://root:pass@localhost:3307/market"
src = file("${path.module}/schema.hcl")
}
resource "atlas_schema" "market" {
hcl = data.atlas_schema.market.hcl
url = "mysql://root:pass@localhost:3306/test"
url = "mysql://root:pass@localhost:3306/market"
}
```

8 changes: 4 additions & 4 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "docker_container" "dev" {
name = "devdb"
env = [
"MYSQL_ROOT_PASSWORD=pass",
"MYSQL_DATABASE=test",
"MYSQL_DATABASE=market",
]
ports {
external = 3307
Expand All @@ -37,7 +37,7 @@ resource "docker_container" "prod" {
name = "proddb"
env = [
"MYSQL_ROOT_PASSWORD=pass",
"MYSQL_DATABASE=test",
"MYSQL_DATABASE=market",
]
ports {
external = 3306
Expand All @@ -47,12 +47,12 @@ resource "docker_container" "prod" {

data "atlas_schema" "market" {
depends_on = [ docker_container.dev ]
dev_db_url = "mysql://root:pass@localhost:3307/test"
dev_db_url = "mysql://root:pass@localhost:3307/market"
src = file("${path.module}/schema.hcl")
}

resource "atlas_schema" "market" {
depends_on = [ docker_container.prod ]
hcl = data.atlas_schema.market.hcl
url = "mysql://root:pass@localhost:3306/test"
url = "mysql://root:pass@localhost:3306/market"
}
4 changes: 2 additions & 2 deletions examples/provider/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
provider "atlas" {}

data "atlas_schema" "market" {
dev_db_url = "mysql://root:pass@localhost:3307/test"
dev_db_url = "mysql://root:pass@localhost:3307/market"
src = file("${path.module}/schema.hcl")
}

resource "atlas_schema" "market" {
hcl = data.atlas_schema.market.hcl
url = "mysql://root:pass@localhost:3306/test"
url = "mysql://root:pass@localhost:3306/market"
}
10 changes: 5 additions & 5 deletions examples/provider/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ resource "docker_container" "dev" {
name = "devdb"
env = [
"MYSQL_ROOT_PASSWORD=pass",
"MYSQL_DATABASE=test",
"MYSQL_DATABASE=market",
]
ports {
external = 3307
Expand All @@ -36,7 +36,7 @@ resource "docker_container" "prod" {
name = "proddb"
env = [
"MYSQL_ROOT_PASSWORD=pass",
"MYSQL_DATABASE=test",
"MYSQL_DATABASE=market",
]
ports {
external = 3306
Expand All @@ -46,13 +46,13 @@ resource "docker_container" "prod" {

data "atlas_schema" "market" {
depends_on = [ docker_container.dev ]
dev_db_url = "mysql://root:pass@localhost:3307/test"
dev_db_url = "mysql://root:pass@localhost:3307/market"
src = file("${path.module}/schema.hcl")
}

resource "atlas_schema" "market" {
depends_on = [ docker_container.prod ]
hcl = data.atlas_schema.market.hcl
url = "mysql://root:pass@localhost:3306/test"
dev_db_url = "mysql://root:pass@localhost:3307/test"
url = "mysql://root:pass@localhost:3306/market"
dev_db_url = "mysql://root:pass@localhost:3307/market"
}

0 comments on commit 8cd5081

Please sign in to comment.