Skip to content

feat(postgres): add minifyAlias option in findOne, findAll, and findAndCountAll - #15394

Merged
ephys merged 27 commits into
sequelize:mainfrom
frostzt:feat/minify-alias-opt-for-findone-findall-findandcountall
Mar 3, 2023
Merged

ephys merged 27 commits into
sequelize:mainfrom
frostzt:feat/minify-alias-opt-for-findone-findall-findandcountall

Conversation

@frostzt

@frostzt frostzt commented Dec 5, 2022 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description Of Change

Closes #15361
Support minifyAlias option in static methods for findOne, findAll, and findAndCountAll.

@frostzt
frostzt marked this pull request as draft December 5, 2022 06:04
@ephys ephys changed the title feat: added minifyAlias option in findOne, findAll, and findAndCountAll feat: add minifyAlias option in findOne, findAll, and findAndCountAll Dec 8, 2022
@frostzt
frostzt marked this pull request as ready for review December 13, 2022 12:59
@frostzt

frostzt commented Dec 13, 2022

Copy link
Copy Markdown
Member Author

Hey @ephys I kinda need enlightenment here! Like I believe this should atleast push it in the d.ts to let the user know and since we are destructuring and passing it, it should work!

@frostzt
frostzt requested review from WikiRik and ephys December 13, 2022 13:01
@ephys

ephys commented Dec 16, 2022

Copy link
Copy Markdown
Member

That lets the user know the option is available but it's not actually doing anything because the code never checks if the option bag the method received has the option enabled. QueryGenerator only checks for the global version of the option right now

@frostzt

frostzt commented Dec 20, 2022 •

Copy link
Copy Markdown
Member Author

@ephys @WikiRik can you have a look at this?

@WikiRik
WikiRik marked this pull request as draft January 3, 2023 12:54
@WikiRik

WikiRik commented Jan 3, 2023

Copy link
Copy Markdown
Member

Can you add some tests on how it should behave?

@frostzt

frostzt commented Jan 7, 2023

Copy link
Copy Markdown
Member Author

Hey give me some time on this! Will start working on it asap!

@frostzt

frostzt commented Jan 18, 2023

Copy link
Copy Markdown
Member Author

@ephys @WikiRik I had a little question, I wanted to know how can I check the sql generated in one of the Model functions? Like for this I wanna test findOne since using instance it can be done however for this I wanna pass the option to a specific method itself?

@WikiRik

WikiRik commented Jan 19, 2023 •

Copy link
Copy Markdown
Member

@frostzt not 100% sure what you mean, but you should be able to enable logging to see the generated SQL queries, you can pass the option to e.g. queryInterface functions directly. You can also log/debug the return from queryGenerator.selectQuery. Does that help?

@frostzt

frostzt commented Jan 19, 2023

Copy link
Copy Markdown
Member Author

Hey @WikiRik actually yeah that logging is working as I expected and the results are also what I expected its just that I wanted to get the generated SQL which I can then compare to some expected SQL.

@WikiRik

WikiRik commented Jan 19, 2023

Copy link
Copy Markdown
Member

@frostzt in unit/integration tests you mean? Or just for you during development?

@frostzt

frostzt commented Jan 19, 2023

Copy link
Copy Markdown
Member Author

@WikiRik yeah mainly I want it through Unit Tests. The thing is I can do it via the selectQuery or wait did I just answer my own question! Oh wait I think I know how this can be done! Thanks a lot @WikiRik !

@frostzt
frostzt marked this pull request as ready for review January 21, 2023 09:44
@frostzt

frostzt commented Jan 21, 2023

Copy link
Copy Markdown
Member Author

I just realized that seems like that thing is not working with any other dialects except Postgres! And that's why the tests for alias minification are only for Postgres!

@WikiRik

WikiRik commented Jan 21, 2023

Copy link
Copy Markdown
Member

The only integration tests we currently have for minifyAliases are indeed postgres specific, but the single unit test we have does work on all dialects;

describe('minifyAliases', () => {
const minifyAliasesSequelize = createSequelizeInstance({
minifyAliases: true,
});
const minifyQueryGenerator = minifyAliasesSequelize.queryInterface.queryGenerator;
it('minifies custom attributes', () => {
const sql = minifyQueryGenerator.selectQuery(User.tableName, {
model: User,
attributes: [
[literal('1'), 'customAttr'],
],
order: ['customAttr'],
group: ['customAttr'],
}, User);
expectsql(sql, {
default: `SELECT 1 AS [_0] FROM [Users] AS [User] GROUP BY [_0] ORDER BY [_0];`,
});
});
});

So maybe as a first step we need to implement the integration tests for the other dialects as well? Then we know it works globally for all dialects and then we can start looking into adding the option on specific queries

@frostzt

frostzt commented Jan 21, 2023

Copy link
Copy Markdown
Member Author

@WikiRik makes sense I'll actually start by writing for each dialect that I know about 😅

@frostzt

frostzt commented Jan 23, 2023 •

Copy link
Copy Markdown
Member Author

@WikiRik after debugging around a bit I found out few things. So there are few stuff that are actually not available in any dialect except Postgres, first only in Postgres we are mapping the join aliases!

https://github.com/sequelize/sequelize/blob/main/packages/core/src/dialects/postgres/query.js#L19

Another thing is; why the other dialects are throwing errors (failing the integration test) is that we are not remapping the columns that were aliased. For example, doing something as _0 returns _0 as columnName this is done in postgres https://github.com/sequelize/sequelize/blob/main/packages/core/src/dialects/postgres/query.js#L76. However other dialects don't have this and that's why the integration tests that I've written are throwing saying that Can't find field [teamAlias] because that alias is actually _3 I think we'll need to implement logic to remap these alises across all the dialects.

The change now should make that integration test pass for MySQL too.

@ephys what are your thoughts on this? Am I even right here?

@WikiRik

WikiRik commented Feb 3, 2023 •

Copy link
Copy Markdown
Member

The last failing test is a fun one. It doesn't fail on its own, but it does fail if you run the describe block it's in.

EDIT: The test name is a bit long so I ran the wrong test individually. It fails either way

@frostzt

frostzt commented Feb 3, 2023

Copy link
Copy Markdown
Member Author

The last failing test is a fun one. It doesn't fail on its own, but it does fail if you run the describe block it's in.

😅 yeah I think its a little tricky but when I put the logs I saw something have a look!

If you generate a query in the main branch without any of my changes I get this

SELECT
	"Task".*,
	"category"."id" AS "_1",
	"category"."createdAt" AS "_2",
	"category"."updatedAt" AS "_3"
FROM (
	SELECT
		*
	FROM (
		SELECT
			"id",
			"title",
			"category_id" AS "_0",
			"createdAt",
			"updatedAt",
			"UserId"
		FROM
			"Tasks" AS "Task"
		WHERE
			"Task"."UserId" = 1
		ORDER BY
			"Task"."title" ASC
		LIMIT 2) AS sub
UNION ALL
SELECT
	*
FROM (
	SELECT
		"id",
		"title",
		"category_id" AS "_0",
		"createdAt",
		"updatedAt",
		"UserId"
	FROM
		"Tasks" AS "Task"
	WHERE
		"Task"."UserId" = 2
	ORDER BY
		"Task"."title" ASC
	LIMIT 2) AS sub) AS "Task"
	LEFT OUTER JOIN "Categories" AS "category" ON _0 = "category"."id";

And here is what happens when you switch to my code:

SELECT
	"Task".*,
	"category"."id" AS "_1",
	"category"."createdAt" AS "_2",
	"category"."updatedAt" AS "_3"
FROM (
	SELECT
		*
	FROM (
		SELECT
			"id",
			"title",
			"category_id" AS "categoryId",
			"createdAt",
			"updatedAt",
			"UserId"
		FROM
			"Tasks" AS "Task"
		WHERE
			"Task"."UserId" = 1
		ORDER BY
			"Task"."title" ASC
		LIMIT 2) AS sub
UNION ALL
SELECT
	*
FROM (
	SELECT
		"id",
		"title",
		"category_id" AS "categoryId",
		"createdAt",
		"updatedAt",
		"UserId"
	FROM
		"Tasks" AS "Task"
	WHERE
		"Task"."UserId" = 2
	ORDER BY
		"Task"."title" ASC
	LIMIT 2) AS sub) AS "Task"
	LEFT OUTER JOIN "Categories" AS "category" ON _0 = "category"."id";

Do you see that category_id got an alias of categoryId which is a little weird because the lower condition tries to join on _0 which in the above query is actually category_id which is converted into _0!

@WikiRik

WikiRik commented Feb 3, 2023

Copy link
Copy Markdown
Member

https://github.com/sequelize/sequelize/pull/15394/files#diff-0977da4c1f5c2f8febfb929e94ef7580b53b102cfd070dff51db9582455b4490R1342-R1358

We're not passing the option to the subquery selectQuery, so it's just a question of adding minifyAliases: options.minifyAliases, in that options object and we're good to go it seems

@frostzt

frostzt commented Feb 3, 2023 •

Copy link
Copy Markdown
Member Author

Thanks a lot @WikiRik 😅 gotta read the code more I never knew that thing existed there! There are few integration tests that fail though they are something with pooling!

@frostzt
frostzt requested a review from WikiRik February 3, 2023 14:57
@WikiRik

WikiRik commented Feb 3, 2023

Copy link
Copy Markdown
Member

@frostzt I think you just got unlucky with failing tests. I also didn't know that it existed there, but I was logging the options passed to selectQuery and found that this one didn't pass minifyAliases. So in the end it was an easy fix, but you never know

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Three small comments, but I think what's I'm still missing here is integration tests for on a local level instead of the global level. So if you add those I think we should be good to merge

@frostzt
frostzt requested a review from WikiRik February 7, 2023 04:54
@frostzt

frostzt commented Feb 18, 2023

Copy link
Copy Markdown
Member Author

@ephys I think this is ready to go!

@frostzt

frostzt commented Feb 26, 2023

Copy link
Copy Markdown
Member Author

@ephys bump just in case!

@ephys ephys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks really good! My last two comments are super minor
Thank you so much for working on this

Comment thread packages/core/src/model.d.ts Resolved
@WikiRik

WikiRik commented Mar 2, 2023

Copy link
Copy Markdown
Member

I'm still missing integration tests for setting the option locally but we can add those in a future PR as well

@ephys

ephys commented Mar 2, 2023

Copy link
Copy Markdown
Member

No you're right, this PR should add at least one integration test in integration/query-interface/select

@frostzt
frostzt requested a review from ephys March 3, 2023 17:47
@WikiRik

WikiRik commented Mar 3, 2023

Copy link
Copy Markdown
Member

Thanks for working on this! I think we're good to go

@frostzt

frostzt commented Mar 3, 2023 •

Copy link
Copy Markdown
Member Author

@ephys so should I start working on the other dialects as well? There is this too #15646, and as far as #15647 goes I'll start by refactoring first in a different PR!

@ephys

ephys commented Mar 3, 2023 •

Copy link
Copy Markdown
Member

Fully up to you :)
If you want to implement the feature in other models, I recommend fixing the vulnerability first, so you don't have to fix it in each dialect

@ephys
ephys merged commit 9e3ac46 into sequelize:main Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Support minifyAliases option in findAll/findOne/findAndCountAll

3 participants