Skip to content

Commit e88fba1

Browse files
Adding support for major version upgrade in cloud sql instance. (#6374) (#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) <[email protected]> Co-authored-by: Stephen Lewis (Burrows) <[email protected]> Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Stephen Lewis (Burrows) <[email protected]>
1 parent 5d40d62 commit e88fba1

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

.changelog/6374.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note:enhancement
2+
sql: added support for major version upgrade to `google_sql_database_instance ` resource
3+
4+
```

google/resource_sql_database_instance.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ is set to true.`,
518518
"database_version": {
519519
Type: schema.TypeString,
520520
Required: true,
521-
ForceNew: true,
522521
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.`,
523522
},
524523

@@ -1357,11 +1356,39 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
13571356
return err
13581357
}
13591358

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

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

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

1375-
var op *sqladmin.Operation
13761402
err = retryTimeDuration(func() (rerr error) {
13771403
op, rerr = config.NewSqlAdminClient(userAgent).Instances.Update(project, d.Get("name").(string), instance).Do()
13781404
return rerr

google/resource_sql_database_instance_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,36 @@ func TestAccSqlDatabaseInstance_SqlServerAuditConfig(t *testing.T) {
11951195
})
11961196
}
11971197

1198+
func TestAccSqlDatabaseInstance_mysqlMajorVersionUpgrade(t *testing.T) {
1199+
t.Parallel()
1200+
1201+
vcrTest(t, resource.TestCase{
1202+
PreCheck: func() { testAccPreCheck(t) },
1203+
Providers: testAccProviders,
1204+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
1205+
Steps: []resource.TestStep{
1206+
{
1207+
Config: testGoogleSqlDatabaseInstance_basic2,
1208+
},
1209+
{
1210+
ResourceName: "google_sql_database_instance.instance",
1211+
ImportState: true,
1212+
ImportStateVerify: true,
1213+
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
1214+
},
1215+
{
1216+
Config: testGoogleSqlDatabaseInstance_basic2_update,
1217+
},
1218+
{
1219+
ResourceName: "google_sql_database_instance.instance",
1220+
ImportState: true,
1221+
ImportStateVerify: true,
1222+
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
1223+
},
1224+
},
1225+
})
1226+
}
1227+
11981228
func TestAccSqlDatabaseInstance_sqlMysqlInstancePvpExample(t *testing.T) {
11991229
t.Parallel()
12001230

@@ -1253,6 +1283,17 @@ resource "google_sql_database_instance" "instance" {
12531283
}
12541284
`
12551285

1286+
var testGoogleSqlDatabaseInstance_basic2_update = `
1287+
resource "google_sql_database_instance" "instance" {
1288+
region = "us-central1"
1289+
database_version = "MYSQL_8_0"
1290+
deletion_protection = false
1291+
settings {
1292+
tier = "db-f1-micro"
1293+
}
1294+
}
1295+
`
1296+
12561297
var testGoogleSqlDatabaseInstance_basic3 = `
12571298
resource "google_sql_database_instance" "instance" {
12581299
name = "%s"

0 commit comments

Comments
 (0)