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
[8.x] Improve support for custom Doctrine column types #40119
Merged
taylorotwell
merged 25 commits into
laravel:8.x
from
bakerkretzmar:feature/custom-column-types
Dec 22, 2021
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2b8e543
Add failing test demonstrating `renameColumn` error with custom Doctr…
bakerkretzmar 8e8d45c
Use `Schema::registerCustomDoctrineType()` to register custom types a…
bakerkretzmar 2ecc82d
Test on MySQL
bakerkretzmar 447df71
Add duplicate test that fails the second time when using a custom column
bakerkretzmar d0fcae9
Always register type mappings even if the type has already been added
bakerkretzmar d9c75a3
Wip
bakerkretzmar 9d957ef
Add initial failing tests for registering custom types with multiple …
bakerkretzmar bd3d3cb
Move custom type registration into Connection, and register all custo…
bakerkretzmar 6f97406
Merge branch '8.x' into fix-doctrine-type-registration
bakerkretzmar 67c9612
Changed test names to camelCase
TomHAnderson d181640
Merge pull request #6 from TomHAnderson/hotfix/doctrine-type-registra…
bakerkretzmar a3a4d0f
Update test to check column type registration on all database drivers
bakerkretzmar e20bedc
Wip
bakerkretzmar 2f66eb9
Formatting
bakerkretzmar 990830d
Wip
bakerkretzmar 492c528
Begin work on timestamp tests
TomHAnderson c0dbfc4
Rename method
bakerkretzmar e35bfc2
Move loop / config type registration into method
bakerkretzmar 4af54e2
Add assertions for database column type
bakerkretzmar 00d2aaf
Merge pull request #5 from bakerkretzmar/fix-doctrine-type-registration
bakerkretzmar 5c7932d
Merge branch 'feature/custom-column-types' into feature/timestamp-typ…
bakerkretzmar b45d753
Update src/Illuminate/Database/DBAL/TimestampType.php
bakerkretzmar 7e4e270
Merge pull request #9 from TomHAnderson/feature/timestamp-type-test
bakerkretzmar 741233a
Merge branch '8.x' into feature/custom-column-types
bakerkretzmar c42df73
formatting
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/Integration/Database/ConfigureCustomDoctrineTypeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\SchemaTest; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
use Illuminate\Database\Grammar; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Tests\Integration\Database\DatabaseTestCase; | ||
|
||
class ConfigureCustomDoctrineTypeTest extends DatabaseTestCase | ||
{ | ||
protected function defineEnvironment($app) | ||
{ | ||
$app['config']['database.connections.sqlite.database'] = ':memory:'; | ||
$app['config']['database.dbal.types'] = [ | ||
'bit' => MySQLBitType::class, | ||
'xml' => PostgresXmlType::class, | ||
]; | ||
} | ||
|
||
public function testRegisterCustomDoctrineTypesWithNonDefaultDatabaseConnections() | ||
{ | ||
$this->assertTrue( | ||
DB::connection() | ||
->getDoctrineSchemaManager() | ||
->getDatabasePlatform() | ||
->hasDoctrineTypeMappingFor('xml') | ||
); | ||
|
||
// Custom type mappings are registered for a connection when it's created, | ||
// this is not the default connection but it has the custom type mappings | ||
$this->assertTrue( | ||
DB::connection('sqlite') | ||
->getDoctrineSchemaManager() | ||
->getDatabasePlatform() | ||
->hasDoctrineTypeMappingFor('xml') | ||
); | ||
} | ||
|
||
public function testRenameConfiguredCustomDoctrineColumnTypeWithPostgres() | ||
{ | ||
if ($this->driver !== 'pgsql') { | ||
$this->markTestSkipped('Test requires a Postgres connection.'); | ||
} | ||
|
||
Grammar::macro('typeXml', function () { | ||
return 'xml'; | ||
}); | ||
|
||
Schema::create('test', function (Blueprint $table) { | ||
$table->addColumn('xml', 'test_column'); | ||
}); | ||
|
||
Schema::table('test', function (Blueprint $table) { | ||
$table->renameColumn('test_column', 'renamed_column'); | ||
}); | ||
|
||
$this->assertFalse(Schema::hasColumn('test', 'test_column')); | ||
$this->assertTrue(Schema::hasColumn('test', 'renamed_column')); | ||
} | ||
|
||
public function testRenameConfiguredCustomDoctrineColumnTypeWithMysql() | ||
{ | ||
if ($this->driver !== 'mysql') { | ||
$this->markTestSkipped('Test requires a MySQL connection.'); | ||
} | ||
|
||
Grammar::macro('typeBit', function () { | ||
return 'bit'; | ||
}); | ||
|
||
Schema::create('test', function (Blueprint $table) { | ||
$table->addColumn('bit', 'test_column'); | ||
}); | ||
|
||
Schema::table('test', function (Blueprint $table) { | ||
$table->renameColumn('test_column', 'renamed_column'); | ||
}); | ||
|
||
$this->assertFalse(Schema::hasColumn('test', 'test_column')); | ||
$this->assertTrue(Schema::hasColumn('test', 'renamed_column')); | ||
} | ||
} | ||
|
||
class PostgresXmlType extends Type | ||
{ | ||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | ||
{ | ||
return 'xml'; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'xml'; | ||
} | ||
} | ||
|
||
class MySQLBitType extends Type | ||
{ | ||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) | ||
{ | ||
return 'bit'; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'bit'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database\DBAL; | ||
|
||
use Illuminate\Database\DBAL\TimestampType; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Tests\Integration\Database\DatabaseTestCase; | ||
|
||
class TimestampTypeTest extends DatabaseTestCase | ||
{ | ||
protected function defineEnvironment($app) | ||
{ | ||
$app['config']['database.dbal.types'] = [ | ||
'timestamp' => TimestampType::class, | ||
]; | ||
} | ||
|
||
public function testRegisterTimestampTypeOnConnection() | ||
{ | ||
$this->assertTrue( | ||
$this->app['db']->connection() | ||
->getDoctrineSchemaManager() | ||
->getDatabasePlatform() | ||
->hasDoctrineTypeMappingFor('timestamp') | ||
); | ||
} | ||
|
||
public function testChangeDatetimeColumnToTimestampColumn() | ||
{ | ||
Schema::create('test', function (Blueprint $table) { | ||
$table->addColumn('datetime', 'datetime_to_timestamp'); | ||
}); | ||
|
||
Schema::table('test', function (Blueprint $table) { | ||
$table->timestamp('datetime_to_timestamp')->nullable(true)->change(); | ||
}); | ||
|
||
$this->assertTrue(Schema::hasColumn('test', 'datetime_to_timestamp')); | ||
// Only Postgres and MySQL actually have a timestamp type | ||
in_array($this->driver, ['pgsql', 'mysql']) | ||
? $this->assertSame('timestamp', Schema::getColumnType('test', 'datetime_to_timestamp')) | ||
: $this->assertSame('datetime', Schema::getColumnType('test', 'datetime_to_timestamp')); | ||
} | ||
|
||
public function testChangeTimestampColumnToDatetimeColumn() | ||
{ | ||
Schema::create('test', function (Blueprint $table) { | ||
$table->addColumn('timestamp', 'timestamp_to_datetime'); | ||
}); | ||
|
||
Schema::table('test', function (Blueprint $table) { | ||
$table->dateTime('timestamp_to_datetime')->nullable(true)->change(); | ||
}); | ||
|
||
$this->assertTrue(Schema::hasColumn('test', 'timestamp_to_datetime')); | ||
// Postgres only has a timestamp type | ||
$this->driver === 'pgsql' | ||
? $this->assertSame('timestamp', Schema::getColumnType('test', 'timestamp_to_datetime')) | ||
: $this->assertSame('datetime', Schema::getColumnType('test', 'timestamp_to_datetime')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,4 +65,26 @@ public function testRegisterCustomDoctrineType() | |
$this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap()); | ||
$this->assertSame('tinyinteger', Schema::getColumnType('test', 'test_column')); | ||
} | ||
|
||
public function testRegisterCustomDoctrineTypeASecondTime() | ||
{ | ||
if ($this->driver !== 'sqlite') { | ||
$this->markTestSkipped('Test requires a SQLite connection.'); | ||
} | ||
|
||
Schema::registerCustomDoctrineType(TinyInteger::class, TinyInteger::NAME, 'TINYINT'); | ||
|
||
Schema::create('test', function (Blueprint $table) { | ||
$table->string('test_column'); | ||
}); | ||
|
||
$blueprint = new Blueprint('test', function (Blueprint $table) { | ||
$table->tinyInteger('test_column')->change(); | ||
}); | ||
|
||
$blueprint->build($this->getConnection(), new SQLiteGrammar); | ||
|
||
$this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap()); | ||
$this->assertSame('tinyinteger', Schema::getColumnType('test', 'test_column')); | ||
} | ||
Comment on lines
+68
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is identical to the one above it, but prior to this PR it failed, because registering custom Doctrine types only worked once during test runs (the very first time). |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll submit a separate follow-up PR to remove this method in Laravel 9 since it's now redundant and users should just call
DB::registerDoctrineType
directly.