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

Remove empty lines from view_definition to avoid unecessary recreation of resources #3361

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

alvaroqueiroz
Copy link

@alvaroqueiroz alvaroqueiroz commented Mar 11, 2024

Changes

This PR solves the problem described here: #3330

empty new lines on view_definition cause terraform to plan to change the view definition.
This PR adds the removal of empty lines to DiffSuppressFunc to avoid this problem

Tests

  • make test run locally
  • relevant change in docs/ folder
  • covered with integration tests in internal/acceptance
  • relevant acceptance tests are passing
  • using Go SDK

image

image

@alvaroqueiroz alvaroqueiroz requested review from a team as code owners March 11, 2024 20:48
@alvaroqueiroz alvaroqueiroz requested review from mgyucht and removed request for a team March 11, 2024 20:48
@alvaroqueiroz alvaroqueiroz changed the title remove empty new lines to test for state change Remove empty lines from view_definition to avoid unecessary recreation of resources Mar 11, 2024
@alvaroqueiroz
Copy link
Author

hello @mgyucht do you think this is ready for review?

Copy link
Contributor

@mgyucht mgyucht left a comment

Choose a reason for hiding this comment

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

This looks good to me. @alexott Do you have any concerns with this?

Only thing is that we should have one integration test to verify this behaves as expected and prevent regression. Look at internal/acceptance/sql_table_test.go. If you install the Databricks CLI (https://github.com/databricks/cli) and login to your workspace, you should be able to run any test. Can you try to add a test for view definition with multiple lines?

@alexott
Copy link
Contributor

alexott commented Mar 13, 2024

My main concern is things like this:

line1\nline2" -> "line1line2

This will break view definitions like this, where it's split between multiple lines:

select col1
from table

will become:

select col1from table

This really should be discussed with the SQL engineering team if it makes sense to do it.

But from the error description, the issue is caused by empty lines, not by newlines, so the replacement should be from this regex \n{1,} -> \n. Also it makes sense to check if it's empty line, vs line with spaces only.

@alvaroqueiroz
Copy link
Author

alvaroqueiroz commented Mar 13, 2024

hello @alexott,
this change would not break line1\nline2" -> "line1line2
I have even a test for that

this will remove both, empty lines and empty lines with spaces, both would cause problems

@alvaroqueiroz
Copy link
Author

I will add the integration test later today @mgyucht

@alvaroqueiroz
Copy link
Author

@mgyucht I have added one acceptance test.
I have added two steps, one creates the view and the second just adds spaces and empty lines to the view definition. It did not generate alter tables as it would with SuppressDiffWhitespaceChange

Let me know if I should make changes to this test

@alvaroqueiroz
Copy link
Author

hello @mgyucht
do you think it is worth proceeding with the review of this PR or I should close it?
I just wanted to fix this bug, but if the team thinks this is not the best solution, I'm open to discussing one that fits

@nkvuong
Copy link
Contributor

nkvuong commented Mar 27, 2024

@alvaroqueiroz could you resolve the merge conflict for this one?

For this change, could you add a unit test that validates there would not be a change when there is diff between InstanceState and HCL?

@alvaroqueiroz
Copy link
Author

done @nkvuong
let me know if you need anything else

@nkvuong
Copy link
Contributor

nkvuong commented Mar 27, 2024

@alvaroqueiroz the conflict resolution for internal/acceptance/sql_table_test.go needs a small fix - could you take a look?

@alvaroqueiroz
Copy link
Author

@nkvuong I have synced my fork with the master,
the conflict is still present?

@nkvuong
Copy link
Contributor

nkvuong commented Mar 28, 2024

@alvaroqueiroz
Copy link
Author

alvaroqueiroz commented Mar 28, 2024

@nkvuong fixed

image

@nkvuong
Copy link
Contributor

nkvuong commented Mar 28, 2024

this test TestResourceSqlTableUpdateView_IgnoreNewlineInDefinition is failing

@alvaroqueiroz
Copy link
Author

@nkvuong I think I'm not setting up the test correctly for some reason.
It seems to me that the Update flag will trigger an update regardless of the supressdiff method.

Even If I use the old SuppressDiffWhitespaceChange with just space changes, it will alter the table.
Do you have tips on how I could fix my tests?

@nkvuong
Copy link
Contributor

nkvuong commented Apr 3, 2024

@alvaroqueiroz for unit testing, you could follow the examples of TestCatalogSuppressCaseSensitivity, which tests that the Diff calculated by Terraform is correct

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.53%. Comparing base (109361e) to head (8811e29).
Report is 8 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3361      +/-   ##
==========================================
+ Coverage   83.51%   83.53%   +0.02%     
==========================================
  Files         178      178              
  Lines       16708    16718      +10     
==========================================
+ Hits        13954    13966      +12     
+ Misses       1901     1900       -1     
+ Partials      853      852       -1     
Files Coverage Δ
catalog/resource_sql_table.go 89.16% <100.00%> (ø)
common/util.go 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

@alvaroqueiroz
Copy link
Author

just fyi @nkvuong the test is passing because of this line

I will get the test with ExpectedDiff in place

@alvaroqueiroz
Copy link
Author

@nkvuong I have tested with ExpectedDiff, I see the same behaviour.
Not even the SuppressDiffWhitespaceChange (the one in prod today) works on the tests

image

image

func TestResourceSqlTableUpdateView_IgnoreNewlineInDefinition(t *testing.T) {
	qa.ResourceFixture{
		InstanceState: map[string]string{
			"name":            "barview",
			"catalog_name":    "main",
			"schema_name":     "foo",
			"table_type":      "VIEW",
			"cluster_id":      "existingcluster",
			"view_definition": "SELECT * FROM main.foo.bar",
		},
		ExpectedDiff: map[string]*terraform.ResourceAttrDiff{
			"catalog_name": {
				Old:         "main",
				New:         "main",
				RequiresNew: false,
			},
			"cluster_id": {
				Old:         "existingcluster",
				New:         "existingcluster",
				RequiresNew: false,
			},
			"name": {
				Old:         "barview",
				New:         "barview",
				RequiresNew: false,
			},
			"schema_name": {
				Old:         "foo",
				New:         "foo",
				RequiresNew: false,
			},
			"table_type": {
				Old:         "VIEW",
				New:         "VIEW",
				RequiresNew: false,
			},
			"view_definition": {
				Old:         "SELECT * FROM main.foo.bar",
				New:         "SELECT * FROM main.foo.bar",
				NewComputed: false,
				RequiresNew: false,
				NewRemoved:  false,
				Sensitive:   false,
			},
			"column.#": {
				Old:         "",
				New:         "",
				NewComputed: true,
				RequiresNew: true,
			},
			"properties.%": {
				Old:         "",
				New:         "",
				NewComputed: true,
				RequiresNew: false,
			},
		},
		HCL: `
		name               = "barview"
		catalog_name       = "main"
		schema_name        = "foo"
		table_type         = "VIEW"
		cluster_id         = "existingcluster"
		view_definition    = "SELECT * FROM main.foo.bar "
		`,
		Resource: ResourceSqlTable(),
		ID:       "main.foo.barview",
	}.ApplyNoError(t)
}

@vsluc
Copy link

vsluc commented Apr 15, 2024

@alvaroqueiroz Appreciate your work. I see a lot of value in this fix, as this unnecessary detection of changes increases the apply time significantly. Also, the recreation of the views with no change is not desirable, as it could run into contention with active ETLs that consume data from the views.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants