8000 [12.x] Support `useCurrent` on date and year column types (#55619) · laravel/framework@d100d1c · GitHub
[go: up one dir, main page]

Skip to content

Commit d100d1c

Browse files
[12.x] Support useCurrent on date and year column types (#55619)
* Support useCurrent on date and year column types Support useCurrent on date and year column types * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 9dae554 commit d100d1c

10 files changed

+255
-4
lines changed

src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

+20
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,16 @@ protected function typeJsonb(Fluent $column)
927927
*/
928928
protected function typeDate(Fluent $column)
929929
{
930+
$isMaria = $this->connection->isMaria();
931+
$version = $this->connection->getServerVersion();
932+
933+
if ($isMaria ||
934+
(! $isMaria && version_compare($version, '8.0.13', '>='))) {
935+
if ($column->useCurrent) {
936+
$column->default(new Expression('(CURDATE())'));
937+
}
938+
}
939+
930940
return 'date';
931941
}
932942

@@ -1024,6 +1034,16 @@ protected function typeTimestampTz(Fluent $column)
10241034
*/
10251035
protected function typeYear(Fluent $column)
10261036
{
1037+
$isMaria = $this->connection->isMaria();
1038+
$version = $this->connection->getServerVersion();
1039+
1040+
if ($isMaria ||
1041+
(! $isMaria && version_compare($version, '8.0.13', '>='))) {
1042+
if ($column->useCurrent) {
1043+
$column->default(new Expression('(YEAR(CURDATE()))'));
1044+
}
1045+
}
1046+
10271047
return 'year';
10281048
}
10291049

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

+8
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,10 @@ protected function typeJsonb(Fluent $column)
922922
*/
923923
protected function typeDate(Fluent $column)
924924
{
925+
if ($column->useCurrent) {
926+
$column->default(new Expression('CURRENT_DATE'));
927+
}
928+
925929
return 'date';
926930
}
927931

@@ -1007,6 +1011,10 @@ protected function typeTimestampTz(Fluent $column)
10071011
*/
10081012
protected function typeYear(Fluent $column)
10091013
{
1014+
if ($column->useCurrent) {
1015+
$column->default(new Expression('EXTRACT(YEAR FROM CURRENT_DATE)'));
1016+
}
1017+
10101018
return $this->typeInteger($column);
10111019
}
10121020

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

+8
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@ protected function typeJsonb(Fluent $column)
907907
*/
908908
protected function typeDate(Fluent $column)
909909
{
910+
if ($column->useCurrent) {
911+
$column->default(new Expression('CURRENT_DATE'));
912+
}
913+
910914
return 'date';
911915
}
912916

@@ -992,6 +996,10 @@ protected function typeTimestampTz(Fluent $column)
992996
*/
993997
protected function typeYear(Fluent $column)
994998
{
999+
if ($column->useCurrent) {
1000+
$column->default(new Expression("(CAST(strftime('%Y', 'now') AS INTEGER))"));
1001+
}
1002+
9951003
return $this->typeInteger($column);
9961004
}
9971005

src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php

+8
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ protected function typeJsonb(Fluent $column)
769769
*/
770770
protected function typeDate(Fluent $column)
771771
{
772+
if ($column->useCurrent) {
773+
$column->default(new Expression('CAST(GETDATE() AS DATE)'));
774+
}
775+
772776
return 'date';
773777
}
774778

@@ -856,6 +860,10 @@ protected function typeTimestampTz(Fluent $column)
856860
*/
857861
protected function typeYear(Fluent $column)
858862
{
863+
if ($column->useCurrent) {
864+
$column->default(new Expression('CAST(YEAR(GETDATE()) AS INTEGER)'));
865+
}
866+
859867
return $this->typeInteger($column);
860868
}
861869

tests/Database/DatabaseMariaDbSchemaGrammarTest.php

+38-2
Original file line numberDiff line numberDiff line change
@@ -873,23 +873,59 @@ public function testAddingJsonb()
873873

874874
public function testAddingDate()
875875
{
876-
$blueprint = new Blueprint($this->getConnection(), 'users');
876+
$conn = $this->getConnection();
877+
$conn->shouldReceive('isMaria')->andReturn(true);
878+
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');
879+
880+
$blueprint = new Blueprint($conn, 'users');
877881
$blueprint->date('foo');
878882
$statements = $blueprint->toSql();
879883

880884
$this->assertCount(1, $statements);
881885
$this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
882886
}
883887

888+
public function testAddingDateWithDefaultCurrent()
889+
{
890+
$conn = $this->getConnection();
891+
$conn->shouldReceive('isMaria')->andReturn(true);
892+
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');
893+
894+
$blueprint = new Blueprint($conn, 'users');
895+
$blueprint->date('foo')->useCurrent();
896+
$statements = $blueprint->toSql();
897+
898+
$this->assertCount(1, $statements);
899+
$this->assertSame('alter table `users` add `foo` date not null default (CURDATE())', $statements[0]);
900+
}
901+
884902
public function testAddingYear()
885903
{
886-
$blueprint = new Blueprint($this->getConnection(), 'users');
904+
$conn = $this->getConnection();
905+
$conn->shouldReceive('isMaria')->andReturn(true);
906+
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');
907+
908+
$blueprint = new Blueprint($conn, 'users');
887909
$blueprint->year('birth_year');
888910
$statements = $blueprint->toSql();
889911
$this->assertCount(1, $statements);
890912
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
891913
}
892914

915+
public function testAddingYearWithDefaultCurrent()
916+
{
917+
$conn = $this->getConnection();
918+
$conn->shouldReceive('isMaria')->andReturn(true);
919+
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');
920+
921+
$blueprint = new Blueprint($conn, 'users');
922+
$blueprint->year('birth_year')->useCurrent();
923+
$statements = $blueprint->toSql();
924+
925+
$this->assertCount(1, $statements);
926+
$this->assertSame('alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))', $statements[0]);
927+
}
928+
893929
public function testAddingDateTime()
894930
{
895931
$blueprint = new Blueprint($this->getConnection(), 'users');

tests/Database/DatabaseMySqlSchemaGrammarTest.php

+66-2
Original file line numberDiff line numberDiff line change
@@ -872,23 +872,87 @@ public function testAddingJsonb()
872872

873873
public function testAddingDate()
874874
{
875-
$blueprint = new Blueprint($this->getConnection(), 'users');
875+
$conn = $this->getConnection();
876+
$conn->shouldReceive('isMaria')->andReturn(false);
877+
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');
878+
879+
$blueprint = new Blueprint($conn, 'users');
876880
$blueprint->date('foo');
877881
$statements = $blueprint->toSql();
878882

879883
$this->assertCount(1, $statements);
880884
$this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
881885
}
882886

887+
public function testAddingDateWithDefaultCurrent()
888+
{
889+
$conn = $this->getConnection();
890+
$conn->shouldReceive('isMaria')->andReturn(false);
891+
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');
892+
893+
$blueprint = new Blueprint($conn, 'users');
894+
$blueprint->date('foo')->useCurrent();
895+
$statements = $blueprint->toSql();
896+
897+
$this->assertCount(1, $statements);
898+
$this->assertSame('alter table `users` add `foo` date not null default (CURDATE())', $statements[0]);
899+
}
900+
901+
public function testAddingDateWithDefaultCurrentOn57()
902+
{
903+
$conn = $this->getConnection();
904+
$conn->shouldReceive('isMaria')->andReturn(false);
905+
$conn->shouldReceive('getServerVersion')->andReturn('5.7');
906+
907+
$blueprint = new Blueprint($conn, 'users');
908+
$blueprint->date('foo')->useCurrent();
909+
$statements = $blueprint->toSql();
910+
911+
$this->assertCount(1, $statements);
912+
$this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
913+
}
914+
883915
public function testAddingYear()
884916
{
885-
$blueprint = new Blueprint($this->getConnection(), 'users');
917+
$conn = $this->getConnection();
918+
$conn->shouldReceive('isMaria')->andReturn(false);
919+
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');
920+
921+
$blueprint = new Blueprint($conn, 'users');
886922
$blueprint->year('birth_year');
887923
$statements = $blueprint->toSql();
888924
$this->assertCount(1, $statements);
889925
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
890926
}
891927

928+
public function testAddingYearWithDefaultCurrent()
929+
{
930+
$conn = $this->getConnection();
931+
$conn->shouldReceive('isMaria')->andReturn(false);
932+
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');
933+
934+
$blueprint = new Blueprint($conn, 'users');
935+
$blueprint->year('birth_year')->useCurrent();
936+
$statements = $blueprint->toSql();
937+
938+
$this->assertCount(1, $statements);
939+
$this->assertSame('alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))', $statements[0]);
940+
}
941+
942+
public function testAddingYearWithDefaultCurrentOn57()
943+
{
944+
$conn = $this->getConnection();
945+
$conn->shouldReceive('isMaria')->andReturn(false);
946+
$conn->shouldReceive('getServerVersion')->andReturn('5.7');
947+
948+
$blueprint = new Blueprint($conn, 'users');
949+
$blueprint->year('birth_year')->useCurrent();
950+
$statements = $blueprint->toSql();
951+
952+
$this->assertCount(1, $statements);
953+
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
954+
}
955+
892956
public function testAddingDateTime()
893957
{
894958
$blueprint = new Blueprint($this->getConnection(), 'users');

tests/Database/DatabasePostgresSchemaGrammarTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,16 @@ public function testAddingDate()
714714
$this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
715715
}
716716

717+
public function testAddingDateWithDefaultCurrent()
718+
{
719+
$blueprint = new Blueprint($this->getConnection(), 'users');
720+
$blueprint->date('foo')->useCurrent();
721+
$statements = $blueprint->toSql();
722+
723+
$this->assertCount(1, $statements);
724+
$this->assertSame('alter table "users" add column "foo" date not null default CURRENT_DATE', $statements[0]);
725+
}
726+
717727
public function testAddingYear()
718728
{
719729
$blueprint = new Blueprint($this->getConnection(), 'users');
@@ -723,6 +733,15 @@ public function testAddingYear()
723733
$this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
724734
}
725735

736+
public function testAddingYearWithDefaultCurrent()
737+
{
738+
$blueprint = new Blueprint($this->getConnection(), 'users');
739+
$blueprint->year('birth_year')->useCurrent();
740+
$statements = $blueprint->toSql();
741+
$this->assertCount(1, $statements);
742+
$this->assertSame('alter table "users" add column "birth_year" integer not null default EXTRACT(YEAR FROM CURRENT_DATE)', $statements[0]);
743+
}
744+
726745
public function testAddingJson()
727746
{
728747
$blueprint = new Blueprint($this->getConnection(), 'users');

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,16 @@ public function testAddingDate()
640640
$this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
641641
}
642642

643+
public function testAddingDateWithDefaultCurrent()
644+
{
645+
$blueprint = new Blueprint($this->getConnection(), 'users');
646+
$blueprint->date('foo')->useCurrent();
647+
$statements = $blueprint->toSql();
648+
649+
$this->assertCount(1, $statements);
650+
$this->assertSame('alter table "users" add column "foo" date not null default CURRENT_DATE', $statements[0]);
651+
}
652+
643653
public function testAddingYear()
644654
{
645655
$blueprint = new Blueprint($this->getConnection(), 'users');
@@ -649,6 +659,15 @@ public function testAddingYear()
649659
$this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
650660
}
651661

662+
public function testAddingYearWithDefaultCurrent()
663+
{
664+
$blueprint = new Blueprint($this->getConnection(), 'users');
665+
$blueprint->year('birth_year')->useCurrent();
666+
$statements = $blueprint->toSql();
667+
$this->assertCount(1, $statements);
668+
$this->assertSame('alter table "users" add column "birth_year" integer not null default (CAST(strftime(\'%Y\', \'now\') AS INTEGER))', $statements[0]);
669+
}
670+
652671
public function testAddingDateTime()
653672
{
654673
$blueprint = new Blueprint($this->getConnection(), 'users');

tests/Database/DatabaseSchemaBlueprintTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ public function testDropIndexDefaultNamesWhenPrefixSupplied()
102102
$this->assertSame('prefix_geo_coordinates_spatialindex', $commands[0]->index);
103103
}
104104

105+
public function testDefaultCurrentDate()
106+
{
107+
$getSql = function ($grammar, $mysql57 = false) {
108+
if ($grammar == 'MySql') {
109+
$connection = $this->getConnection($grammar);
110+
$mysql57 ? $connection->shouldReceive('getServerVersion')->andReturn('5.7') : $connection->shouldReceive('getServerVersion')->andReturn('8.0.13');
111+
$connection->shouldReceive('isMaria')->andReturn(false);
112+
113+
return (new Blueprint($connection, 'users', function ($table) {
114+
$table->date('created')->useCurrent();
115+
}))->toSql();
116+
} else {
117+
return $this->getBlueprint($grammar, 'users', function ($table) {
118+
$table->date('created')->useCurrent();
119+
})->toSql();
120+
}
121+
};
122+
123+
$this->assertEquals(['alter table `users` add `created` date not null default (CURDATE())'], $getSql('MySql'));
124+
$this->assertEquals(['alter table `users` add `created` date not null'], $getSql('MySql', mysql57: true));
125+
$this->assertEquals(['alter table "users" add column "created" date not null default CURRENT_DATE'], $getSql('Postgres'));
126+
$this->assertEquals(['alter table "users" add column "created" date not null default CURRENT_DATE'], $getSql('SQLite'));
127+
$this->assertEquals(['alter table "users" add "created" date not null default CAST(GETDATE() AS DATE)'], $getSql('SqlServer'));
128+
}
129+
105130
public function testDefaultCurrentDateTime()
106131
{
107132
$getSql = function ($grammar) {
@@ -130,6 +155,31 @@ public function testDefaultCurrentTimestamp()
130155
$this->assertEquals(['alter table "users" add "created" datetime not null default CURRENT_TIMESTAMP'], $getSql('SqlServer'));
131156
}
132157

158+
public function testDefaultCurrentYear()
159+
{
160+
$getSql = function ($grammar, $mysql57 = false) {
161+
if ($grammar == 'MySql') {
162+
$connection = $this->getConnection($grammar);
163+
$mysql57 ? $connection->shouldReceive('getServerVersion')->andReturn('5.7') : $connection->shouldReceive('getServerVersion')->andReturn('8.0.13');
164+
$connection->shouldReceive('isMaria')->andReturn(false);
165+
166+
return (new Blueprint($connection, 'users', function ($table) {
167+
$table->year('birth_year')->useCurrent();
168+
}))->toSql();
169+
} else {
170+
return $this->getBlueprint($grammar, 'users', function ($table) {
171+
$table->year('birth_year')->useCurrent();
172+
})->toSql();
173+
}
174+
};
175+
176+
$this->assertEquals(['alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))'], $getSql('MySql'));
177+
$this->assertEquals(['alter table `users` add `birth_year` year not null'], $getSql('MySql', mysql57: true));
178+
$this->assertEquals(['alter table "users" add column "birth_year" integer not null default EXTRACT(YEAR FROM CURRENT_DATE)'], $getSql('Postgres'));
179+
$this->assertEquals(['alter table "users" add column "birth_year" integer not null default (CAST(strftime(\'%Y\', \'now\') AS INTEGER))'], $getSql('SQLite'));
180+
$this->assertEquals(['alter table "users" add "birth_year" int not null default CAST(YEAR(GETDATE()) AS INTEGER)'], $getSql('SqlServer'));
181+
}
182+
133183
public function testRemoveColumn()
134184
{
135185
$getSql = function ($grammar) {

0 commit comments

Comments
 (0)
0