Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,16 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
$isMaria = $this->connection->isMaria();
$version = $this->connection->getServerVersion();

if ($isMaria ||
(! $isMaria && version_compare($version, '8.0.13', '>='))) {
if ($column->useCurrent) {
$column->default(new Expression('(CURDATE())'));
}
}

return 'date';
}

Expand Down Expand Up @@ -1024,6 +1034,16 @@ protected function typeTimestampTz(Fluent $column)
*/
protected function typeYear(Fluent $column)
{
$isMaria = $this->connection->isMaria();
$version = $this->connection->getServerVersion();

if ($isMaria ||
(! $isMaria && version_compare($version, '8.0.13', '>='))) {
if ($column->useCurrent) {
$column->default(new Expression('(YEAR(CURDATE()))'));
}
}

return 'year';
}

Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression('CURRENT_DATE'));
}

return 'date';
}

Expand Down Expand Up @@ -1007,6 +1011,10 @@ protected function typeTimestampTz(Fluent $column)
*/
protected function typeYear(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression('EXTRACT(YEAR FROM CURRENT_DATE)'));
}

return $this->typeInteger($column);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,10 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression('CURRENT_DATE'));
}

return 'date';
}

Expand Down Expand Up @@ -992,6 +996,10 @@ protected function typeTimestampTz(Fluent $column)
*/
protected function typeYear(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression("(CAST(strftime('%Y', 'now') AS INTEGER))"));
}

return $this->typeInteger($column);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression('CAST(GETDATE() AS DATE)'));
}

return 'date';
}

Expand Down Expand Up @@ -856,6 +860,10 @@ protected function typeTimestampTz(Fluent $column)
*/
protected function typeYear(Fluent $column)
{
if ($column->useCurrent) {
$column->default(new Expression('CAST(YEAR(GETDATE()) AS INTEGER)'));
}

return $this->typeInteger($column);
}

Expand Down
40 changes: 38 additions & 2 deletions tests/Database/DatabaseMariaDbSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,23 +873,59 @@ public function testAddingJsonb()

public function testAddingDate()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(true);
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');

$blueprint = new Blueprint($conn, 'users');
$blueprint->date('foo');
$statements = $blueprint->toSql();

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

public function testAddingDateWithDefaultCurrent()
{
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(true);
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');

$blueprint = new Blueprint($conn, 'users');
$blueprint->date('foo')->useCurrent();
$statements = $blueprint->toSql();

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

public function testAddingYear()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(true);
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');

$blueprint = new Blueprint($conn, 'users');
$blueprint->year('birth_year');
$statements = $blueprint->toSql();
$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
}

public function testAddingYearWithDefaultCurrent()
{
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(true);
$conn->shouldReceive('getServerVersion')->andReturn('10.3.0');

$blueprint = new Blueprint($conn, 'users');
$blueprint->year('birth_year')->useCurrent();
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))', $statements[0]);
}

public function testAddingDateTime()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand Down
68 changes: 66 additions & 2 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,23 +872,87 @@ public function testAddingJsonb()

public function testAddingDate()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(false);
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');

$blueprint = new Blueprint($conn, 'users');
$blueprint->date('foo');
$statements = $blueprint->toSql();

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

public function testAddingDateWithDefaultCurrent()
{
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(false);
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');

$blueprint = new Blueprint($conn, 'users');
$blueprint->date('foo')->useCurrent();
$statements = $blueprint->toSql();

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

public function testAddingDateWithDefaultCurrentOn57()
{
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(false);
$conn->shouldReceive('getServerVersion')->andReturn('5.7');

$blueprint = new Blueprint($conn, 'users');
$blueprint->date('foo')->useCurrent();
$statements = $blueprint->toSql();

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

public function testAddingYear()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(false);
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');

$blueprint = new Blueprint($conn, 'users');
$blueprint->year('birth_year');
$statements = $blueprint->toSql();
$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
}

public function testAddingYearWithDefaultCurrent()
{
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(false);
$conn->shouldReceive('getServerVersion')->andReturn('8.0.13');

$blueprint = new Blueprint($conn, 'users');
$blueprint->year('birth_year')->useCurrent();
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))', $statements[0]);
}

public function testAddingYearWithDefaultCurrentOn57()
{
$conn = $this->getConnection();
$conn->shouldReceive('isMaria')->andReturn(false);
$conn->shouldReceive('getServerVersion')->andReturn('5.7');

$blueprint = new Blueprint($conn, 'users');
$blueprint->year('birth_year')->useCurrent();
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `birth_year` year not null', $statements[0]);
}

public function testAddingDateTime()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ public function testAddingDate()
$this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
}

public function testAddingDateWithDefaultCurrent()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$blueprint->date('foo')->useCurrent();
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" date not null default CURRENT_DATE', $statements[0]);
}

public function testAddingYear()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand All @@ -723,6 +733,15 @@ public function testAddingYear()
$this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
}

public function testAddingYearWithDefaultCurrent()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$blueprint->year('birth_year')->useCurrent();
$statements = $blueprint->toSql();
$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "birth_year" integer not null default EXTRACT(YEAR FROM CURRENT_DATE)', $statements[0]);
}

public function testAddingJson()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,16 @@ public function testAddingDate()
$this->assertSame('alter table "users" add column "foo" date not null', $statements[0]);
}

public function testAddingDateWithDefaultCurrent()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$blueprint->date('foo')->useCurrent();
$statements = $blueprint->toSql();

$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" date not null default CURRENT_DATE', $statements[0]);
}

public function testAddingYear()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand All @@ -649,6 +659,15 @@ public function testAddingYear()
$this->assertSame('alter table "users" add column "birth_year" integer not null', $statements[0]);
}

public function testAddingYearWithDefaultCurrent()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
$blueprint->year('birth_year')->useCurrent();
$statements = $blueprint->toSql();
$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "birth_year" integer not null default (CAST(strftime(\'%Y\', \'now\') AS INTEGER))', $statements[0]);
}

public function testAddingDateTime()
{
$blueprint = new Blueprint($this->getConnection(), 'users');
Expand Down
50 changes: 50 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,31 @@ public function testDropIndexDefaultNamesWhenPrefixSupplied()
$this->assertSame('prefix_geo_coordinates_spatialindex', $commands[0]->index);
}

public function testDefaultCurrentDate()
{
$getSql = function ($grammar, $mysql57 = false) {
if ($grammar == 'MySql') {
$connection = $this->getConnection($grammar);
$mysql57 ? $connection->shouldReceive('getServerVersion')->andReturn('5.7') : $connection->shouldReceive('getServerVersion')->andReturn('8.0.13');
$connection->shouldReceive('isMaria')->andReturn(false);

return (new Blueprint($connection, 'users', function ($table) {
$table->date('created')->useCurrent();
}))->toSql();
} else {
return $this->getBlueprint($grammar, 'users', function ($table) {
$table->date('created')->useCurrent();
})->toSql();
}
};

$this->assertEquals(['alter table `users` add `created` date not null default (CURDATE())'], $getSql('MySql'));
$this->assertEquals(['alter table `users` add `created` date not null'], $getSql('MySql', mysql57: true));
$this->assertEquals(['alter table "users" add column "created" date not null default CURRENT_DATE'], $getSql('Postgres'));
$this->assertEquals(['alter table "users" add column "created" date not null default CURRENT_DATE'], $getSql('SQLite'));
$this->assertEquals(['alter table "users" add "created" date not null default CAST(GETDATE() AS DATE)'], $getSql('SqlServer'));
}

public function testDefaultCurrentDateTime()
{
$getSql = function ($grammar) {
Expand Down Expand Up @@ -130,6 +155,31 @@ public function testDefaultCurrentTimestamp()
$this->assertEquals(['alter table "users" add "created" datetime not null default CURRENT_TIMESTAMP'], $getSql('SqlServer'));
}

public function testDefaultCurrentYear()
{
$getSql = function ($grammar, $mysql57 = false) {
if ($grammar == 'MySql') {
$connection = $this->getConnection($grammar);
$mysql57 ? $connection->shouldReceive('getServerVersion')->andReturn('5.7') : $connection->shouldReceive('getServerVersion')->andReturn('8.0.13');
$connection->shouldReceive('isMaria')->andReturn(false);

return (new Blueprint($connection, 'users', function ($table) {
$table->year('birth_year')->useCurrent();
}))->toSql();
} else {
return $this->getBlueprint($grammar, 'users', function ($table) {
$table->year('birth_year')->useCurrent();
})->toSql();
}
};

$this->assertEquals(['alter table `users` add `birth_year` year not null default (YEAR(CURDATE()))'], $getSql('MySql'));
$this->assertEquals(['alter table `users` add `birth_year` year not null'], $getSql('MySql', mysql57: true));
$this->assertEquals(['alter table "users" add column "birth_year" integer not null default EXTRACT(YEAR FROM CURRENT_DATE)'], $getSql('Postgres'));
$this->assertEquals(['alter table "users" add column "birth_year" integer not null default (CAST(strftime(\'%Y\', \'now\') AS INTEGER))'], $getSql('SQLite'));
$this->assertEquals(['alter table "users" add "birth_year" int not null default CAST(YEAR(GETDATE()) AS INTEGER)'], $getSql('SqlServer'));
}

public function testRemoveColumn()
{
$getSql = function ($grammar) {
Expand Down
Loading