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
1 changed files with 23 additions and 7 deletions

View File

@ -91,7 +91,7 @@ func resourcePostgreSQLDatabase() *schema.Resource {
dbAllowConnsAttr: {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Default: true,
Description: "If false then no one can connect to this database",
},
dbIsTemplateAttr: {
@ -226,10 +226,7 @@ func resourcePostgreSQLDatabaseCreate(d *schema.ResourceData, meta interface{})
}
for _, opt := range boolOpts {
v, ok := d.GetOk(opt.hclKey)
if !ok {
continue
}
v := d.Get(opt.hclKey)
valStr := "FALSE"
if val := v.(bool); val {
@ -263,6 +260,19 @@ func resourcePostgreSQLDatabaseDelete(d *schema.ResourceData, meta interface{})
defer conn.Close()
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))
_, err = conn.Query(query)
if err != nil {
@ -476,8 +486,14 @@ func setDBIsTemplate(conn *sql.DB, d *schema.ResourceData) error {
return nil
}
isTemplate := d.Get(dbIsTemplateAttr).(bool)
dbName := d.Get(dbNameAttr).(string)
if err := doSetDBIsTemplate(conn, d.Get(dbNameAttr).(string), d.Get(dbIsTemplateAttr).(bool)); err != nil {
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)
if _, err := conn.Query(query); err != nil {
return errwrap.Wrapf("Error updating database IS_TEMPLATE: {{err}}", err)