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

Allow custom types as indexes when joining tables #197

Merged
merged 4 commits into from
Dec 17, 2022

Conversation

emarj
Copy link
Contributor

@emarj emarj commented Dec 16, 2022

See #198

The change is quite simple. We check if the field is tagged as primary key, if so, we add it as an index regardless of its type.

@codecov-commenter
Copy link

codecov-commenter commented Dec 16, 2022

Codecov Report

Merging #197 (31cddca) into master (4873e43) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #197      +/-   ##
==========================================
- Coverage   92.76%   92.76%   -0.01%     
==========================================
  Files         116      116              
  Lines        6929     6927       -2     
==========================================
- Hits         6428     6426       -2     
  Misses        400      400              
  Partials      101      101              
Impacted Files Coverage Δ
qrm/scan_context.go 94.82% <100.00%> (-0.06%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@go-jet
Copy link
Owner

go-jet commented Dec 16, 2022

Good catch. 💯 Could you also include one test in ./tests/postgres (or mysql).

}
if !isPrimaryKey(field, primaryKeyOverwrites) {
if !isSimpleModelType(fieldType) {
if fieldType.Kind() != reflect.Struct {
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe we can reorder this condition, to avoid two ifs with negations.

if isPrimaryKey(...) {
...
} else !isSimpleModelType(fieldType) {
...
}

Copy link
Owner

Choose a reason for hiding this comment

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

Actually it can be just:

if isPrimaryKey(...) {
...
} else fieldType.Kind() == reflect.Struct {
...
}

Copy link
Contributor Author

@emarj emarj Dec 16, 2022

Choose a reason for hiding this comment

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

Yes you are right. Done!

@emarj emarj force-pushed the custom-types-indexes-pk branch from 6dfde03 to 90d8199 Compare December 16, 2022 22:17
@emarj emarj force-pushed the custom-types-indexes-pk branch from 90d8199 to 78ed3fd Compare December 16, 2022 22:17
@emarj
Copy link
Contributor Author

emarj commented Dec 16, 2022

I added the test. Unfortunately I was not able to properly configure the test environment on my local machine. I hope it passes on CI.

require.NoError(t, err)
require.Equal(t, len(dest), 2)
testutils.AssertJSON(t, dest, `
[
Copy link
Owner

@go-jet go-jet Dec 17, 2022

Choose a reason for hiding this comment

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

It seems test fails because json doesn't match. Expected json:

[
	{
		"CityID": {
			"Int64": 312,
			"Valid": true
		},
		"CityName": "London",
		"Customers": [
			{
				"CustomerID": {
					"Int64": 252,
					"Valid": true
				},
				"LastName": "Hoffman",
				"Address": {
					"AddressID": {
						"Int64": 256,
						"Valid": true
					},
					"AddressLine": "1497 Yuzhou Drive"
				}
			},
			{
				"CustomerID": {
					"Int64": 512,
					"Valid": true
				},
				"LastName": "Vines",
				"Address": {
					"AddressID": {
						"Int64": 517,
						"Valid": true
					},
					"AddressLine": "548 Uruapan Street"
				}
			}
		]
	},
	{
		"CityID": {
			"Int64": 589,
			"Valid": true
		},
		"CityName": "York",
		"Customers": [
			{
				"CustomerID": {
					"Int64": 497,
					"Valid": true
				},
				"LastName": "Sledge",
				"Address": {
					"AddressID": {
						"Int64": 502,
						"Valid": true
					},
					"AddressLine": "1515 Korla Way"
				}
			}
		]
	}
]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was silly on my part. I am so used to null.Int by github.com/guregu/null that I forgot about the JSON marshaling of sql.NullInt64. I also added another test by using the aforementioned null.Int.

I was able to configure the environment to run some test, even tough not everything works. I had to pass trough some hoops to make it work on my M1 Mac. In my case platform: linux/amd64 was needed in docker-compose.yaml.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, I've heard M1 Mac tends to have problems with docker. Maybe we can include your docker-compose.yaml changes. M1 is getting popular, someone else might stumble on the same issue. It can be a separate PR.

@emarj emarj marked this pull request as ready for review December 17, 2022 16:30
@go-jet
Copy link
Owner

go-jet commented Dec 17, 2022

Perfect. LGTM.

@go-jet go-jet merged commit b80fb5d into go-jet:master Dec 17, 2022
@emarj emarj deleted the custom-types-indexes-pk branch January 17, 2023 18:52
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.

3 participants