1 | <?php |
||
32 | class Migration |
||
33 | { |
||
34 | /** |
||
35 | * The OutputWriter object instance used for outputting information |
||
36 | * |
||
37 | * @var OutputWriter |
||
38 | */ |
||
39 | private $outputWriter; |
||
40 | |||
41 | /** |
||
42 | * @var Configuration |
||
43 | */ |
||
44 | private $configuration; |
||
45 | |||
46 | /** |
||
47 | * @var boolean |
||
48 | */ |
||
49 | private $noMigrationException; |
||
50 | |||
51 | /** |
||
52 | * Construct a Migration instance |
||
53 | * |
||
54 | * @param Configuration $configuration A migration Configuration instance |
||
55 | */ |
||
56 | 34 | public function __construct(Configuration $configuration) |
|
62 | |||
63 | /** |
||
64 | * Get the array of versions and SQL queries that would be executed for |
||
65 | * each version but do not execute anything. |
||
66 | * |
||
67 | * @param string $to The version to migrate to. |
||
68 | * |
||
69 | * @return array $sql The array of SQL queries. |
||
70 | */ |
||
71 | 2 | public function getSql($to = null) |
|
75 | |||
76 | /** |
||
77 | * Write a migration SQL file to the given path |
||
78 | * |
||
79 | * @param string $path The path to write the migration SQL file. |
||
80 | * @param string $to The version to migrate to. |
||
81 | * |
||
82 | * @return boolean $written |
||
83 | */ |
||
84 | 6 | public function writeSqlFile($path, $to = null) |
|
85 | { |
||
86 | 6 | $sql = $this->getSql($to); |
|
87 | |||
88 | 6 | $from = $this->configuration->getCurrentVersion(); |
|
89 | 6 | if ($to === null) { |
|
90 | 1 | $to = $this->configuration->getLatestVersion(); |
|
91 | 1 | } |
|
92 | |||
93 | 6 | $direction = $from > $to ? Version::DIRECTION_DOWN : Version::DIRECTION_UP; |
|
94 | |||
95 | 6 | $this->outputWriter->write(sprintf("-- Migrating from %s to %s\n", $from, $to)); |
|
96 | |||
97 | 6 | $sqlWriter = new SqlFileWriter( |
|
98 | 6 | $this->configuration->getMigrationsColumnName(), |
|
99 | 6 | $this->configuration->getMigrationsTableName(), |
|
100 | 6 | $path, |
|
101 | 6 | $this->outputWriter |
|
102 | 6 | ); |
|
103 | |||
104 | 6 | return $sqlWriter->write($sql, $direction); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param boolean $noMigrationException Throw an exception or not if no migration is found. Mostly for Continuous Integration. |
||
109 | */ |
||
110 | 2 | public function setNoMigrationException($noMigrationException = false) |
|
114 | |||
115 | /** |
||
116 | * Run a migration to the current version or the given target version. |
||
117 | * |
||
118 | * @param string $to The version to migrate to. |
||
119 | * @param boolean $dryRun Whether or not to make this a dry run and not execute anything. |
||
120 | * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
||
121 | * @param callable|null $confirm A callback to confirm whether the migrations should be executed. |
||
122 | * |
||
123 | * @return array An array of migration sql statements. This will be empty if the the $confirm callback declines to execute the migration |
||
124 | * |
||
125 | * @throws MigrationException |
||
126 | */ |
||
127 | 28 | public function migrate($to = null, $dryRun = false, $timeAllQueries = false, callable $confirm = null) |
|
195 | |||
196 | 3 | private function noMigrations() |
|
201 | |||
202 | 24 | private function migrationsCanExecute(callable $confirm=null) |
|
206 | } |
||
207 |