1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional\Schema\MySQL;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnEditor;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\Functional\Schema\ComparatorTestUtils;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;
final class ComparatorTest extends FunctionalTestCase
{
private AbstractPlatform $platform;
private AbstractSchemaManager $schemaManager;
private Comparator $comparator;
protected function setUp(): void
{
$this->platform = $this->connection->getDatabasePlatform();
if (! $this->platform instanceof AbstractMySQLPlatform) {
self::markTestSkipped();
}
$this->schemaManager = $this->connection->createSchemaManager();
$this->comparator = $this->schemaManager->createComparator();
}
#[DataProvider('lobColumnProvider')]
public function testLobLengthIncrementWithinLimit(string $type, int $length): void
{
$table = $this->createLobTable($type, $length - 1);
$table = $this->setBlobLength($table, $length);
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
}
#[DataProvider('lobColumnProvider')]
public function testLobLengthIncrementOverLimit(string $type, int $length): void
{
$table = $this->createLobTable($type, $length);
$table = $this->setBlobLength($table, $length + 1);
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
/** @return iterable<array{string,int}> */
public static function lobColumnProvider(): iterable
{
yield [Types::BLOB, AbstractMySQLPlatform::LENGTH_LIMIT_TINYBLOB];
yield [Types::BLOB, AbstractMySQLPlatform::LENGTH_LIMIT_BLOB];
yield [Types::BLOB, AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMBLOB];
yield [Types::TEXT, AbstractMySQLPlatform::LENGTH_LIMIT_TINYTEXT];
yield [Types::TEXT, AbstractMySQLPlatform::LENGTH_LIMIT_TEXT];
yield [Types::TEXT, AbstractMySQLPlatform::LENGTH_LIMIT_MEDIUMTEXT];
}
/** @throws Exception */
private function createLobTable(string $typeName, int $length): Table
{
$table = Table::editor()
->setUnquotedName('comparator_test')
->setColumns(
Column::editor()
->setUnquotedName('lob')
->setTypeName($typeName)
->setLength($length)
->create(),
)
->create();
$this->dropAndCreateTable($table);
return $table;
}
/** @throws Exception */
private function setBlobLength(Table $table, int $length): Table
{
return $table->edit()
->modifyColumnByUnquotedName('lob', static function (ColumnEditor $editor) use ($length): void {
$editor->setLength($length);
})
->create();
}
public function testExplicitDefaultCollation(): void
{
$table = $this->createCollationTable()
->edit()
->modifyColumnByUnquotedName('id', static function (ColumnEditor $editor): void {
$editor->setCollation('utf8mb4_general_ci');
})
->create();
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
}
public function testChangeColumnCharsetAndCollation(): void
{
$table = $this->createCollationTable()
->edit()
->modifyColumnByUnquotedName('id', static function (ColumnEditor $editor): void {
$editor
->setCharset('latin1')
->setCollation('latin1_bin');
})
->create();
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
public function testChangeColumnCollation(): void
{
$table = $this->createCollationTable()
->edit()
->modifyColumnByUnquotedName('id', static function (ColumnEditor $editor): void {
$editor->setCollation('utf8mb4_bin');
})
->create();
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
/**
* @param array<string,string> $tableOptions
* @param ?non-empty-string $columnCharset
* @param ?non-empty-string $columnCollation
*/
#[DataProvider('tableAndColumnOptionsProvider')]
public function testTableAndColumnOptions(
array $tableOptions,
?string $columnCharset,
?string $columnCollation,
): void {
$table = Table::editor()
->setUnquotedName('comparator_test')
->setColumns(
Column::editor()
->setUnquotedName('name')
->setTypeName(Types::STRING)
->setLength(32)
->setCharset($columnCharset)
->setCollation($columnCollation)
->create(),
)
->setOptions($tableOptions)
->create();
$this->dropAndCreateTable($table);
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
}
public function testSimpleArrayTypeNonChangeNotDetected(): void
{
$table = Table::editor()
->setUnquotedName('comparator_test')
->setColumns(
Column::editor()
->setUnquotedName('simple_array_col')
->setTypeName(Types::SIMPLE_ARRAY)
->setLength(255)
->create(),
)
->create();
$this->dropAndCreateTable($table);
self::assertTrue(ComparatorTestUtils::diffFromActualToDesiredTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
self::assertTrue(ComparatorTestUtils::diffFromDesiredToActualTable(
$this->schemaManager,
$this->comparator,
$table,
)->isEmpty());
}
/** @return iterable<string,array{array<string,string>,?non-empty-string,?non-empty-string}> */
public static function tableAndColumnOptionsProvider(): iterable
{
yield "Column collation explicitly set to its table's default" => [
[],
null,
'utf8mb4_general_ci',
];
yield "Column charset implicitly set to a value matching its table's charset" => [
['charset' => 'utf8mb4'],
null,
'utf8mb4_general_ci',
];
yield "Column collation reset to the collation's default matching its table's charset" => [
['collation' => 'utf8mb4_unicode_ci'],
'utf8mb4',
null,
];
}
public function testMariaDb1043NativeJsonUpgradeDetected(): void
{
if (! $this->platform instanceof MariaDBPlatform && ! $this->platform instanceof MySQL80Platform) {
self::markTestSkipped();
}
$table = Table::editor()
->setUnquotedName('mariadb_json_upgrade')
->setColumns(
Column::editor()
->setUnquotedName('json_col')
->setTypeName(Types::JSON)
->create(),
)
->create();
$this->dropAndCreateTable($table);
// Revert column to old LONGTEXT declaration
$sql = 'ALTER TABLE mariadb_json_upgrade CHANGE json_col json_col LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\'';
$this->connection->executeStatement($sql);
ComparatorTestUtils::assertDiffNotEmpty($this->connection, $this->comparator, $table);
}
private function createCollationTable(): Table
{
$table = Table::editor()
->setUnquotedName('comparator_test')
->setColumns(
Column::editor()
->setUnquotedName('id')
->setTypeName(Types::STRING)
->setLength(32)
->create(),
)
->setOptions([
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
])
->create();
$this->dropAndCreateTable($table);
return $table;
}
}
|