Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add MySQL column modifier to set current timestamp on update #35143

Merged
merged 3 commits into from
Nov 9, 2020
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
1 change: 1 addition & 0 deletions src/Illuminate/Database/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @method $this unique(string $indexName = null) Add a unique index
* @method $this unsigned() Set the INTEGER column as UNSIGNED (MySQL)
* @method $this useCurrent() Set the TIMESTAMP column to use CURRENT_TIMESTAMP as default value
* @method $this useCurrentOnUpdate() Set the TIMESTAMP column to use CURRENT_TIMESTAMP when updating (MySQL)
* @method $this virtualAs(string $expression) Create a virtual generated column (MySQL/SQLite)
*/
class ColumnDefinition extends Fluent
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,9 +706,11 @@ protected function typeTimestamp(Fluent $column)
{
$columnType = $column->precision ? "timestamp($column->precision)" : 'timestamp';

$defaultCurrent = $column->precision ? "CURRENT_TIMESTAMP($column->precision)" : 'CURRENT_TIMESTAMP';
$current = $column->precision ? "CURRENT_TIMESTAMP($column->precision)" : 'CURRENT_TIMESTAMP';

return $column->useCurrent ? "$columnType default $defaultCurrent" : $columnType;
$columnType = $column->useCurrent ? "$columnType default $current" : $columnType;

return $column->useCurrentOnUpdate ? "$columnType on update $current" : $columnType;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,24 @@ public function testAddingTimestampWithDefaultCurrentSpecifyingPrecision()
$this->assertSame('alter table `users` add `created_at` timestamp(1) default CURRENT_TIMESTAMP(1) not null', $statements[0]);
}

public function testAddingTimestampWithOnUpdateCurrentSpecifyingPrecision()
{
$blueprint = new Blueprint('users');
$blueprint->timestamp('created_at', 1)->useCurrentOnUpdate();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `created_at` timestamp(1) on update CURRENT_TIMESTAMP(1) not null', $statements[0]);
}

public function testAddingTimestampWithDefaultCurrentAndOnUpdateCurrentSpecifyingPrecision()
{
$blueprint = new Blueprint('users');
$blueprint->timestamp('created_at', 1)->useCurrent()->useCurrentOnUpdate();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `created_at` timestamp(1) default CURRENT_TIMESTAMP(1) on update CURRENT_TIMESTAMP(1) not null', $statements[0]);
}

public function testAddingTimestampTz()
{
$blueprint = new Blueprint('users');
Expand Down