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

postgresql_schema: Add drop_cascade option. #108

Merged
merged 1 commit into from
Dec 23, 2019
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ FEATURES:
([#103](https://github.com/terraform-providers/terraform-provider-postgresql/pull/103))
This allows to disable the version detection which requires a database connection, so plan on empty state does not require a connection.

* `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_cascade" {
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_cascade", "foo"),
resource.TestCheckResourceAttr("postgresql_schema.test_cascade", "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