From 6bb5ef40223a69fac3feaee97f474382c174aa4a Mon Sep 17 00:00:00 2001 From: Xu Wu Date: Mon, 20 Jan 2025 20:21:35 +0800 Subject: [PATCH] pg server support in-place major version upgrade --- .../postgresql_flexible_server_resource.go | 33 ++++++++----------- .../postgresql_flexible_server.html.markdown | 4 +-- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_resource.go b/internal/services/postgres/postgresql_flexible_server_resource.go index 7d5b7eb89cd4..6182127be373 100644 --- a/internal/services/postgres/postgresql_flexible_server_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_resource.go @@ -335,31 +335,26 @@ func resourcePostgresqlFlexibleServer() *pluginsdk.Resource { }, CustomizeDiff: pluginsdk.CustomDiffWithAll(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error { - createModeVal := d.Get("create_mode").(string) - if createModeVal == string(servers.CreateModeUpdate) { + // as create_mode should never change after creation, we can ignore it when checking for version changes + if d.HasChange("version") { oldVersionVal, newVersionVal := d.GetChange("version") + // `version` value has been validated already, ignore the parse errors is safe + oldVersion, _ := strconv.ParseInt(oldVersionVal.(string), 10, 32) + newVersion, _ := strconv.ParseInt(newVersionVal.(string), 10, 32) - if oldVersionVal != "" && newVersionVal != "" { - oldVersion, err := strconv.ParseInt(oldVersionVal.(string), 10, 32) - if err != nil { - return err - } - - newVersion, err := strconv.ParseInt(newVersionVal.(string), 10, 32) - if err != nil { - return err - } - - if oldVersion < newVersion { - return nil - } + if oldVersion > newVersion { + d.ForceNew("version") } } - d.ForceNew("create_mode") - d.ForceNew("version") - + if d.HasChange("create_mode") { + // if create_mode is set to `Update`, then we can update it rather than force new + if d.Get("create_mode").(string) != string(servers.CreateModeForUpdateUpdate) { + return nil + } + d.ForceNew("create_mode") + } return nil }, func(ctx context.Context, diff *pluginsdk.ResourceDiff, v interface{}) error { oldLoginName, _ := diff.GetChange("administrator_login") diff --git a/website/docs/r/postgresql_flexible_server.html.markdown b/website/docs/r/postgresql_flexible_server.html.markdown index 9f7e6dacb054..f71f1aafbf8a 100644 --- a/website/docs/r/postgresql_flexible_server.html.markdown +++ b/website/docs/r/postgresql_flexible_server.html.markdown @@ -109,9 +109,7 @@ The following arguments are supported: * `create_mode` - (Optional) The creation mode which can be used to restore or replicate existing servers. Possible values are `Default`, `GeoRestore`, `PointInTimeRestore`, `Replica` and `Update`. Changing this forces a new PostgreSQL Flexible Server to be created. --> **Note:** `create_mode` cannot be changed once it's set since it's a parameter at creation. - --> **Note:** While creating the resource, `create_mode` cannot be set to `Update`. +-> **Note:** `create_mode` cannot be changed to other values than `Update`, it's set since it's a parameter at creation. It's optional to change it to `Update` when do a major version upgrade. * `delegated_subnet_id` - (Optional) The ID of the virtual network subnet to create the PostgreSQL Flexible Server. The provided subnet should not have any other resource deployed in it and this subnet will be delegated to the PostgreSQL Flexible Server, if not already delegated. Changing this forces a new PostgreSQL Flexible Server to be created.