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] Allow adding multiple columns after a column #36145

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 48 additions & 10 deletions src/Illuminate/Database/Schema/Blueprint.php
Expand Up @@ -71,6 +71,13 @@ class Blueprint
*/
public $temporary = false;

/**
* The column to add new columns after.
*
* @var string
*/
public $after;

/**
* Create a new schema blueprint.
*
Expand Down Expand Up @@ -836,14 +843,12 @@ public function unsignedBigInteger($column, $autoIncrement = false)
*/
public function foreignId($column)
{
$this->columns[] = $column = new ForeignIdColumnDefinition($this, [
return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
'type' => 'bigInteger',
'name' => $column,
'autoIncrement' => false,
'unsigned' => true,
]);

return $column;
]));
}

/**
Expand Down Expand Up @@ -1189,10 +1194,10 @@ public function uuid($column)
*/
public function foreignUuid($column)
{
return $this->columns[] = new ForeignIdColumnDefinition($this, [
return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
'type' => 'uuid',
'name' => $column,
]);
]));
}

/**
Expand Down Expand Up @@ -1435,6 +1440,41 @@ public function rememberToken()
return $this->string('remember_token', 100)->nullable();
}

/**
* Add the columns from the callback after the given column.
*
* @param string $column
* @param \Closure $callback
* @return void
*/
public function after($column, Closure $callback)
{
$this->after = $column;

$callback($this);

$this->after = null;
}

/**
* Add a new column definition to the blueprint.
*
* @param \Illuminate\Database\Schema\ColumnDefinition $definition
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
protected function addColumnDefinition($definition)
{
$this->columns[] = $definition;

if ($this->after) {
$definition->after($this->after);

$this->after = $definition->name;
}

return $definition;
}

/**
* Add a new index command to the blueprint.
*
Expand Down Expand Up @@ -1504,11 +1544,9 @@ protected function createIndexName($type, array $columns)
*/
public function addColumn($type, $name, array $parameters = [])
{
$this->columns[] = $column = new ColumnDefinition(
return $this->addColumnDefinition(new ColumnDefinition(
array_merge(compact('type', 'name'), $parameters)
);

return $column;
));
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Expand Up @@ -504,6 +504,19 @@ public function testAddingColumnAfterAnotherColumn()
$this->assertSame('alter table `users` add `name` varchar(255) not null after `foo`', $statements[0]);
}

public function testAddingMultipleColumnsAfterAnotherColumn()
{
$blueprint = new Blueprint('users');
$blueprint->after('foo', function ($blueprint) {
$blueprint->string('one');
$blueprint->string('two');
});
$blueprint->string('three');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add `one` varchar(255) not null after `foo`, add `two` varchar(255) not null after `one`, add `three` varchar(255) not null', $statements[0]);
}

public function testAddingGeneratedColumn()
{
$blueprint = new Blueprint('products');
Expand Down