Add the version attribute to postgresql_extension.

This commit is contained in:
Sean Chittenden 2016-12-12 14:37:40 -08:00
parent a4965c01af
commit 8c41f0859b
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16
2 changed files with 53 additions and 4 deletions

View File

@ -13,8 +13,9 @@ import (
)
const (
extNameAttr = "name"
extSchemaAttr = "schema"
extNameAttr = "name"
extSchemaAttr = "schema"
extVersionAttr = "version"
)
func resourcePostgreSQLExtension() *schema.Resource {
@ -39,6 +40,12 @@ func resourcePostgreSQLExtension() *schema.Resource {
Computed: true,
Description: "Sets the schema of an extension",
},
extVersionAttr: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Sets the version number of the extension",
},
},
}
}
@ -60,6 +67,10 @@ func resourcePostgreSQLExtensionCreate(d *schema.ResourceData, meta interface{})
fmt.Fprint(b, " SCHEMA ", pq.QuoteIdentifier(v.(string)))
}
if v, ok := d.GetOk(extVersionAttr); ok {
fmt.Fprint(b, " VERSION ", pq.QuoteIdentifier(v.(string)))
}
query := b.String()
_, err = conn.Query(query)
if err != nil {
@ -80,8 +91,8 @@ func resourcePostgreSQLExtensionRead(d *schema.ResourceData, meta interface{}) e
defer conn.Close()
extID := d.Id()
var extName, extSchema string
err = conn.QueryRow("SELECT e.extname, n.nspname FROM pg_catalog.pg_extension e, pg_catalog.pg_namespace n WHERE n.oid = e.extnamespace AND e.extname = $1", extID).Scan(&extName, &extSchema)
var extName, extSchema, extVersion string
err = conn.QueryRow("SELECT e.extname, n.nspname, e.extversion FROM pg_catalog.pg_extension e, pg_catalog.pg_namespace n WHERE n.oid = e.extnamespace AND e.extname = $1", extID).Scan(&extName, &extSchema, &extVersion)
switch {
case err == sql.ErrNoRows:
log.Printf("[WARN] PostgreSQL extension (%s) not found", d.Id())
@ -92,6 +103,7 @@ func resourcePostgreSQLExtensionRead(d *schema.ResourceData, meta interface{}) e
default:
d.Set(extNameAttr, extName)
d.Set(extSchemaAttr, extSchema)
d.Set(extVersionAttr, extVersion)
d.SetId(extName)
return nil
}
@ -132,6 +144,10 @@ func resourcePostgreSQLExtensionUpdate(d *schema.ResourceData, meta interface{})
return err
}
if err := setExtVersion(conn, d); err != nil {
return err
}
return resourcePostgreSQLExtensionRead(d, meta)
}
@ -154,3 +170,27 @@ func setExtSchema(conn *sql.DB, d *schema.ResourceData) error {
return nil
}
func setExtVersion(conn *sql.DB, d *schema.ResourceData) error {
if !d.HasChange(extVersionAttr) {
return nil
}
extID := d.Id()
b := bytes.NewBufferString("ALTER EXTENSION ")
fmt.Fprintf(b, "%s UPDATE", pq.QuoteIdentifier(extID))
_, nraw := d.GetChange(extVersionAttr)
n := nraw.(string)
if n != "" {
fmt.Fprintf(b, " TO %s", pq.QuoteIdentifier(n))
}
query := b.String()
if _, err := conn.Query(query); err != nil {
return errwrap.Wrapf("Error updating extension version: {{err}}", err)
}
return nil
}

View File

@ -21,6 +21,15 @@ func TestAccPostgresqlExtension_Basic(t *testing.T) {
testAccCheckPostgresqlExtensionExists("postgresql_extension.myextension"),
resource.TestCheckResourceAttr(
"postgresql_extension.myextension", "name", "pg_trgm"),
resource.TestCheckResourceAttr(
"postgresql_extension.myextension", "schema", "public"),
// NOTE(sean): Version 1.3 is what's
// shipped with PostgreSQL 9.6.1. This
// version number may drift in the
// future.
resource.TestCheckResourceAttr(
"postgresql_extension.myextension", "version", "1.3"),
),
},
},