Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for major version upgrade in cloud sql instance. #6374

Merged
merged 9 commits into from
Aug 18, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,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 @@ -867,7 +866,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})

cloneContext, cloneSource := expandCloneContext(d.Get("clone").([]interface{}))

s, ok := d.GetOk("settings")
s, ok := d.GetOk("settings")
desiredSettings := expandSqlDatabaseInstanceSettings(s.([]interface{}))
if ok {
instance.Settings = desiredSettings
Expand Down Expand Up @@ -1359,11 +1358,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()
usingh83 marked this conversation as resolved.
Show resolved Hide resolved
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))
usingh83 marked this conversation as resolved.
Show resolved Hide resolved
// Collation cannot be included in the update request
instance.Settings.Collation = ""

Expand All @@ -1374,7 +1401,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
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,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 @@ -1255,6 +1285,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