Skip to content

Properly process booleans in MySQL dialect to fix #607#608

Merged
sdepold merged 5 commits intosequelize:masterfrom
terraflubb:mysql_bools_save_as_tinyints
May 13, 2013
Merged

Properly process booleans in MySQL dialect to fix #607#608
sdepold merged 5 commits intosequelize:masterfrom
terraflubb:mysql_bools_save_as_tinyints

Conversation

@terraflubb
Copy link
Copy Markdown
Contributor

Fixes #607

The problem was that queries generated by with the MySQL dialect (where Sequelize.BOOLEAN is a TINYINT(1)) contained booleans in the form true or false instead of 1 or 0. This was causing all my queries involving adding or updating booleans to fail to the default. A whole lot of true values where I wasn't expecting them!

The solution was to make sure that when values were being passed into a query, we check if they're boolean, and if we do then turn them into integers.

When I was doing that, I noticed that there was duplicated logic for processing values (where it was checked if the value was a Date) all over the place, but not everywhere. In particular, the values used to build WHERE clauses.

So I factored all the logic for processing values (checking the type then escaping) into a function and called it from every where the logic was duplicated, and added it to the hashToWhereConditions too.

I included failing tests and I ran the entire test suite locally before committing.

Also to make sure I was doing it right, I ran JSHint like the README suggested. I saw a handful of minor lints I picked off. If this isn't cool, let me know and I'll revert that commit.

(I was also careful not to use any semicolons ;) )

In the MySQL dialect, values used to generate insert, bulk insert, and update queries are now checked for boolean-ness and will be turned into an int.

There was already duplicated logic applied to outgoing values to check if the value was a Date or not. I factored out value processing to a single function and added a check for typeof boolean.

The new function also escapes the return value since that was also being done everywhere.

Fixes #607
@durango
Copy link
Copy Markdown
Member

durango commented May 10, 2013

Looks good, you have my vote :+1 :D

@janmeier
Copy link
Copy Markdown
Member

Good catch, and the tests look very exhaustive. Good work!

I know that @sdepold is working on refactoring the query generators right now, but I think this should still be merged in - the tests at least are crucial, since there is obviously a part that is not covered right now,

@terraflubb
Copy link
Copy Markdown
Contributor Author

Great! Thanks. Does that mean we should stick this in as a bandaid in the meantime and it will be blown away when the @sdepold refactoring drops? Or would you prefer I revert my code changes and make this a "failing-test only" PR (hopefully not to be merged until the code is fixed elsewhere 😉).

@sdepold
Copy link
Copy Markdown
Member

sdepold commented May 13, 2013

coolio :)

sdepold added a commit that referenced this pull request May 13, 2013
Properly process booleans in MySQL dialect to fix #607
@sdepold sdepold merged commit d3669c8 into sequelize:master May 13, 2013
@sdepold
Copy link
Copy Markdown
Member

sdepold commented May 13, 2013

@terraflubb I merged your stuff into a special branch features/mariadb: https://github.com/sequelize/sequelize/tree/features/mariadb Tests are pretty broken. It would be cool if you could take a closer look at it.

@janmeier
Copy link
Copy Markdown
Member

@sdepold , I think that last comment was for #582 right?

@terraflubb
Copy link
Copy Markdown
Contributor Author

I'd love to! I'll install MariaDB tonight and see what I can do.

@terraflubb
Copy link
Copy Markdown
Contributor Author

@janmeier Hah, I think so. I think it was also directed at @reedog117 since I don't see any of my stuff in the mariadb branch. I thought he meant that my MySQL test fudged things up over there, since MariaDB is "drop in replacement" maybe the MySQL tests were being aped.

@sdepold
Copy link
Copy Markdown
Member

sdepold commented May 14, 2013

oh. got confused obsiously :D sorry

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.

Generated MySQL queries inserting BOOLEAN field values as booleans, not integers

4 participants