Skip to content

Commit

Permalink
[10.x] Replace Deprecated DBAL Comparator creation with schema aware …
Browse files Browse the repository at this point in the history
…Comparator (#46517)

* Replace Deprecated DBAL Comparator creation with schema aware Comparator

Reference to change in DBAL: doctrine/dbal#4746

* Add additional test for changing text columns to different text columns

* Revert test change

* Remove redundant use statement

* Rework GetDoctrineTableDiff to remove deprication call due to missing constructor parameter

* Change Doctrine Integration tests removing Binary additions

These are no longer included as provider in now schema aware when generating the SQL

* Add additional possibilities for output SQL

* Adding missing comma

* Add missing Binary in test

* Update Grammar.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
liamh101 and taylorotwell committed Mar 22, 2023
1 parent 7046e9b commit f5f91ae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
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);
}
}
}

0 comments on commit f5f91ae

Please sign in to comment.