-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix default value for boolean type in SQLite getting columns schema #18973
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
Conversation
| author_id INT(11) NOT NULL, | ||
| unique_id UNSIGNED INTEGER NOT NULL, | ||
| published BOOLEAN DEFAULT 0, | ||
| reviewed BOOLEAN DEFAULT TRUE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this? Wouldnt it suffice to just map the other ones to numeric 0 as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe we just switch to using TRUE/FALSE then explicitly (in the future)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 I think that it is a more complete test case because \Cake\Database\Schema\SqliteSchemaDialect::columnSql():
- passing
'default' => trueproducesBOOLEAN DEFAULT TRUE - passing
'default' => falseproducesBOOLEAN DEFAULT FALSE - passing
'default' => 1producesBOOLEAN DEFAULT 1 - passing
'default' => 0producesBOOLEAN DEFAULT 0
so you can deal with both cases.
Anyway if you think it is unnecessary I can remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the other code changes above I think it should for now always produce only 0 and 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, SQLite doesn't have a dedicated boolean type
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
SQLite recognizes the keywords "TRUE" and "FALSE", as of version 3.23.0 (2018-04-02) but those keywords are really just alternative spellings for the integer literals 1 and 0 respectively.
https://sqlite.org/datatype3.html
Our other drivers use 0/1 in their schema reflection boolean logic, and it would be good to be consistent here.
|
I guess the one fail is probably an invalid test (data): Since it is "not empty" I wonder if this should map to |
| * @return string|int|bool|null | ||
| */ | ||
| protected function _defaultValue(string|int|null $default): string|int|null | ||
| protected function _defaultValue(string|int|null $default, ?string $type = null): string|int|bool|null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about as bugfix for 5.x:
- You dont change the signature and map it to 0/1
Then in 5.next:
- We can adjust the internal protected method to always return true/false for booleans from now on.
This is a bit more BC safe for people and consistent in the output then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to leave the second parameter ?string $type = null and change the return value for type boolean always to int?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minimal changeset that makes the code work, yes, for a bugfix release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to worry too much about BC here. I doubt that there are userland extensions to the sqlite schema dialect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think it would be cleaner if we had one single way of interpretation.
Our other drivers use 0/1 in their schema reflection boolean logic, and it would be good to be consistent here.
So its settled then.
Co-authored-by: Mark Scherer <[email protected]>
Uhm... I'm not sure how to handle this situation. But what is the action to take in this scenario? SQLite allows to set any default i.e. Any suggestion? |
What does it convert it to? 0 or 1? whatever the real result, thats your answer IMO. |
- Fixed failing tests - Aligned on int cast for boolean column default values. - Made validation stricter by not handling arbitrary string-integers
|
I pushed up a commit with my suggestions and fixed failing tests. |
Co-authored-by: ADmad <[email protected]>
This PR fixes an issue with default value of boolean type columns for SQLite database.
Building a table with
TableSchemafor example:will produce
Once the table was created reading the schema we obtain an inconsistency columns for default value.
output
The default is a string
'TRUE'but I expect a booleantrueinstead.The same happens defining default as INT, for example
0. The results in this case will be'default' => '0'.Side note
To change code just in one place I modified the
protected function _defaultValuesignature. I think that it shouldn't have serious impact but let me know in case.