From db825c3efeb95f6ac88fab62dad5dd746fa9954b Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sun, 8 Nov 2020 00:06:14 +1000 Subject: [PATCH 1/3] Create tests for setting current timestamp on update --- .../DatabaseMySqlSchemaGrammarTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 1d35d05d097a..c89af64847ad 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -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'); From 86cc50006428142b9e34f62aa80966333f3860dd Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sun, 8 Nov 2020 00:06:40 +1000 Subject: [PATCH 2/3] Add method for setting current timestamp on update --- src/Illuminate/Database/Schema/ColumnDefinition.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Database/Schema/ColumnDefinition.php b/src/Illuminate/Database/Schema/ColumnDefinition.php index 4bc047fbf0d4..6b1e4f11f839 100644 --- a/src/Illuminate/Database/Schema/ColumnDefinition.php +++ b/src/Illuminate/Database/Schema/ColumnDefinition.php @@ -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 From cb39e184f1ed49cbc66e7becb1568716cb6a2e27 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sun, 8 Nov 2020 00:08:21 +1000 Subject: [PATCH 3/3] Optionally set current timestamp on update --- src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index 75ef80e45ae4..c2952e47926c 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -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; } /**