-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schema update support to spanner db 2082 (#3947)
* eoncders and customdiff added for spanner DB ddl update * config update test case added * customdiff modified to handle out-of-index issue * new lines added * indent fixed * indent fixed for tests * test added for ddl update condition * mock added Terraformresourcediff, unit tests added * test fixed * more unit tests added * tests fixed * PR comments implemented * unit tests converted to table driven tests * ImportStateVerifyIgnore flag added to tests * syntax corrected in test
- Loading branch information
1 parent
727e17f
commit c32422a
Showing
8 changed files
with
241 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<%- # the license inside this block applies to this file | ||
# Copyright 2020 Google Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-%> | ||
// customizeDiff func for additional checks on google_spanner_database properties: | ||
func resourceSpannerDBDdlCustomDiffFunc(diff TerraformResourceDiff) error { | ||
old, new := diff.GetChange("ddl") | ||
oldDdls := old.([]interface{}) | ||
newDdls := new.([]interface{}) | ||
var err error | ||
|
||
if len(newDdls) < len(oldDdls) { | ||
err = diff.ForceNew("ddl") | ||
if err != nil { | ||
return fmt.Errorf("ForceNew failed for ddl, old - %v and new - %v", oldDdls, newDdls) | ||
} | ||
return nil | ||
} | ||
|
||
for i := range oldDdls { | ||
if newDdls[i].(string) != oldDdls[i].(string) { | ||
err = diff.ForceNew("ddl") | ||
if err != nil { | ||
return fmt.Errorf("ForceNew failed for ddl, old - %v and new - %v", oldDdls, newDdls) | ||
} | ||
return nil | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func resourceSpannerDBDdlCustomDiff(diff *schema.ResourceDiff, meta interface{}) error { | ||
// separate func to allow unit testing | ||
return resourceSpannerDBDdlCustomDiffFunc(diff) | ||
} |
15 changes: 15 additions & 0 deletions
15
templates/terraform/resource_definition/spanner_database.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<%# The license inside this block applies to this file. | ||
# Copyright 2020 Google Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-%> | ||
CustomizeDiff: resourceSpannerDBDdlCustomDiff, |
29 changes: 29 additions & 0 deletions
29
templates/terraform/update_encoder/spanner_database.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<%# The license inside this block applies to this file. | ||
# Copyright 2020 Google Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
-%> | ||
old, new := d.GetChange("ddl") | ||
oldDdls := old.([]interface{}) | ||
newDdls := new.([]interface{}) | ||
updateDdls := []string{} | ||
|
||
//Only new ddl statments to be add to update call | ||
for i := len(oldDdls); i < len(newDdls); i++ { | ||
updateDdls = append(updateDdls, newDdls[i].(string)) | ||
} | ||
|
||
obj["statements"] = strings.Join(updateDdls, ",") | ||
delete(obj, "name") | ||
delete(obj, "instance") | ||
delete(obj, "extraStatements") | ||
return obj, nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters