Skip to content

Commit 8524f35

Browse files
multanicyrilgdn
authored andcommitted
Support DROP EXTENSION xx CASCADE (#167)
* Support DROP EXTENSION xx CASCADE This drops the extension even if it has dependencies. * Skip test incompatible with RDS-like environment
1 parent 04979fe commit 8524f35

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

postgresql/resource_postgresql_extension.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
)
1414

1515
const (
16-
extNameAttr = "name"
17-
extSchemaAttr = "schema"
18-
extVersionAttr = "version"
19-
extDatabaseAttr = "database"
16+
extNameAttr = "name"
17+
extSchemaAttr = "schema"
18+
extVersionAttr = "version"
19+
extDatabaseAttr = "database"
20+
extDropCascadeAttr = "drop_cascade"
2021
)
2122

2223
func resourcePostgreSQLExtension() *schema.Resource {
@@ -55,6 +56,12 @@ func resourcePostgreSQLExtension() *schema.Resource {
5556
ForceNew: true,
5657
Description: "Sets the database to add the extension to",
5758
},
59+
extDropCascadeAttr: {
60+
Type: schema.TypeBool,
61+
Optional: true,
62+
Default: false,
63+
Description: "When true, will also drop all the objects that depend on the extension, and in turn all objects that depend on those objects",
64+
},
5865
},
5966
}
6067
}
@@ -225,7 +232,12 @@ func resourcePostgreSQLExtensionDelete(d *schema.ResourceData, meta interface{})
225232
}
226233
defer deferredRollback(txn)
227234

228-
sql := fmt.Sprintf("DROP EXTENSION %s", pq.QuoteIdentifier(extName))
235+
dropMode := "RESTRICT"
236+
if d.Get(extDropCascadeAttr).(bool) {
237+
dropMode = "CASCADE"
238+
}
239+
240+
sql := fmt.Sprintf("DROP EXTENSION %s %s ", pq.QuoteIdentifier(extName), dropMode)
229241
if _, err := txn.Exec(sql); err != nil {
230242
return err
231243
}

postgresql/resource_postgresql_extension_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,52 @@ func TestAccPostgresqlExtension_Database(t *testing.T) {
209209
})
210210
}
211211

212+
func TestAccPostgresqlExtension_DropCascade(t *testing.T) {
213+
skipIfNotAcc(t)
214+
215+
var testAccPostgresqlExtensionConfig = `
216+
resource "postgresql_extension" "cascade" {
217+
name = "pgcrypto"
218+
drop_cascade = true
219+
}
220+
`
221+
resource.Test(t, resource.TestCase{
222+
PreCheck: func() {
223+
testAccPreCheck(t)
224+
testCheckCompatibleVersion(t, featureExtension)
225+
testSuperuserPreCheck(t)
226+
},
227+
Providers: testAccProviders,
228+
CheckDestroy: testAccCheckPostgresqlExtensionDestroy,
229+
Steps: []resource.TestStep{
230+
{
231+
Config: testAccPostgresqlExtensionConfig,
232+
Check: resource.ComposeTestCheckFunc(
233+
testAccCheckPostgresqlExtensionExists("postgresql_extension.cascade"),
234+
resource.TestCheckResourceAttr("postgresql_extension.cascade", "name", "pgcrypto"),
235+
// This will create a dependency on the extension.
236+
testAccCreateExtensionDependency("test_extension_cascade"),
237+
),
238+
},
239+
},
240+
})
241+
}
242+
243+
func testAccCreateExtensionDependency(tableName string) resource.TestCheckFunc {
244+
return func(s *terraform.State) error {
245+
246+
client := testAccProvider.Meta().(*Client)
247+
db := client.DB()
248+
249+
_, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s; CREATE TABLE %s (id uuid DEFAULT gen_random_uuid())", tableName, tableName))
250+
if err != nil {
251+
return fmt.Errorf("could not create test table in schema: %s", err)
252+
}
253+
254+
return nil
255+
}
256+
}
257+
212258
var testAccPostgresqlExtensionConfig = `
213259
resource "postgresql_extension" "myextension" {
214260
name = "pg_trgm"

0 commit comments

Comments
 (0)