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

[10.x] Replace Deprecated DBAL Comparator creation with schema aware Comparator #46517

Merged
merged 10 commits into from
Mar 22, 2023
3 changes: 1 addition & 2 deletions src/Illuminate/Database/Schema/Grammars/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\Database\Schema\Grammars;

use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Illuminate\Database\Connection;
Expand Down Expand Up @@ -60,7 +59,7 @@ protected static function getChangedDiff($grammar, Blueprint $blueprint, SchemaM
{
$current = $schema->introspectTable($grammar->getTablePrefix().$blueprint->getTable());

return (new Comparator)->compareTables(
return $schema->createComparator()->compareTables(
$current, static::getTableWithColumnChanges($blueprint, $current)
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ protected function getDefaultValue($value)
*/
public function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema)
{
$table = $this->getTablePrefix().$blueprint->getTable();
$tableName = $this->getTablePrefix().$blueprint->getTable();

return tap(new TableDiff($table), function ($tableDiff) use ($schema, $table) {
$tableDiff->fromTable = $schema->introspectTable($table);
});
$table = $schema->introspectTable($tableName);

return new TableDiff(tableName: $tableName, fromTable: $table);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public function testRenamingAndChangingColumnsWork()

// Expect one of the following two query sequences to be present...
$expected = [
[
'CREATE TEMPORARY TABLE __temp__users AS SELECT name, age FROM users',
'DROP TABLE users',
'CREATE TABLE users (name VARCHAR(255) NOT NULL, age INTEGER NOT NULL)',
'INSERT INTO users (name, age) SELECT name, age FROM __temp__users',
'DROP TABLE __temp__users',
'CREATE TEMPORARY TABLE __temp__users AS SELECT name, age FROM users',
'DROP TABLE users',
'CREATE TABLE users (first_name VARCHAR(255) NOT NULL, age VARCHAR(255) NOT NULL COLLATE "BINARY")',
'INSERT INTO users (first_name, age) SELECT name, age FROM __temp__users',
'DROP TABLE __temp__users',
],
[
'CREATE TEMPORARY TABLE __temp__users AS SELECT name, age FROM users',
'DROP TABLE users',
Expand Down Expand Up @@ -327,6 +339,13 @@ public function testChangingCharColumnsWork()
$queries = $blueprint->toSql($this->db->connection(), new SQLiteGrammar);

$expected = [
[
'CREATE TEMPORARY TABLE __temp__users AS SELECT name FROM users',
'DROP TABLE users',
'CREATE TABLE users (name CHAR(50) NOT NULL)',
'INSERT INTO users (name) SELECT name FROM __temp__users',
'DROP TABLE __temp__users',
],
[
'CREATE TEMPORARY TABLE __temp__users AS SELECT name FROM users',
'DROP TABLE users',
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,29 @@ public function testChangeToTextColumn()
$this->assertEquals($expected, $queries);
}
}

public function testChangeTextColumnToTextColumn()
{
if ($this->driver !== 'mysql') {
$this->markTestSkipped('Test requires a MySQL connection.');
}

Schema::create('test', static function (Blueprint $table) {
$table->text('test_column');
});

foreach (['tinyText', 'mediumText', 'longText'] as $type) {
$blueprint = new Blueprint('test', function ($table) use ($type) {
$table->$type('test_column')->change();
});

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

$uppercase = strtoupper($type);

$expected = ["ALTER TABLE test CHANGE test_column test_column $uppercase NOT NULL"];

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