Skip to content

Commit

Permalink
Adding support for major version upgrade in cloud sql instance. (#6374)…
Browse files Browse the repository at this point in the history
… (#12338)

* Adding support for major version upgrade in cloud sql instance.

* Resolving Lint errors

* Minor changes to improve readability

* Op redeclared in the file

* Update resource_sql_database_instance.go.erb

* Added test for the database version upgrade and added appropriate comments.

* Update mmv1/third_party/terraform/tests/resource_sql_database_instance_test.go.erb

Co-authored-by: Stephen Lewis (Burrows) <stephen.r.burrows@gmail.com>

Co-authored-by: Stephen Lewis (Burrows) <stephen.r.burrows@gmail.com>
Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
Co-authored-by: Stephen Lewis (Burrows) <stephen.r.burrows@gmail.com>
  • Loading branch information
modular-magician and melinath authored Aug 18, 2022
1 parent 5d40d62 commit e88fba1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .changelog/6374.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
sql: added support for major version upgrade to `google_sql_database_instance ` resource

```
36 changes: 31 additions & 5 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ is set to true.`,
"database_version": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The MySQL, PostgreSQL or SQL Server (beta) version to use. Supported values include MYSQL_5_6, MYSQL_5_7, MYSQL_8_0, POSTGRES_9_6, POSTGRES_10, POSTGRES_11, POSTGRES_12, POSTGRES_13, POSTGRES_14, SQLSERVER_2017_STANDARD, SQLSERVER_2017_ENTERPRISE, SQLSERVER_2017_EXPRESS, SQLSERVER_2017_WEB. Database Version Policies includes an up-to-date reference of supported versions.`,
},

Expand Down Expand Up @@ -1357,11 +1356,39 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
return err
}

// Update only updates the settings, so they are all we need to set.
instance := &sqladmin.DatabaseInstance{
Settings: expandSqlDatabaseInstanceSettings(d.Get("settings").([]interface{})),
desiredSetting := d.Get("settings")
var op *sqladmin.Operation
var instance *sqladmin.DatabaseInstance
// Check if the database version is being updated, because patching database version is an atomic operation and can not be
// performed with other fields, we first patch database version before updating the rest of the fields.
if v, ok := d.GetOk("database_version"); ok {
instance = &sqladmin.DatabaseInstance{DatabaseVersion: v.(string)}
err = retryTimeDuration(func() (rerr error) {
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Patch(project, d.Get("name").(string), instance).Do()
return rerr
}, d.Timeout(schema.TimeoutUpdate), isSqlOperationInProgressError)
if err != nil {
return fmt.Errorf("Error, failed to patch instance settings for %s: %s", instance.Name, err)
}
err = sqlAdminOperationWaitTime(config, op, project, "Patch Instance", userAgent, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}
err = resourceSqlDatabaseInstanceRead(d, meta)
if err != nil {
return err
}
}

s := d.Get("settings")
instance = &sqladmin.DatabaseInstance{
Settings: expandSqlDatabaseInstanceSettings(desiredSetting.([]interface{})),
}
_settings := s.([]interface{})[0].(map[string]interface{})
// Instance.Patch operation on completion updates the settings proto version by +8. As terraform does not know this it tries
// to make an update call with the proto version before patch and fails. To resolve this issue we update the setting version
// before making the update call.
instance.Settings.SettingsVersion = int64(_settings["version"].(int))
// Collation cannot be included in the update request
instance.Settings.Collation = ""

Expand All @@ -1372,7 +1399,6 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
defer mutexKV.Unlock(instanceMutexKey(project, v.(string)))
}

var op *sqladmin.Operation
err = retryTimeDuration(func() (rerr error) {
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Update(project, d.Get("name").(string), instance).Do()
return rerr
Expand Down
41 changes: 41 additions & 0 deletions google/resource_sql_database_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,36 @@ func TestAccSqlDatabaseInstance_SqlServerAuditConfig(t *testing.T) {
})
}

func TestAccSqlDatabaseInstance_mysqlMajorVersionUpgrade(t *testing.T) {
t.Parallel()

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testGoogleSqlDatabaseInstance_basic2,
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
},
{
Config: testGoogleSqlDatabaseInstance_basic2_update,
},
{
ResourceName: "google_sql_database_instance.instance",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
},
},
})
}

func TestAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1253,6 +1283,17 @@ resource "google_sql_database_instance" "instance" {
}
`

var testGoogleSqlDatabaseInstance_basic2_update = `
resource "google_sql_database_instance" "instance" {
region = "us-central1"
database_version = "MYSQL_8_0"
deletion_protection = false
settings {
tier = "db-f1-micro"
}
}
`

var testGoogleSqlDatabaseInstance_basic3 = `
resource "google_sql_database_instance" "instance" {
name = "%s"
Expand Down

0 comments on commit e88fba1

Please sign in to comment.