Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/Database/Schema/BlueprintState.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ protected function preserveExistingColumnAttributes(Fluent $column): void
continue;
}

$attributes = $column->getAttributes();

foreach ($this->preservedAttributes as $attribute) {
if (!isset($column[$attribute]) && isset($existing[$attribute])) {
if (!array_key_exists($attribute, $attributes) && isset($existing[$attribute])) {
$column[$attribute] = $existing[$attribute];
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Schema/Grammars/Concerns/MySqlBasedGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function compileChange(Blueprint $blueprint, Fluent $command)
$oldColumn = new ColumnDefinition($oldColumn);
}

$attributes = $column->getAttributes();

foreach ($this->modifiers as $modifier) {
if (method_exists($this, $method = "modify{$modifier}")) {
$mod = strtolower($modifier);
$col = isset($oldColumn->{$mod}) && !isset($column->{$mod}) ? $oldColumn : $column;
$col = isset($oldColumn->{$mod}) && !array_key_exists($mod, $attributes) ? $oldColumn : $column;
$sql .= $this->{$method}($blueprint, $col);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ public function compileChange(Blueprint $blueprint, Fluent $command)
$oldColumn = new ColumnDefinition($oldColumn);
}

$attributes = $column->getAttributes();

foreach ($this->modifiers as $modifier) {
if ($modifier === 'Collate') {
continue;
}

if (method_exists($this, $method = "modify{$modifier}")) {
$mod = strtolower($modifier);
$col = isset($oldColumn->{$mod}) && ! isset($column->{$mod}) ? $oldColumn : $column;
$col = isset($oldColumn->{$mod}) && ! array_key_exists($mod, $attributes) ? $oldColumn : $column;
$constraints = (array) $this->{$method}($blueprint, $col);

foreach ($constraints as $constraint) {
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function compileChange(Blueprint $blueprint, Fluent $command)
$oldColumn = new ColumnDefinition($oldColumn);
}

$attributes = $column->getAttributes();

foreach ($this->modifiers as $modifier) {
if (method_exists($this, $method = "modify{$modifier}")) {
$mod = strtolower($modifier);
$col = isset($oldColumn->{$mod}) && !isset($column->{$mod}) ? $oldColumn : $column;
$col = isset($oldColumn->{$mod}) && !array_key_exists($mod, $attributes) ? $oldColumn : $column;
$sql .= $this->{$method}($blueprint, $col);
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/Schema/Grammars/MySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,19 @@ public function testQuoteInDefaultValue()
$statements = $this->runBlueprint($initialBlueprint);
$this->assertSame("alter table `users` add `name` varchar(255) not null default 'O''Brian'", $statements[0]);
}

public function testChangeCanRemoveDefaultWithNull()
{
$initialBlueprint = $this->getBlueprint('users');
$initialBlueprint->string('name')->default('admin');

$statements = $this->runBlueprint($initialBlueprint);
$this->assertSame("alter table `users` add `name` varchar(255) not null default 'admin'", $statements[0]);

$changedBlueprint = $this->getBlueprint('users');
$changedBlueprint->string('name')->nullable()->default(null)->change();

$statements = $this->runBlueprint($changedBlueprint);
$this->assertSame('alter table `users` modify `name` varchar(255) null', $statements[0]);
}
}
15 changes: 15 additions & 0 deletions tests/Database/Schema/Grammars/PostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,19 @@ public function testQuoteInDefaultValue()
$parts = explode(', ', $statements[0]);
$this->assertSame("alter table \"users\" add column \"name\" varchar(255) not null default 'O''Brian'", $parts[0]);
}

public function testChangeCanRemoveDefaultWithNull()
{
$initialBlueprint = $this->getBlueprint('users');
$initialBlueprint->string('name')->default('admin');

$statements = $this->runBlueprint($initialBlueprint);
$this->assertSame("alter table \"users\" add column \"name\" varchar(255) not null default 'admin'", $statements[0]);

$changedBlueprint = $this->getBlueprint('users');
$changedBlueprint->string('name')->nullable()->default(null)->change();

$statements = $this->runBlueprint($changedBlueprint);
$this->assertSame('alter table "users" alter column "name" type varchar(255), alter column "name" drop not null, alter column "name" drop default, alter column "name" drop identity if exists', $statements[0]);
}
}
12 changes: 12 additions & 0 deletions tests/Database/Schema/Grammars/SQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,16 @@ public function testChangeRebuildsTableExactlyOnce(): void

$this->assertSame(1, $rebuilds, 'A single ->change() must rebuild the table exactly once.');
}

public function testChangeCanRemoveDefaultWithNull(): void
{
[$pdo, , $builder] = $this->bootSqlite("CREATE TABLE users (id integer primary key, name varchar not null default 'bob')");

// An explicit null default must remove the existing default, not keep it.
$builder->table('users', fn (Blueprint $table) => $table->string('name')->nullable()->default(null)->change());

$info = $this->columnInfo($pdo, 'users', 'name');
$this->assertSame(0, (int) $info['notnull']);
$this->assertNull($info['dflt_value']);
}
}
16 changes: 16 additions & 0 deletions tests/Database/Schema/Grammars/SqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ public function testQuoteInDefaultValue()
$statements = $this->runBlueprint($initialBlueprint);
$this->assertSame("alter table \"users\" add \"name\" nvarchar(255) not null default 'O''Brian'", $statements[0]);
}

public function testChangeCanRemoveDefaultWithNull()
{
$initialBlueprint = $this->getBlueprint('users');
$initialBlueprint->string('name')->default('admin');

$statements = $this->runBlueprint($initialBlueprint);
$this->assertSame('alter table "users" add "name" nvarchar(255) not null default \'admin\'', $statements[0]);

$changedBlueprint = $this->getBlueprint('users');
$changedBlueprint->string('name')->nullable()->default(null)->change();

$statements = $this->runBlueprint($changedBlueprint);
$this->assertCount(2, $statements);
$this->assertSame('alter table "users" alter column "name" nvarchar(255) null', $statements[1]);
}
}
Loading