Always remove the IS_TEMPLATE attribute before dropping a database.

This commit is contained in:
Sean Chittenden 2016-11-06 08:46:40 -08:00
parent e9b2b38288
commit 37fdc958b3
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16

View File

@ -91,7 +91,7 @@ func resourcePostgreSQLDatabase() *schema.Resource {
dbAllowConnsAttr: { dbAllowConnsAttr: {
Type: schema.TypeBool, Type: schema.TypeBool,
Optional: true, Optional: true,
Computed: true, Default: true,
Description: "If false then no one can connect to this database", Description: "If false then no one can connect to this database",
}, },
dbIsTemplateAttr: { dbIsTemplateAttr: {
@ -226,10 +226,7 @@ func resourcePostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{})
} }
for _, opt := range boolOpts { for _, opt := range boolOpts {
v, ok := d.GetOk(opt.hclKey) v := d.Get(opt.hclKey)
if !ok {
continue
}
valStr := "FALSE" valStr := "FALSE"
if val := v.(bool); val { if val := v.(bool); val {
@ -263,6 +260,19 @@ func resourcePostgreSQLDatabaseDelete(d *schema.ResourceData, meta interface{})
defer conn.Close() defer conn.Close()
dbName := d.Get(dbNameAttr).(string) dbName := d.Get(dbNameAttr).(string)
if isTemplate := d.Get(dbIsTemplateAttr).(bool); isTemplate {
// Template databases must have this attribute cleared before
// they can be dropped.
if err := doSetDBIsTemplate(conn, dbName, false); err != nil {
return errwrap.Wrapf("Error updating database IS_TEMPLATE during DROP DATABASE: {{err}}", err)
}
}
if err := setDBIsTemplate(conn, d); err != nil {
return err
}
query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName)) query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName))
_, err = conn.Query(query) _, err = conn.Query(query)
if err != nil { if err != nil {
@ -476,8 +486,14 @@ func setDBIsTemplate(conn *sql.DB, d *schema.ResourceData) error {
return nil return nil
} }
isTemplate := d.Get(dbIsTemplateAttr).(bool) if err := doSetDBIsTemplate(conn, d.Get(dbNameAttr).(string), d.Get(dbIsTemplateAttr).(bool)); err != nil {
dbName := d.Get(dbNameAttr).(string) return errwrap.Wrapf("Error updating database IS_TEMPLATE: {{err}}", err)
}
return nil
}
func doSetDBIsTemplate(conn *sql.DB, dbName string, isTemplate bool) error {
query := fmt.Sprintf("ALTER DATABASE %s IS_TEMPLATE %t", pq.QuoteIdentifier(dbName), isTemplate) query := fmt.Sprintf("ALTER DATABASE %s IS_TEMPLATE %t", pq.QuoteIdentifier(dbName), isTemplate)
if _, err := conn.Query(query); err != nil { if _, err := conn.Query(query); err != nil {
return errwrap.Wrapf("Error updating database IS_TEMPLATE: {{err}}", err) return errwrap.Wrapf("Error updating database IS_TEMPLATE: {{err}}", err)