Skip to content

Commit

Permalink
Ensure all temporal columns handle useCurrent() as supported
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofreitas committed Nov 8, 2017
1 parent 0255644 commit 5cbe957
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 53 deletions.
31 changes: 29 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;

class MySqlGrammar extends Grammar
Expand Down Expand Up @@ -582,6 +583,8 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
$this->parseTemporalType($column);

return 'date';
}

Expand All @@ -593,6 +596,8 @@ protected function typeDate(Fluent $column)
*/
protected function typeDateTime(Fluent $column)
{
$this->parseTemporalType($column);

return $column->precision ? "datetime($column->precision)" : 'datetime';
}

Expand All @@ -615,6 +620,8 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
$this->parseTemporalType($column);

return $column->precision ? "time($column->precision)" : 'time';
}

Expand All @@ -637,9 +644,9 @@ protected function typeTimeTz(Fluent $column)
*/
protected function typeTimestamp(Fluent $column)
{
$type = $column->precision ? "timestamp($column->precision)" : 'timestamp';
$this->parseTemporalType($column);

return $column->useCurrent ? "$type default CURRENT_TIMESTAMP" : $type;
return $column->precision ? "timestamp($column->precision)" : 'timestamp';
}

/**
Expand Down Expand Up @@ -785,6 +792,26 @@ public function typeMultiPolygon(Fluent $column)
return 'multipolygon';
}

/**
* Parse the default value for a temporal column type.
*
* @param \Illuminate\Support\Fluent $column
* @return void
*/
protected function parseTemporalType(Fluent &$column)
{
if ($column->useCurrent) {
switch ($column->type) {
case 'dateTime':
case 'dateTimeTz':
case 'timestamp':
case 'timestampTz':
$column->default = new Expression('CURRENT_TIMESTAMP');
break;
}
}
}

/**
* Get the SQL for a generated virtual column modifier.
*
Expand Down
42 changes: 38 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RuntimeException;
use Illuminate\Support\Fluent;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;

class PostgresGrammar extends Grammar
Expand Down Expand Up @@ -533,6 +534,8 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
$this->parseTemporalType($column);

return 'date';
}

Expand Down Expand Up @@ -566,6 +569,8 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
$this->parseTemporalType($column);

return "time($column->precision) without time zone";
}

Expand All @@ -577,6 +582,8 @@ protected function typeTime(Fluent $column)
*/
protected function typeTimeTz(Fluent $column)
{
$this->parseTemporalType($column);

return "time($column->precision) with time zone";
}

Expand All @@ -588,9 +595,9 @@ protected function typeTimeTz(Fluent $column)
*/
protected function typeTimestamp(Fluent $column)
{
$type = "timestamp($column->precision) without time zone";
$this->parseTemporalType($column);

return $column->useCurrent ? "$type default CURRENT_TIMESTAMP" : $type;
return "timestamp($column->precision) without time zone";
}

/**
Expand All @@ -601,9 +608,9 @@ protected function typeTimestamp(Fluent $column)
*/
protected function typeTimestampTz(Fluent $column)
{
$type = "timestamp($column->precision) with time zone";
$this->parseTemporalType($column);

return $column->useCurrent ? "$type default CURRENT_TIMESTAMP" : $type;
return "timestamp($column->precision) with time zone";
}

/**
Expand Down Expand Up @@ -749,6 +756,33 @@ private function formatPostGisType(string $type)
return "geography($type, 4326)";
}

/**
* Parse the default value for a temporal column type.
*
* @param \Illuminate\Support\Fluent $column
* @return void
*/
protected function parseTemporalType(Fluent &$column)
{
if ($column->useCurrent) {
switch ($column->type) {
case 'date':
$column->default = new Expression('CURRENT_DATE');
break;
case 'time':
case 'timeTz':
$column->default = new Expression('CURRENT_TIME');
break;
case 'dateTime':
case 'dateTimeTz':
case 'timestamp':
case 'timestampTz':
$column->default = new Expression('CURRENT_TIMESTAMP');
break;
}
}
}

/**
* Get the SQL for a nullable column modifier.
*
Expand Down
36 changes: 35 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RuntimeException;
use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;

class SQLiteGrammar extends Grammar
Expand Down Expand Up @@ -542,6 +543,8 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
$this->parseTemporalType($column);

return 'date';
}

Expand All @@ -553,7 +556,9 @@ protected function typeDate(Fluent $column)
*/
protected function typeDateTime(Fluent $column)
{
return $column->useCurrent ? 'datetime default CURRENT_TIMESTAMP' : 'datetime';
$this->parseTemporalType($column);

return 'datetime';
}

/**
Expand All @@ -578,6 +583,8 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
$this->parseTemporalType($column);

return 'time';
}

Expand Down Expand Up @@ -746,6 +753,33 @@ public function typeMultiPolygon(Fluent $column)
return 'multipolygon';
}

/**
* Parse the default value for a temporal column type.
*
* @param \Illuminate\Support\Fluent $column
* @return void
*/
protected function parseTemporalType(Fluent &$column)
{
if ($column->useCurrent) {
switch ($column->type) {
case 'date':
$column->default = new Expression('CURRENT_DATE');
break;
case 'time':
case 'timeTz':
$column->default = new Expression('CURRENT_TIME');
break;
case 'dateTime':
case 'dateTimeTz':
case 'timestamp':
case 'timestampTz':
$column->default = new Expression('CURRENT_TIMESTAMP');
break;
}
}
}

/**
* Get the SQL for a nullable column modifier.
*
Expand Down
36 changes: 32 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Schema\Grammars;

use Illuminate\Support\Fluent;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;

class SqlServerGrammar extends Grammar
Expand Down Expand Up @@ -485,6 +486,8 @@ protected function typeJsonb(Fluent $column)
*/
protected function typeDate(Fluent $column)
{
$this->parseTemporalType($column);

return 'date';
}

Expand All @@ -496,9 +499,9 @@ protected function typeDate(Fluent $column)
*/
protected function typeDateTime(Fluent $column)
{
$type = $column->precision ? "datetime2($column->precision)" : 'datetime';
$this->parseTemporalType($column);

return $column->useCurrent ? "$type default CURRENT_TIMESTAMP" : $type;
return $column->precision ? "datetime2($column->precision)" : 'datetime';
}

/**
Expand All @@ -509,9 +512,9 @@ protected function typeDateTime(Fluent $column)
*/
protected function typeDateTimeTz(Fluent $column)
{
$type = "datetimeoffset($column->precision)";
$this->parseTemporalType($column);

return $column->useCurrent ? "$type default CURRENT_TIMESTAMP" : $type;
return "datetimeoffset($column->precision)";
}

/**
Expand All @@ -522,6 +525,8 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
$this->parseTemporalType($column);

return "time($column->precision)";
}

Expand Down Expand Up @@ -692,6 +697,29 @@ public function typeMultiPolygon(Fluent $column)
return 'geography';
}

/**
* Parse the default value for a temporal column type.
*
* @param \Illuminate\Support\Fluent $column
* @return void
*/
protected function parseTemporalType(Fluent &$column)
{
if ($column->useCurrent) {
switch ($column->type) {
case 'date':
case 'time':
case 'timeTz':
case 'dateTime':
case 'dateTimeTz':
case 'timestamp':
case 'timestampTz':
$column->default = new Expression('CURRENT_TIMESTAMP');
break;
}
}
}

/**
* Get the SQL for a collation column modifier.
*
Expand Down
Loading

0 comments on commit 5cbe957

Please sign in to comment.