Skip to content

[12.x] Support useCurrent on date and year column types #55619

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

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from

Conversation

nicholasbrantley
Copy link

@nicholasbrantley nicholasbrantley commented May 1, 2025

This PR adds support for the useCurrent column modifier on DATE and YEAR column types. This allows these columns to default to the current date/year, like the TIMESTAMP column type.

Usage

$table->date('foo')->useCurrent();

$table->year('birth_year')->useCurrent();

This works across all supported versions of MariaDB, PostgreSQL, SQLite, and SQL Server, as well as MySQL 8.0.13+.

For MySQL, it checks against the server version and is supported on 8.0.13 and later. This is due to older versions not supporting default expressions.

Generated queries

-- MariaDB

# date
alter table `users` add `foo` date not null default (CURDATE());
# year
alter table `users` add `birth_year` year not null default (YEAR(CURDATE()));
-- PostgreSQL

# date
alter table "users" add column "foo" date not null default CURRENT_DATE;
# year
alter table "users" add column "birth_year" integer not null default EXTRACT(YEAR FROM CURRENT_DATE);
-- SQLite

# date
alter table "users" add column "foo" date not null default CURRENT_DATE;
# year
alter table "users" add column "birth_year" integer not null default (CAST(strftime('%Y', 'now') AS INTEGER));
-- SQL Server

# date
alter table "users" add "foo" date not null default CAST(GETDATE() AS DATE);
# year
alter table "users" add "birth_year" int not null default CAST(YEAR(GETDATE()) AS INTEGER);
-- MySQL (8.0.13+)

# date
alter table `users` add `foo` date not null default (CURDATE());
# year 
alter table `users` add `birth_year` year not null default (YEAR(CURDATE()));
-- MySQL (5.7 - 8.0.12)
# (the useCurrent modifier is ignored)

# date
alter table `users` add `foo` date not null;
# year 
alter table `users` add `birth_year` year not null;

Copy link

github-actions bot commented May 1, 2025

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

Support useCurrent on date and year column types
@nicholasbrantley nicholasbrantley marked this pull request as ready for review May 1, 2025 02:22
@nicholasbrantley nicholasbrantley changed the title [12.x] Support useCurrent on date and year column types [12.x] Support useCurrent on date and year column types May 1, 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.

1 participant