Skip to content

Commit

Permalink
Complete precision support on datetime columns
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofreitas committed Nov 17, 2017
1 parent 9285f28 commit 939875f
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 164 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,12 @@ public function time($column, $precision = 0)
* Create a new time column (with time zone) on the table.
*
* @param string $column
* @param int $precision
* @return \Illuminate\Support\Fluent
*/
public function timeTz($column)
public function timeTz($column, $precision = 0)
{
return $this->addColumn('timeTz', $column);
return $this->addColumn('timeTz', $column, compact('precision'));
}

/**
Expand Down
18 changes: 7 additions & 11 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ protected function typeDateTime(Fluent $column)
}

/**
* Create the column definition for a date-time type.
* Create the column definition for a date-time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
Expand All @@ -615,18 +615,18 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
return 'time';
return $column->precision ? "time($column->precision)" : 'time';
}

/**
* Create the column definition for a time type.
* Create the column definition for a time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimeTz(Fluent $column)
{
return 'time';
return $this->typeTime($column);
}

/**
Expand All @@ -637,17 +637,13 @@ protected function typeTimeTz(Fluent $column)
*/
protected function typeTimestamp(Fluent $column)
{
if ($column->useCurrent) {
return $column->precision
? "timestamp($column->precision) default CURRENT_TIMESTAMP"
: 'timestamp default CURRENT_TIMESTAMP';
}
$columnType = $column->precision ? "timestamp($column->precision)" : 'timestamp';

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

/**
* Create the column definition for a timestamp type.
* Create the column definition for a timestamp (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
Expand Down
22 changes: 9 additions & 13 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ protected function typeDateTime(Fluent $column)
}

/**
* Create the column definition for a date-time type.
* Create the column definition for a date-time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
Expand All @@ -566,18 +566,18 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
return 'time(0) without time zone';
return "time($column->precision) without time zone";
}

/**
* Create the column definition for a time type.
* Create the column definition for a time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimeTz(Fluent $column)
{
return 'time(0) with time zone';
return "time($column->precision) with time zone";
}

/**
Expand All @@ -588,26 +588,22 @@ protected function typeTimeTz(Fluent $column)
*/
protected function typeTimestamp(Fluent $column)
{
if ($column->useCurrent) {
return "timestamp($column->precision) without time zone default CURRENT_TIMESTAMP($column->precision)";
}
$columnType = "timestamp($column->precision) without time zone";

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

/**
* Create the column definition for a timestamp type.
* Create the column definition for a timestamp (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimestampTz(Fluent $column)
{
if ($column->useCurrent) {
return "timestamp($column->precision) with time zone default CURRENT_TIMESTAMP($column->precision)";
}
$columnType = "timestamp($column->precision) with time zone";

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

/**
Expand Down
22 changes: 7 additions & 15 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ protected function typeDateTime(Fluent $column)
}

/**
* Create the column definition for a date-time type.
* Create the column definition for a date-time (with time zone) type.
*
* Note: "SQLite does not have a storage class set aside for storing dates and/or times."
* @link https://www.sqlite.org/datatype3.html
Expand All @@ -567,7 +567,7 @@ protected function typeDateTime(Fluent $column)
*/
protected function typeDateTimeTz(Fluent $column)
{
return 'datetime';
return $this->typeDateTime($column);
}

/**
Expand All @@ -582,14 +582,14 @@ protected function typeTime(Fluent $column)
}

/**
* Create the column definition for a time type.
* Create the column definition for a time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimeTz(Fluent $column)
{
return 'time';
return $this->typeTime($column);
}

/**
Expand All @@ -600,26 +600,18 @@ protected function typeTimeTz(Fluent $column)
*/
protected function typeTimestamp(Fluent $column)
{
if ($column->useCurrent) {
return 'datetime default CURRENT_TIMESTAMP';
}

return 'datetime';
return $column->useCurrent ? 'datetime default CURRENT_TIMESTAMP' : 'datetime';
}

/**
* Create the column definition for a timestamp type.
* Create the column definition for a timestamp (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimestampTz(Fluent $column)
{
if ($column->useCurrent) {
return 'datetime default CURRENT_TIMESTAMP';
}

return 'datetime';
return $this->typeTimestamp($column);
}

/**
Expand Down
24 changes: 10 additions & 14 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ protected function typeDateTime(Fluent $column)
}

/**
* Create the column definition for a date-time type.
* Create the column definition for a date-time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
Expand All @@ -518,18 +518,18 @@ protected function typeDateTimeTz(Fluent $column)
*/
protected function typeTime(Fluent $column)
{
return 'time';
return $column->precision ? "time($column->precision)" : 'time';
}

/**
* Create the column definition for a time type.
* Create the column definition for a time (with time zone) type.
*
* @param \Illuminate\Support\Fluent $column
* @return string
*/
protected function typeTimeTz(Fluent $column)
{
return 'time';
return $this->typeTime($column);
}

/**
Expand All @@ -540,17 +540,13 @@ protected function typeTimeTz(Fluent $column)
*/
protected function typeTimestamp(Fluent $column)
{
if ($column->useCurrent) {
return $column->precision
? "datetime2($column->precision) default CURRENT_TIMESTAMP"
: 'datetime default CURRENT_TIMESTAMP';
}
$columnType = $column->precision ? "datetime2($column->precision)" : 'datetime';

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

/**
* Create the column definition for a timestamp type.
* Create the column definition for a timestamp (with time zone) type.
*
* @link https://msdn.microsoft.com/en-us/library/bb630289(v=sql.120).aspx
*
Expand All @@ -560,9 +556,9 @@ protected function typeTimestamp(Fluent $column)
protected function typeTimestampTz(Fluent $column)
{
if ($column->useCurrent) {
return $column->precision
? "datetimeoffset($column->precision) default CURRENT_TIMESTAMP"
: 'datetimeoffset default CURRENT_TIMESTAMP';
$columnType = $column->precision ? "datetimeoffset($column->precision)" : 'datetimeoffset';

return "$columnType default CURRENT_TIMESTAMP";
}

return "datetimeoffset($column->precision)";
Expand Down
Loading

0 comments on commit 939875f

Please sign in to comment.