Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
postgresql_schema: Add drop_cascade option.
Browse files Browse the repository at this point in the history
This allows to delete cascade all dependent objects while deleting a schema.
  • Loading branch information
cyrilgdn committed Dec 20, 2019
1 parent 8445816 commit 774915e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ FEATURES:
* `postgresql_schema`: Add `database` attribute.
([#100](https://github.com/terraform-providers/terraform-provider-postgresql/pull/100))

* `postgresql_schema`: Add `drop_cascade` attribute.
([#108](https://github.com/terraform-providers/terraform-provider-postgresql/pull/108))


## 1.3.0 (November 01, 2019)

Expand Down
15 changes: 13 additions & 2 deletions postgresql/resource_postgresql_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
schemaOwnerAttr = "owner"
schemaPolicyAttr = "policy"
schemaIfNotExists = "if_not_exists"
schemaDropCascade = "drop_cascade"

schemaPolicyCreateAttr = "create"
schemaPolicyCreateWithGrantAttr = "create_with_grant"
Expand Down Expand Up @@ -64,6 +65,12 @@ func resourcePostgreSQLSchema() *schema.Resource {
Default: true,
Description: "When true, use the existing schema if it exists",
},
schemaDropCascade: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "When true, will also drop all the objects that are contained in the schema",
},
schemaPolicyAttr: {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -213,8 +220,12 @@ func resourcePostgreSQLSchemaDelete(d *schema.ResourceData, meta interface{}) er

schemaName := d.Get(schemaNameAttr).(string)

// NOTE(sean@): Deliberately not performing a cascading drop.
sql := fmt.Sprintf("DROP SCHEMA %s", pq.QuoteIdentifier(schemaName))
dropMode := "RESTRICT"
if d.Get(schemaDropCascade).(bool) {
dropMode = "CASCADE"
}

sql := fmt.Sprintf("DROP SCHEMA %s %s", pq.QuoteIdentifier(schemaName), dropMode)
if _, err = txn.Exec(sql); err != nil {
return errwrap.Wrapf("Error deleting schema: {{err}}", err)
}
Expand Down
41 changes: 41 additions & 0 deletions postgresql/resource_postgresql_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ func TestAccPostgresqlSchema_Database(t *testing.T) {
})
}

func TestAccPostgresqlSchema_DropCascade(t *testing.T) {
var testAccPostgresqlSchemaConfig = `
resource "postgresql_schema" "test_restrict" {
name = "foo"
drop_cascade = true
}
`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPostgresqlSchemaDestroy,
Steps: []resource.TestStep{
{
Config: testAccPostgresqlSchemaConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckPostgresqlSchemaExists("postgresql_schema.test_restrict", "foo"),
resource.TestCheckResourceAttr("postgresql_schema.test_restrict", "name", "foo"),

// This will create a table in the schema to check if the drop will work thanks to the cascade
testAccCreateSchemaTable("foo"),
),
},
},
})
}

func testAccCheckPostgresqlSchemaDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*Client)

Expand Down Expand Up @@ -284,6 +310,21 @@ func checkSchemaExists(txn *sql.Tx, schemaName string) (bool, error) {
return true, nil
}

func testAccCreateSchemaTable(schemaName string) resource.TestCheckFunc {
return func(s *terraform.State) error {

client := testAccProvider.Meta().(*Client)
db := client.DB()

_, err := db.Exec(fmt.Sprintf("CREATE TABLE %s.test_table (id serial)", schemaName))
if err != nil {
return fmt.Errorf("could not create test table in schema %s: %s", schemaName, err)
}

return nil
}
}

const testAccPostgresqlSchemaConfig = `
resource "postgresql_role" "role_all_without_grant" {
name = "role_all_without_grant"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/postgresql_schema.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ resource "postgresql_schema" "my_schema" {
database instance where it is configured.
* `owner` - (Optional) The ROLE who owns the schema.
* `if_not_exists` - (Optional) When true, use the existing schema if it exists. (Default: true)
* `drop_cascade` - (Optional) When true, will also drop all the objects that are contained in the schema. (Default: false)
* `policy` - (Optional) Can be specified multiple times for each policy. Each
policy block supports fields documented below.

Expand Down

0 comments on commit 774915e

Please sign in to comment.