diff --git a/builtin/providers/postgresql/resource_postgresql_extension.go b/builtin/providers/postgresql/resource_postgresql_extension.go index 37df80918..240de3eb3 100644 --- a/builtin/providers/postgresql/resource_postgresql_extension.go +++ b/builtin/providers/postgresql/resource_postgresql_extension.go @@ -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 +} diff --git a/builtin/providers/postgresql/resource_postgresql_extension_test.go b/builtin/providers/postgresql/resource_postgresql_extension_test.go index a41a3496b..7deb71a39 100644 --- a/builtin/providers/postgresql/resource_postgresql_extension_test.go +++ b/builtin/providers/postgresql/resource_postgresql_extension_test.go @@ -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"), ), }, },