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

[11.x] Test modifying nullable columns #50708

Merged
Merged
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
64 changes: 64 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,70 @@ public function testChangeTextColumnToTextColumn()
}
}

public function testModifyNullableColumn()
{
if (! in_array($this->driver, ['mysql', 'mariadb'])) {
$this->markTestSkipped('Test requires a MySQL or a MariaDB connection.');
}

Schema::create('test', static function (Blueprint $table) {
$table->string('not_null_column_to_not_null');
$table->string('not_null_column_to_nullable');
$table->string('nullable_column_to_nullable')->nullable();
$table->string('nullable_column_to_not_null')->nullable();
});

$blueprint = new Blueprint('test', function ($table) {
$table->text('not_null_column_to_not_null')->change();
$table->text('not_null_column_to_nullable')->nullable()->change();
$table->text('nullable_column_to_nullable')->nullable()->change();
$table->text('nullable_column_to_not_null')->change();
});

$queries = $blueprint->toSql($this->getConnection(), $this->getConnection()->getSchemaGrammar());

$expected = [
'alter table `test` '
.'modify `not_null_column_to_not_null` text not null, '
.'modify `not_null_column_to_nullable` text null, '
.'modify `nullable_column_to_nullable` text null, '
.'modify `nullable_column_to_not_null` text not null',
];

$this->assertEquals($expected, $queries);
}

public function testChangeNullableColumn()
{
Schema::create('test', function (Blueprint $table) {
$table->string('not_null_column_to_not_null');
$table->string('not_null_column_to_nullable');
$table->string('nullable_column_to_nullable')->nullable();
$table->string('nullable_column_to_not_null')->nullable();
});

$columns = collect(Schema::getColumns('test'));

$this->assertFalse($columns->firstWhere('name', 'not_null_column_to_not_null')['nullable']);
$this->assertFalse($columns->firstWhere('name', 'not_null_column_to_nullable')['nullable']);
$this->assertTrue($columns->firstWhere('name', 'nullable_column_to_nullable')['nullable']);
$this->assertTrue($columns->firstWhere('name', 'nullable_column_to_not_null')['nullable']);

Schema::table('test', function (Blueprint $table) {
$table->text('not_null_column_to_not_null')->change();
$table->text('not_null_column_to_nullable')->nullable()->change();
$table->text('nullable_column_to_nullable')->nullable()->change();
$table->text('nullable_column_to_not_null')->change();
});

$columns = collect(Schema::getColumns('test'));

$this->assertFalse($columns->firstWhere('name', 'not_null_column_to_not_null')['nullable']);
$this->assertTrue($columns->firstWhere('name', 'not_null_column_to_nullable')['nullable']);
$this->assertTrue($columns->firstWhere('name', 'nullable_column_to_nullable')['nullable']);
$this->assertFalse($columns->firstWhere('name', 'nullable_column_to_not_null')['nullable']);
}

public function testRenameColumnWithDefault()
{
Schema::create('test', static function (Blueprint $table) {
Expand Down