8000 Support useCurrent on date and year column types · laravel/framework@98acefb · GitHub
[go: up one dir, main page]

Skip to content

Commit 98acefb

Browse files
Support useCurrent on date and year column types
Support useCurrent on date and year column types
1 parent bcc9220 commit 98acefb

10 files changed

+227
-0
lines changed

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

+24
Original file line numberDiff line numberDiff line change
@@ -922,11 +922,23 @@ protected function typeJsonb(Fluent $column)
922922
/**
923923
* Create the column definition for a date type.
924924
*
925+
* Note: "useCurrent requires MariaDB or MySQL 8.0.13+."
926+
*
925927
* @param \Illuminate\Support\Fluent $column
926928
* @return string
927929
*/
928930
protected function typeDate(Fluent $column)
929931
{
932+
$isMaria = $this->connection->isMaria();
933+
$version = $this->connection->getServerVersion();
934+
935+
if ($isMaria ||
936+
(! $isMaria && version_compare($version, '8.0.13', '>='))) {
937+
if ($column->useCurrent) {
938+
$column->default(new Expression('(CURDATE())'));
939+
}
940+
}
941+
930942
return 'date';
931943
}
932944

@@ -1019,11 +1031,23 @@ protected function typeTimestampTz(Fluent $column)
10191031
/**
10201032
* Create the column definition for a year type.
10211033
*
1034+
* Note: "useCurrent requires MariaDB or MySQL 8.0.13+."
1035+
*
10221036
* @param \Illuminate\Support\Fluent $column
10231037
* @return string
10241038
*/
10251039
protected function typeYear(Fluent $column)
10261040
{
1041+
$isMaria = $this->connection->isMaria();
1042+
$version = $this->connection->getServerVersion();
1043+
1044+
if ($isMaria ||
1045+
(! $isMaria && version_compare($version, '8.0.13', '>='))) {
1046+
if ($column->useCurrent) {
1047+
$column->default(new Expression('(YEAR(CURDATE()))'));
1048+
}
1049+
}
1050+
10271051
return 'year';
10281052
}
10291053

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 < 10000 span class=pl-k>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

+20
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,16 @@ public function testAddingDate()
881881
$this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
882882
}
883883

884+
public function testAddingDateWithDefaultCurrent()
885+
{
886+
$blueprint = new Blueprint($this->getConnection(), 'users');
887+
$blueprint->date('foo')->useCurrent();
888+
$statements = $blueprint->toSql();
889+
890+
$this->assertCount(1, $statements);
891+
$this->assertSame('alter table `users` add `foo` date not null default (CURDATE())', $statements[0]);
892+
}
893+
884894
public function testAddingYear()
885895
{
886896
$blueprint = new Blueprint($this->getConnection(), 'users');
@@ -890,6 +900,16 @@ public function testAddingYear()
890900
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
891901
}
892902

903+
public function testAddingYearWithDefaultCurrent()
904+
{
905+
$blueprint = new Blueprint($this->getConnection(), 'users');
906+
$blueprint->year('birth_year')->useCurrent();
907+
$statements = $blueprint->toSql();
908+
909+
$this->assertCount(1, $statements);
910+
$this->assertSame('alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))', $statements[0]);
911+
}
912+
893913
public function testAddingDateTime()
894914
{
895915
$blueprint = new Blueprint($this->getConnection(), 'users');

tests/Database/DatabaseMySqlSchemaGrammarTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,32 @@ public function testAddingDate()
880880
$this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
881881
}
882882

883+
public function testAddingDateWithDefaultCurrent()
884+
{
885+
$conn = $this->getConnection();
886+
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');
887+
888+
$blueprint = new Blueprint($this->getConnection(), 'users');
889+
$blueprint->date('foo')->useCurrent();
890+
$statements = $blueprint->toSql();
891+
892+
$this->assertCount(1, $statements);
893+
$this->assertSame('alter table `users` add `foo` date not null default (CURDATE())', $statements[0]);
894+
}
895+
896+
public function testAddingDateWithDefaultCurrentOn57()
897+
{
898+
$conn = $this->getConnection();
899+
$conn->shouldReceive('getServerVersion')->andReturn('5.7');
900+
901+
$blueprint = new Blueprint($this->getConnection(), 'users');
902+
$blueprint->date('foo')->useCurrent();
903+
$statements = $blueprint->toSql();
904+
905+
$this->assertCount(1, $statements);
906+
$this->assertSame('alter table `users` add `foo` date not null', $statements[0]);
907+
}
908+
883909
public function testAddingYear()
884910
{
885911
$blueprint = new Blueprint($this->getConnection(), 'users');
@@ -889,6 +915,32 @@ public function testAddingYear()
889915
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
890916
}
891917

918+
public function testAddingYearWithDefaultCurrent()
919+
{
920+
$conn = $this->getConnection();
921+
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');
922+
923+
$blueprint = new Blueprint($this->getConnection(), 'users');
924+
$blueprint->year('birth_year')->useCurrent();
925+
$statements = $blueprint->toSql();
926+
927+
$this->assertCount(1, $statements);
928+
$this->assertSame('alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))', $statements[0]);
929+
}
930+
931+
public function testAddingYearWithDefaultCurrentOn57()
932+
{
933+
$conn = $this->getConnection();
934+
$conn->shouldReceive('getServerVersion')->andReturn('5.7');
935+
936+
$blueprint = new Blueprint($this->getConnection(), 'users');
937+
$blueprint->year('birth_year')->useCurrent();
938+
$statements = $blueprint->toSql();
939+
940+
$this->assertCount(1, $statements);
941+
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
942+
}
943+
892944
public function testAddingDateTime()
893945
{
894946
$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(& 741A #39;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) {

tests/Database/DatabaseSqlServerSchemaGrammarTest.php

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

617+
public function testAddingDateWithDefaultCurrent()
618+
{
619+
$blueprint = new Blueprint($this->getConnection(), 'users');
620+
$blueprint->date('foo')->useCurrent();
621+
$statements = $blueprint->toSql();
622+
623+
$this->assertCount(1, $statements);
624+
$this->assertSame('alter table "users" add "foo" date not null default CAST(GETDATE() AS DATE)', $statements[0]);
625+
}
626+
617627
public function testAddingYear()
618628
{
619629
$blueprint = new Blueprint($this->getConnection(), 'users');
@@ -623,6 +633,15 @@ public function testAddingYear()
623633
$this->assertSame('alter table "users" add "birth_year" int not null', $statements[0]);
624634
}
625635

636+
public function testAddingYearWithDefaultCurrent()
637+
{
638+
$blueprint = new Blueprint($this->getConnection(), 'users');
639+
$blueprint->year('birth_year')->useCurrent();
640+
$statements = $blueprint->toSql();
641+
$this->assertCount(1, $statements);
642+
$this->assertSame('alter table "users" add "birth_year" int not null default CAST(YEAR(GETDATE()) AS INTEGER)', $statements[0]);
643+
}
644+
626645
public function testAddingDateTime()
627646
{
628647
$blueprint = new Blueprint($this->getConnection(), 'users');

0 commit comments

Comments
 (0)
0