| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Doctrine\DBAL\Platforms\SQLServer;
|
|
|
4 |
|
|
|
5 |
use Doctrine\DBAL\Platforms\SQLServerPlatform;
|
|
|
6 |
use Doctrine\DBAL\Schema\Comparator as BaseComparator;
|
|
|
7 |
use Doctrine\DBAL\Schema\Table;
|
|
|
8 |
use Doctrine\DBAL\Schema\TableDiff;
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Compares schemas in the context of SQL Server platform.
|
|
|
12 |
*
|
|
|
13 |
* @link https://docs.microsoft.com/en-us/sql/t-sql/statements/collations?view=sql-server-ver15
|
|
|
14 |
*/
|
|
|
15 |
class Comparator extends BaseComparator
|
|
|
16 |
{
|
|
|
17 |
private string $databaseCollation;
|
|
|
18 |
|
|
|
19 |
/** @internal The comparator can be only instantiated by a schema manager. */
|
|
|
20 |
public function __construct(SQLServerPlatform $platform, string $databaseCollation)
|
|
|
21 |
{
|
|
|
22 |
parent::__construct($platform);
|
|
|
23 |
|
|
|
24 |
$this->databaseCollation = $databaseCollation;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public function compareTables(Table $fromTable, Table $toTable): TableDiff
|
|
|
28 |
{
|
|
|
29 |
return parent::compareTables(
|
|
|
30 |
$this->normalizeColumns($fromTable),
|
|
|
31 |
$this->normalizeColumns($toTable),
|
|
|
32 |
);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* {@inheritDoc}
|
|
|
37 |
*/
|
|
|
38 |
public function diffTable(Table $fromTable, Table $toTable)
|
|
|
39 |
{
|
|
|
40 |
return parent::diffTable(
|
|
|
41 |
$this->normalizeColumns($fromTable),
|
|
|
42 |
$this->normalizeColumns($toTable),
|
|
|
43 |
);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
private function normalizeColumns(Table $table): Table
|
|
|
47 |
{
|
|
|
48 |
$table = clone $table;
|
|
|
49 |
|
|
|
50 |
foreach ($table->getColumns() as $column) {
|
|
|
51 |
$options = $column->getPlatformOptions();
|
|
|
52 |
|
|
|
53 |
if (! isset($options['collation']) || $options['collation'] !== $this->databaseCollation) {
|
|
|
54 |
continue;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
unset($options['collation']);
|
|
|
58 |
$column->setPlatformOptions($options);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return $table;
|
|
|
62 |
}
|
|
|
63 |
}
|