-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[9.x] Add support for native column modifying #45281
Changes from 18 commits
c656088
ee221d2
137be15
09afcd2
6c6634a
96a38e9
e53fcfe
d067475
513859c
2d2f959
a2278aa
c7e5250
f355977
b2ea87b
6fb23da
4a15bb7
8ecf560
56002bf
0a881dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,15 @@ public function toSql(Connection $connection, Grammar $grammar) | |
$this->ensureCommandsAreValid($connection); | ||
|
||
foreach ($this->commands as $command) { | ||
if (in_array($command->name, $grammar->getFluentCommands()) | ||
&& (! $command->column->change && ! isset($command->column->{lcfirst($command->name)}) | ||
|| ($command->column->change && ! $connection->usingNativeSchemaOperations()))) { | ||
// Doctrine DBAL handles fluent commands on change. Also, there is no need to | ||
// compile them when creating or adding a column when its value is not set | ||
// but we should always compile them when using native column modifying. | ||
continue; | ||
} | ||
|
||
$method = 'compile'.ucfirst($command->name); | ||
|
||
if (method_exists($grammar, $method) || $grammar::hasMacro($method)) { | ||
|
@@ -188,6 +197,10 @@ protected function commandsNamed(array $names) | |
*/ | ||
protected function addImpliedCommands(Grammar $grammar) | ||
{ | ||
if ($this->hasAutoIncrementColumn() && ! $this->creating()) { | ||
array_unshift($this->commands, $this->createCommand('autoIncrementStartingValues')); | ||
} | ||
|
||
if (count($this->getAddedColumns()) > 0 && ! $this->creating()) { | ||
array_unshift($this->commands, $this->createCommand('add')); | ||
} | ||
|
@@ -243,13 +256,7 @@ public function addFluentCommands(Grammar $grammar) | |
{ | ||
foreach ($this->columns as $column) { | ||
foreach ($grammar->getFluentCommands() as $commandName) { | ||
$attributeName = lcfirst($commandName); | ||
|
||
if (! isset($column->{$attributeName})) { | ||
continue; | ||
} | ||
|
||
$value = $column->{$attributeName}; | ||
$value = $column->{lcfirst($commandName)}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain the reason for this change. Seems breaking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not breaking, many tests would fail if it was. I removed this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TLDR; it's removed from here and added (with more conditions) to its parent method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's for handling |
||
|
||
$this->addCommand( | ||
$commandName, compact('value', 'column') | ||
|
@@ -1794,7 +1801,7 @@ public function getChangedColumns() | |
*/ | ||
public function hasAutoIncrementColumn() | ||
{ | ||
return ! is_null(collect($this->getAddedColumns())->first(function ($column) { | ||
return ! is_null(collect($this->columns)->first(function ($column) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why no longer using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It now supports both added and modified columns (looking for |
||
return $column->autoIncrement === true; | ||
})); | ||
} | ||
|
@@ -1810,7 +1817,7 @@ public function autoIncrementingStartingValues() | |
return []; | ||
} | ||
|
||
return collect($this->getAddedColumns())->mapWithKeys(function ($column) { | ||
return collect($this->columns)->mapWithKeys(function ($column) { | ||
return $column->autoIncrement === true | ||
? [$column->name => $column->get('startingValue', $column->get('from'))] | ||
: [$column->name => null]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to handle auto-increment starting value using
from
modifier, before this PRfrom
was only supported when adding a column and creating a table. I removed this fromcompileAdd
and add it here to handle both add and change. now it is supported to usefrom
modifier withchange
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's much cleaner to handle auto-increment starting value as a fluent command for MySQL and PostgreSQL. but I didn't want to change a lot of code at once and make it hard to review.