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

Fix datetime field with empty default #166

Merged

Conversation

wojtekn
Copy link
Contributor

@wojtekn wojtekn commented Dec 11, 2024

Fixes #165

I propose to fix an issue where dumping the datetime field that has empty value results with producing incorrect MySQL.

@wojtekn wojtekn force-pushed the fix/datetime-field-with-empty-default branch from a18ec4e to 7835a5e Compare December 11, 2024 16:54
@wojtekn wojtekn marked this pull request as ready for review December 11, 2024 17:02
@katinthehatsite
Copy link

I initially had a question but I figured it so I removed it (in case you got a notification 😅 ). The fix looks good to me. I looked through The MySQL data types in the backups from Studio and Jetpack and the fix seems to cover the cases that we ran into. 👍

Out of curiosity, I see that the fix is covered with unit tests so there are no manual testing steps needed. If I were to test this manually with Studio, do you know what steps I would need to take? 🙇

@wojtekn
Copy link
Contributor Author

wojtekn commented Dec 12, 2024

Out of curiosity, I see that the fix is covered with unit tests so there are no manual testing steps needed. If I were to test this manually with Studio, do you know what steps I would need to take? 🙇

The easiest way would be:

  1. Create and start Studio site
  2. Copy the wp-includes/sqlite/class-wp-sqlite-translator.php file from this PR to wp-content/mu-plugins/sqlite-database-integration/wp-includes in your site
  3. Run Export or Push

@katinthehatsite
Copy link

Nice thanks!

private function column_has_default( $column, $mysql_type ) {
if ( null !== $column->dflt_value && '' !== $column->dflt_value && ! in_array( strtolower( $mysql_type ), array( 'datetime', 'date', 'time', 'timestamp', 'year' ), true ) ) {
return true;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

@wojtekn This seems to remove all defaults for date/time fields, including valid defaults, such as col YEAR DEFAULT '2024'. Could we add a test case with a valid default for each of these data types, and ensure that it is preserved?

My guess is that the problem could be that the original condition, despite looking fine ('' !== $column->dflt_value), might be missing the fact that the default can include a quoted string, so it should be rather something like '' !== trim( $column->dflt_value, "'" ).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thinking further, what's tricky, though, is that the '' value as a default is valid for string fields. So I guess we need to trim the quotes only for the case of non-string or date/time fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I untangled the conditions to handle all cases and make it more readable in 86b43fd

Copy link
Collaborator

@JanJakes JanJakes left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for the improvements!

@JanJakes JanJakes merged commit e1931d2 into WordPress:develop Dec 19, 2024
8 checks passed
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.

Create table with empty default timestamp is incorrect
4 participants