1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tracking; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
9
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
11
|
|
|
use Doctrine\DBAL\Schema\Table; |
12
|
|
|
use Throwable; |
13
|
|
|
use function in_array; |
14
|
|
|
|
15
|
|
|
class TableUpdater |
16
|
|
|
{ |
17
|
|
|
/** @var Connection */ |
18
|
|
|
private $connection; |
19
|
|
|
|
20
|
|
|
/** @var AbstractSchemaManager */ |
21
|
|
|
private $schemaManager; |
22
|
|
|
|
23
|
|
|
/** @var TableDefinition */ |
24
|
|
|
private $migrationTable; |
25
|
|
|
|
26
|
|
|
/** @var AbstractPlatform */ |
27
|
|
|
private $platform; |
28
|
|
|
|
29
|
78 |
|
public function __construct( |
30
|
|
|
Connection $connection, |
31
|
|
|
AbstractSchemaManager $schemaManager, |
32
|
|
|
TableDefinition $migrationTable, |
33
|
|
|
AbstractPlatform $platform |
34
|
|
|
) { |
35
|
78 |
|
$this->connection = $connection; |
36
|
78 |
|
$this->schemaManager = $schemaManager; |
37
|
78 |
|
$this->migrationTable = $migrationTable; |
38
|
78 |
|
$this->platform = $platform; |
39
|
78 |
|
} |
40
|
|
|
|
41
|
3 |
|
public function updateMigrationTable() : void |
42
|
|
|
{ |
43
|
3 |
|
$fromTable = $this->getFromTable(); |
44
|
3 |
|
$toTable = $this->migrationTable->getDBALTable(); |
45
|
|
|
|
46
|
3 |
|
$fromSchema = $this->createSchema([$fromTable]); |
47
|
3 |
|
$toSchema = $this->createSchema([$toTable]); |
48
|
|
|
|
49
|
3 |
|
$queries = $fromSchema->getMigrateToSql($toSchema, $this->platform); |
50
|
|
|
|
51
|
3 |
|
$this->connection->beginTransaction(); |
52
|
|
|
|
53
|
|
|
try { |
54
|
3 |
|
foreach ($queries as $query) { |
55
|
3 |
|
$this->connection->executeQuery($query); |
56
|
|
|
} |
57
|
1 |
|
} catch (Throwable $e) { |
58
|
1 |
|
$this->connection->rollBack(); |
59
|
|
|
|
60
|
1 |
|
throw $e; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$this->connection->commit(); |
64
|
2 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Table[] $tables |
68
|
|
|
*/ |
69
|
1 |
|
protected function createSchema(array $tables) : Schema |
70
|
|
|
{ |
71
|
1 |
|
return new Schema($tables); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
private function getFromTable() : Table |
75
|
|
|
{ |
76
|
3 |
|
$tableName = $this->migrationTable->getName(); |
77
|
3 |
|
$columnNames = $this->migrationTable->getColumnNames(); |
78
|
|
|
|
79
|
3 |
|
$currentTable = $this->schemaManager->listTableDetails($tableName); |
80
|
|
|
|
81
|
3 |
|
$table = $this->migrationTable->createDBALTable($currentTable->getColumns()); |
82
|
|
|
|
83
|
|
|
// remove columns from the table definition that we don't care about |
84
|
|
|
// so we don't try to drop those columns |
85
|
3 |
|
foreach ($table->getColumns() as $column) { |
86
|
3 |
|
if (in_array($column->getName(), $columnNames, true)) { |
87
|
3 |
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$table->dropColumn($column->getName()); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return $table; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|