Passed
Push — master ( bad8f2...753de8 )
by Jonathan
02:33
created

Migration::getSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use Doctrine\Migrations\Configuration\Configuration;
8
use Doctrine\Migrations\Exception\MigrationException;
9
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
10
use Doctrine\Migrations\Exception\UnknownMigrationVersion;
11
use const COUNT_RECURSIVE;
12
use function count;
13
use function sprintf;
14
15
class Migration
16
{
17
    /** @var Configuration */
18
    private $configuration;
19
20
    /** @var MigrationRepository */
21
    private $migrationRepository;
22
23
    /** @var OutputWriter */
24
    private $outputWriter;
25
26
    /** @var bool */
27
    private $noMigrationException = false;
28
29 37
    public function __construct(
30
        Configuration $configuration,
31
        MigrationRepository $migrationRepository,
32
        OutputWriter $outputWriter
33
    ) {
34 37
        $this->configuration       = $configuration;
35 37
        $this->migrationRepository = $migrationRepository;
36 37
        $this->outputWriter        = $outputWriter;
37 37
    }
38
39 2
    public function setNoMigrationException(bool $noMigrationException = false) : void
40
    {
41 2
        $this->noMigrationException = $noMigrationException;
42 2
    }
43
44
    /** @return string[][] */
45 2
    public function getSql(?string $to = null) : array
46
    {
47 2
        return $this->migrate($to, true);
48
    }
49
50 6
    public function writeSqlFile(string $path, ?string $to = null) : bool
51
    {
52 6
        $sql = $this->getSql($to);
53
54 6
        $from = $this->migrationRepository->getCurrentVersion();
55
56 6
        if ($to === null) {
57 1
            $to = $this->migrationRepository->getLatestVersion();
58
        }
59
60 6
        $direction = $from > $to
61 1
            ? Version::DIRECTION_DOWN
62 6
            : Version::DIRECTION_UP;
63
64 6
        $this->outputWriter->write(
65 6
            sprintf("-- Migrating from %s to %s\n", $from, $to)
66
        );
67
68
        /**
69
         * Since the configuration object changes during the creation we cannot inject things
70
         * properly, so I had to violate LoD here (so please, let's find a way to solve it on v2).
71
         */
72 6
        return $this->configuration
73 6
            ->getQueryWriter()
74 6
            ->write($path, $direction, $sql);
75
    }
76
77
    /**
78
     * @throws MigrationException
79
     *
80
     * @return string[][]
81
     */
82 31
    public function migrate(
83
        ?string $to = null,
84
        bool $dryRun = false,
85
        bool $timeAllQueries = false,
86
        ?callable $confirm = null
87
    ) : array {
88 31
        if ($to === null) {
89 14
            $to = $this->migrationRepository->getLatestVersion();
90
        }
91
92 31
        $from = $this->migrationRepository->getCurrentVersion();
93
94 31
        $versions = $this->migrationRepository->getMigrations();
95
96 31
        if (! isset($versions[$to]) && $to > 0) {
97 1
            throw UnknownMigrationVersion::new($to);
98
        }
99
100 30
        $direction = $from > $to
101 3
            ? Version::DIRECTION_DOWN
102 30
            : Version::DIRECTION_UP;
103
104 30
        $migrationsToExecute = $this->configuration
105 30
            ->getMigrationsToExecute($direction, $to);
106
107
        /**
108
         * If
109
         *  there are no migrations to execute
110
         *  and there are migrations,
111
         *  and the migration from and to are the same
112
         * means we are already at the destination return an empty array()
113
         * to signify that there is nothing left to do.
114
         */
115 30
        if ($from === $to && count($migrationsToExecute) === 0 && count($versions) !== 0) {
116 2
            return $this->noMigrations();
117
        }
118
119 29
        if (! $dryRun && $this->migrationsCanExecute($confirm) === false) {
120 1
            return [];
121
        }
122
123 28
        $output  = $dryRun ? 'Executing dry run of migration' : 'Migrating';
124 28
        $output .= ' <info>%s</info> to <comment>%s</comment> from <comment>%s</comment>';
125
126 28
        $this->outputWriter->write(sprintf($output, $direction, $to, $from));
127
128
        /**
129
         * If there are no migrations to execute throw an exception.
130
         */
131 28
        if (count($migrationsToExecute) === 0 && ! $this->noMigrationException) {
132 1
            throw NoMigrationsToExecute::new();
133 27
        } elseif (count($migrationsToExecute) === 0) {
134 1
            return $this->noMigrations();
135
        }
136
137 26
        $this->configuration->dispatchMigrationEvent(Events::onMigrationsMigrating, $direction, $dryRun);
138
139 26
        $sql  = [];
140 26
        $time = 0;
141
142 26
        foreach ($migrationsToExecute as $version) {
143 26
            $versionExecutionResult = $version->execute($direction, $dryRun, $timeAllQueries);
144
145 26
            $sql[$version->getVersion()] = $versionExecutionResult->getSql();
146 26
            $time                       += $versionExecutionResult->getTime();
147
        }
148
149 26
        $this->configuration->dispatchMigrationEvent(Events::onMigrationsMigrated, $direction, $dryRun);
150
151 26
        $this->outputWriter->write("\n  <comment>------------------------</comment>\n");
152 26
        $this->outputWriter->write(sprintf('  <info>++</info> finished in %ss', $time));
153 26
        $this->outputWriter->write(sprintf('  <info>++</info> %s migrations executed', count($migrationsToExecute)));
154 26
        $this->outputWriter->write(sprintf('  <info>++</info> %s sql queries', count($sql, COUNT_RECURSIVE) - count($sql)));
155
156 26
        return $sql;
157
    }
158
159
    /** @return string[][] */
160 3
    private function noMigrations() : array
161
    {
162 3
        $this->outputWriter->write('<comment>No migrations to execute.</comment>');
163
164 3
        return [];
165
    }
166
167 27
    private function migrationsCanExecute(?callable $confirm = null) : bool
168
    {
169 27
        return $confirm === null ? true : (bool) $confirm();
170
    }
171
}
172