Skip to content

Commit

Permalink
Add/alter columns should be separate statements (#159)
Browse files Browse the repository at this point in the history
* add/alter columns should be separate statements

* type hint compileAdd properly

* fix dropColumn as well

* changelog

* Update CHANGELOG.md

---------

Co-authored-by: Takayasu Oyama <taka.oyama@gmail.com>
  • Loading branch information
matthewjumpsoffbuildings and taka-oyama committed Dec 14, 2023
1 parent 03a5d97 commit 0d438b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Added
Changed
- `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156)

Fixed
- `Schema\Grammar::compileAdd()` `Schema\Grammar::compileChange()` `Schema\Grammar::compileChange()` now create separate statements (#159)

# v6.1.1 (2023-12-11)

Fixed
Expand Down
25 changes: 14 additions & 11 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ public function compileCreate(Blueprint $blueprint, Fluent $command)
*
* @param Blueprint $blueprint
* @param Fluent<string, mixed> $command
* @return string
* @return string[]
*/
public function compileAdd(Blueprint $blueprint, Fluent $command)
{
$columns = $this->prefixArray('add column', $this->getColumns($blueprint));

return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
return $this->prefixArray(
'alter table '.$this->wrapTable($blueprint).' add column',
$this->getColumns($blueprint)
);
}

/**
Expand All @@ -149,23 +150,25 @@ public function compileAdd(Blueprint $blueprint, Fluent $command)
*/
public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection)
{
$columns = $this->prefixArray('alter column', $this->getChangedColumns($blueprint));

return ['alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns)];
return $this->prefixArray(
'alter table '.$this->wrapTable($blueprint).' alter column',
$this->getChangedColumns($blueprint)
);
}

/**
* Compile a drop column command.
*
* @param Blueprint $blueprint
* @param Fluent<string, mixed> $command
* @return string
* @return string[]
*/
public function compileDropColumn(Blueprint $blueprint, Fluent $command)
{
$columns = $this->prefixArray('drop column', $this->wrapArray($command->columns));

return 'alter table '.$this->wrapTable($blueprint).' '.implode(', ', $columns);
return $this->prefixArray(
'alter table '.$this->wrapTable($blueprint).' drop column',
$this->wrapArray($command->columns)
);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions tests/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ public function testAddColumn(): void

$blueprint = new Blueprint('Test3', function (Blueprint $table) {
$table->string('description', 255);
$table->integer('value');
});

$queries = $blueprint->toSql($conn, new Grammar());
$this->assertEquals(
$this->assertEquals([
'alter table `Test3` add column `description` string(255) not null',
$queries[0]
'alter table `Test3` add column `value` int64 not null'
],
$queries
);
}

Expand All @@ -125,12 +128,15 @@ public function testChangeColumn(): void

$blueprint = new Blueprint('Test3', function (Blueprint $table) {
$table->string('description', 512)->change();
$table->float('value')->change();
});

$queries = $blueprint->toSql($conn, new Grammar());
$this->assertEquals(
$this->assertEquals([
'alter table `Test3` alter column `description` string(512) not null',
$queries[0]
'alter table `Test3` alter column `value` float64 not null',
],
$queries
);
}

Expand Down

0 comments on commit 0d438b3

Please sign in to comment.