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

[8.x] Support useCurrentOnUpdate for MySQL datetime column types #36817

Conversation

timmartin19
Copy link
Contributor

@timmartin19 timmartin19 commented Mar 30, 2021

This commit introduces support for useCurrentOnUpdate for MySQL datetime
column types. MySQL has supported using ON UPDATE CURRENT_TIMESTAMP
for DATETIME columns since v5.6.5 (and Laravel supports MySQL 5.7+
from what I can tell). Laravel previously only supported useCurrentOnUpdate
for TIMESTAMP columns in MySQL.

This allows using DATETIME columns for metadata like created_at
and updated_at more effectively. Since TIMESTAMP columns in
MySQL suffer from the Year 2038 Problem,
using DATETIME columns is preferred in some applications.

We set the fractional seconds precision to be consistent
throughout the column initialization (for the column type, the default,
and the on update). MySQL requires that the precision be identical
throughout. Note that this implicitly fixed a (likely rare) bug when
users tried to run something like ->dateTime('foo', 2)->useCurrent().
Previously this would result in MySQL error code 1067 for invalid
default values. It will not set the default to have the same precision
as the column definition.

The actual code was borrowed heavily from typeTimeStamp. Since they
are distinct column types, it felt incorrect to DRY it up despite their
similarities.

Relevant MySQL documentation:
MySQL 5.6
MySQL 5.7
MySQL 8.0

This commit introduces support `useCurrentOnUpdate` for MySQL datetime
column types.  MySQL has supported using `ON UPDATE CURRENT_TIMESTAMP`
for `DATETIME` columns since `v5.6.5`.  Laravel currently previously
only supported `useCurrentOnUpdate` for `TIMESTAMP` columns in MySQL.

We set the fractional seconds precision to be consistent
throughout the column initialization (for the column type, the default,
and the on update).  MySQL requires that the precision be identical
throughout.  Note that this implicitly fixed a (likely rare) bug when
users tried to run something like `->dateTime('foo', 2)->useCurrent()`.
Previously this would result in MySQL error code 1067 for invalid
default values.  It will not set the default to have the same precision
as the column definition.

The actual code was borrowed heavily from `typeTimeStamp`.  Since they
are distinct column types, it felt incorrect to DRY it up despite their
similarities.

Relevant MySQL documentation:
[MySQL 5.6](https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html)
[MySQL 5.7](https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html)
[MySQL 8.0](https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html)
@timmartin19
Copy link
Contributor Author

Note that this is my first PR in any of the Laravel repos, so please let me know if there's anything I can improve on going forward!

@timmartin19 timmartin19 marked this pull request as ready for review March 30, 2021 16:55
@driesvints driesvints changed the title Support useCurrentOnUpdate for MySQL datetime column types [8.x] Support useCurrentOnUpdate for MySQL datetime column types Mar 30, 2021
@taylorotwell taylorotwell merged commit f09f067 into laravel:8.x Mar 31, 2021
@timmartin19 timmartin19 deleted the allow-on-update-clause-for-datetime-columns branch March 31, 2021 15:42
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.

2 participants