Skip to content

Fix the query that is generated to purge the old installed extras #16717

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

Merged
merged 4 commits into from
Mar 20, 2025

Conversation

Jako
Copy link
Collaborator

@Jako Jako commented Mar 3, 2025

What does it do?

Fix incorrect DATETIME value: '0000-00-00 00:00:00' error in the query

Why is it needed?

The generated query is wrong for MySQL installations that have NO_ZERO_DATE/NO_ZERO_IN_DATE activated.

Related issue(s)/PR(s)

MODX 3.x port of #16716

@Jako Jako requested review from opengeek and Mark-H as code owners March 3, 2025 10:34
@smg6511
Copy link
Collaborator

smg6511 commented Mar 14, 2025

The question here is: Is it safe to assume that the saved installed value would never be 0000-00-00 00:00:00 (indicating package's status is not installed)? If so, this change is good to go with a few code quality fix ups. If not, the null check should be added as an additional where criteria instead of replacing the original check, right?

@opengeek
Copy link
Member

The question here is: Is it safe to assume that the saved installed value would never be 0000-00-00 00:00:00 (indicating package's status is not installed)? If so, this change is good to go with a few code quality fix ups. If not, the null check should be added as an additional where criteria instead of replacing the original check, right?

I believe you are right—the null check needs to be added to the existing criteria rather than replacing it.

@Jako
Copy link
Collaborator Author

Jako commented Mar 14, 2025

I have checked that first before b9b533b but that did not work on at least two installations. Or did I make a mistake in my query?

@opengeek
Copy link
Member

I have checked that first before b9b533b but that did not work on at least two installations. Or did I make a mistake in my query?

I think the query was incorrect. The string '0000-00-00 00:00:00" is not the same as 0.

@Jako
Copy link
Collaborator Author

Jako commented Mar 14, 2025

The first check was 'installed:!=' => '0000-00-00 00:00:00' which gives an Incorrect DATETIME value: '0000-00-00 00:00:00' error when NO_ZERO_DATE/NO_ZERO_IN_DATE is activated.

@opengeek
Copy link
Member

The first check was 'installed:!=' => '0000-00-00 00:00:00' which gives an incorrect DATETIME value: '0000-00-00 00:00:00' error when NO_ZERO_DATE/NO_ZERO_IN_DATE is activated.

Ohhh… well, that is a huge problem then. I was afraid of that.

@smg6511
Copy link
Collaborator

smg6511 commented Mar 14, 2025

Hmmm, according to the mysql documentation:

For statements such as SELECT that do not change data, invalid values generate a warning in strict mode, not an error.

So, when you ran the query with both criteria, was it a warning or error you were seeing?

@Jako
Copy link
Collaborator Author

Jako commented Mar 14, 2025

The server has the following MySQL 8.0.22 sql_mode set:

STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

The same sql_mode don't show that issue in MySQL 5.7.44.

The errror message according to SequelPro is: Incorrect DATETIME value: '0000-00-00 00:00:00'

@smg6511
Copy link
Collaborator

smg6511 commented Mar 14, 2025

Yeah, I was just playing with this after changing the mode and got the error as well. Interesting that behavior is not consistent with what the docs say.

It seems to me that the bottom line here is that if someone has these strict date modes enabled in mysql 8+ and there are any zeroish values hanging around in the installed col, things are going to be a bit broken for those rows. It seems like the only way to fix those is manually (they can't be changed via an update query from 0000-00-00 00:00:00 to NULL).

I think we need to consider changing how dates are stored (to unix timestamp) when they can be initially empty or set back to empty (null), as dealing with this sql mode is a PITA.

@Jako
Copy link
Collaborator Author

Jako commented Mar 14, 2025

It is possible with an intermediate value (which normally does not occur in the table):

UPDATE `modx_transport_packages` SET `installed` = "1970-01-02 00:00:00" WHERE `installed` < "1970-03-01 00:00:00";
ALTER TABLE `modx_transport_packages` MODIFY `installed` DATETIME NULL;
UPDATE `modx_transport_packages` SET `installed` = NULL WHERE `installed` = '1970-01-02 00:00:00';

@smg6511
Copy link
Collaborator

smg6511 commented Mar 14, 2025

Ahh, nice! Just ran that (didn't need the ALTER line, because in 3.x that's already the default config for that column) on my dev install. Works.

Maybe this should make it into an upgrade script for the next release?

@opengeek
Copy link
Member

Maybe this should make it into an upgrade script for the next release?

Yes. We should review other tables that may have similar issues, as well.

@Jako
Copy link
Collaborator Author

Jako commented Mar 15, 2025

I have used a similar query for repairing the system/context/user settings in some old installations.

opengeek added a commit that referenced this pull request Mar 20, 2025
### What does it do?
Converts any invalid dates in the modTransportPackage.installed column
to NULL.

### Why is it needed?
Empty dates should have been saved as NULL in the column and their
presence can cause errors when NO_ZERO_DATE/NO_ZERO_IN_DATE sql_modes
are enabled in an environment.

### How to test
In an environment with NO_ZERO_DATE/NO_ZERO_DATE sql_modes enabled,
build the transport and run the upgrade process, confirming that the
process completes and that any invalid dates in the
modTransportPackage.installed column are now NULL. You will need to have
invalid dates in that column before enabling the strict sql_modes if you
want to verify that this works properly.

### Related issue(s)/PR(s)
Required for #16717 to be merged.
@opengeek opengeek merged commit 2533619 into modxcms:3.x Mar 20, 2025
7 checks passed
@Jako Jako deleted the patch-3 branch March 20, 2025 20:45
@opengeek opengeek added this to the v3.1.2 milestone Mar 20, 2025
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