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

[Bug Fix] Fixes PostgreSQL collation issues #396 and #760 #1255

Merged
merged 21 commits into from
May 24, 2018

Conversation

WodansSon
Copy link
Collaborator

NOTE: This PR is based on the u-postgreslq branch, once the u-postgreslq branch PR #1090 is merged into master this PR will only affect the following 4 files to fix the collation issues per #396 and #760

  • resource_arm_postgresql_database.go
  • validators.go
  • validators_test.go
  • utils.go

@WodansSon WodansSon changed the title [Bug Fix] Fixes collation issues #396 and #760 [Bug Fix] Fixes PostgreSQL collation issues #396 and #760 May 18, 2018
@katbyte
Copy link
Collaborator

katbyte commented May 19, 2018

Hi @jeffreyCline,

Thanks for fixing this 🙂 I am going to wait review this until the other 2 PRs are merged so the diff is only the 4 file to review.

@tombuildsstuff
Copy link
Contributor

tombuildsstuff commented May 23, 2018

@jeffreyCline FYI since I've merged the PostgreSQL PR I've rebased (well, merged since this was done previously) this PR so it now only contains the original changes :)

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @jeffreyCline

Thanks for this PR - I hope you don't mind but I've rebased/merged this since the PostgreSQL PR's been merged. I've taken a look through and left some comments inline, but this is off to a good start. If we can fix the commented issues (and the tests pass) this should be good to merge.

Thanks!

return
}

if strings.Contains(v, "-") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we'd be better using a Regex match here, it appears ^[A-Za-z0-9_. ]+$ should suffice for this?

@@ -372,6 +372,8 @@ func expandAzureRmPostgreSQLServerSku(d *schema.ResourceData) *postgresql.Sku {
skus := d.Get("sku").([]interface{})
sku := skus[0].(map[string]interface{})

fmt.Printf("[SKU]: %s", sku)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor can we remove this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOH! Sorry, that was part of my debug code... I forgot to remove it thanks for spotting it! :)

@@ -119,8 +120,7 @@ func resourceArmPostgreSQLDatabaseRead(d *schema.ResourceData, meta interface{})
d.Set("resource_group_name", resGroup)
d.Set("server_name", serverName)
d.Set("charset", resp.Charset)
d.Set("collation", resp.Collation)

d.Set("collation", utils.AzureRMNormalizeCollation(d.Get("collation").(string), "-", "_", -1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

providing we split this out to a separate line (to make it more readable) - we should be able to just use a string replacement directly? e.g.

collation := strings.Replace(d.Get(resp.Collation, "-", "_", -1)
d.Set("collation", collation)

note: in general we shouldn't be retrieving any properties from the schema in the Read function - since they won't exist during Import and can lead to crashes.

func AzureRMNormalizeCollation(input string, find string, replace string, count int) *string {
collation := strings.Replace(input, find, replace, count)
return &collation
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(which means we don't need this method)

@WodansSon
Copy link
Collaborator Author

I made all the requested changes, I also snuck a fix in for the charset issue... PostgreSQL had the same charset casing issues that MySQL did, except opposite.... PostgreSQL always returns uppercase, while MySQL always returns lowercase… go figure. I also added two test cases to validate the casing behavior, you can see the test results below.

Test Results

@WodansSon WodansSon dismissed tombuildsstuff’s stale review May 24, 2018 01:47

Requested changes made.

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor crash point we should fix (which I'll push a commit for) - but this otherwise LGTM 👍

@@ -119,7 +122,9 @@ func resourceArmPostgreSQLDatabaseRead(d *schema.ResourceData, meta interface{})
d.Set("resource_group_name", resGroup)
d.Set("server_name", serverName)
d.Set("charset", resp.Charset)
d.Set("collation", resp.Collation)

collation := strings.Replace(*resp.Collation, "-", "_", -1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a potential crash here, if resp.Collation is returned nil from the API - I'll push a commit to wrap this e.g.

if collation := resp.Collation; collation != nil {
  v := strings.Replace(collation, "-", "_", -1)
  d.Set("collation", v)
}

Also working around a potential crash. Tests pass:

```
$ acctests azurerm TestAccAzureRMPostgreSQLDatabase_
=== RUN   TestAccAzureRMPostgreSQLDatabase_importBasic
--- PASS: TestAccAzureRMPostgreSQLDatabase_importBasic (402.40s)
=== RUN   TestAccAzureRMPostgreSQLDatabase_basic
--- PASS: TestAccAzureRMPostgreSQLDatabase_basic (486.39s)
=== RUN   TestAccAzureRMPostgreSQLDatabase_charsetLowercase
--- PASS: TestAccAzureRMPostgreSQLDatabase_charsetLowercase (424.71s)
=== RUN   TestAccAzureRMPostgreSQLDatabase_charsetMixedcase
--- PASS: TestAccAzureRMPostgreSQLDatabase_charsetMixedcase (424.97s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	1738.507s
```
@tombuildsstuff
Copy link
Contributor

Tests pass after adding the crash fix / switching to use the .Properties fields (which matches the Azure response):

$ acctests azurerm TestAccAzureRMPostgreSQLDatabase_
=== RUN   TestAccAzureRMPostgreSQLDatabase_importBasic
--- PASS: TestAccAzureRMPostgreSQLDatabase_importBasic (402.40s)
=== RUN   TestAccAzureRMPostgreSQLDatabase_basic
--- PASS: TestAccAzureRMPostgreSQLDatabase_basic (486.39s)
=== RUN   TestAccAzureRMPostgreSQLDatabase_charsetLowercase
--- PASS: TestAccAzureRMPostgreSQLDatabase_charsetLowercase (424.71s)
=== RUN   TestAccAzureRMPostgreSQLDatabase_charsetMixedcase
--- PASS: TestAccAzureRMPostgreSQLDatabase_charsetMixedcase (424.97s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	1738.507s

@tombuildsstuff tombuildsstuff merged commit 6070152 into master May 24, 2018
@tombuildsstuff tombuildsstuff deleted the b-postgresql branch May 24, 2018 07:05
tombuildsstuff added a commit that referenced this pull request May 24, 2018
@katbyte katbyte added this to the 1.6.0 milestone May 25, 2018
@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants