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] Support useCurrentOnUpdate for MySQL datetime column types #36817

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
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,11 @@ protected function typeDateTime(Fluent $column)
{
$columnType = $column->precision ? "datetime($column->precision)" : 'datetime';

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

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

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

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,42 @@ public function testAddingDateTime()
$this->assertSame('alter table `users` add `foo` datetime(1) not null', $statements[0]);
}

public function testAddingDateTimeWithDefaultCurrent()
{
$blueprint = new Blueprint('users');
$blueprint->dateTime('foo')->useCurrent();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `foo` datetime default CURRENT_TIMESTAMP not null', $statements[0]);
}

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

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

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

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