Failed Conditions
Pull Request — master (#675)
by Jonathan
06:31
created

MigrationTableUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
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 MigrationTableUpdater
16
{
17
    /** @var Connection */
18
    private $connection;
19
20
    /** @var AbstractSchemaManager */
21
    private $schemaManager;
22
23
    /** @var MigrationTable */
24
    private $migrationTable;
25
26
    /** @var AbstractPlatform */
27
    private $platform;
28
29 68
    public function __construct(
30
        Connection $connection,
31
        AbstractSchemaManager $schemaManager,
32
        MigrationTable $migrationTable,
33
        AbstractPlatform $platform
34
    ) {
35 68
        $this->connection     = $connection;
36 68
        $this->schemaManager  = $schemaManager;
37 68
        $this->migrationTable = $migrationTable;
38 68
        $this->platform       = $platform;
39 68
    }
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 3
        $columnName  = $this->migrationTable->getColumnName();
79
80 3
        $currentTable = $this->schemaManager->listTableDetails($tableName);
81
82 3
        $table = new Table($tableName, $currentTable->getColumns());
83 3
        $table->setPrimaryKey([$columnName]);
84
85
        // remove columns from the table definition that we don't care about
86
        // so we don't try to drop those columns
87 3
        foreach ($table->getColumns() as $column) {
88 3
            if (in_array($column->getName(), $columnNames, true)) {
89 3
                continue;
90
            }
91
92 1
            $table->dropColumn($column->getName());
93
        }
94
95 3
        return $table;
96
    }
97
}
98