| @@ 117-147 (lines=31) @@ | ||
| 114 | * |
|
| 115 | * @return MigrationInterface|null |
|
| 116 | */ |
|
| 117 | public function run(CapsuleInterface $capsule = null) |
|
| 118 | { |
|
| 119 | $capsule = $capsule ?? new MigrationCapsule($this->dbal); |
|
| 120 | ||
| 121 | /** |
|
| 122 | * @var MigrationInterface $migration |
|
| 123 | */ |
|
| 124 | foreach ($this->getMigrations() as $migration) { |
|
| 125 | if ($migration->getState()->getStatus() != State::STATUS_PENDING) { |
|
| 126 | continue; |
|
| 127 | } |
|
| 128 | ||
| 129 | //Isolate migration commands in a capsule |
|
| 130 | $migration = $migration->withCapsule($capsule); |
|
| 131 | ||
| 132 | //Executing migration inside global transaction |
|
| 133 | $this->execute(function () use ($migration) { |
|
| 134 | $migration->up(); |
|
| 135 | }); |
|
| 136 | ||
| 137 | //Registering record in database |
|
| 138 | $this->stateTable()->insert([ |
|
| 139 | 'migration' => $migration->getState()->getName(), |
|
| 140 | 'time_executed' => new \DateTime('now') |
|
| 141 | ]); |
|
| 142 | ||
| 143 | return $migration; |
|
| 144 | } |
|
| 145 | ||
| 146 | return null; |
|
| 147 | } |
|
| 148 | ||
| 149 | /** |
|
| 150 | * Rollback last migration and return it's instance. |
|
| @@ 156-185 (lines=30) @@ | ||
| 153 | * |
|
| 154 | * @return MigrationInterface|null |
|
| 155 | */ |
|
| 156 | public function rollback(CapsuleInterface $capsule = null) |
|
| 157 | { |
|
| 158 | $capsule = $capsule ?? new MigrationCapsule($this->dbal); |
|
| 159 | ||
| 160 | /** |
|
| 161 | * @var MigrationInterface $migration |
|
| 162 | */ |
|
| 163 | foreach (array_reverse($this->getMigrations()) as $migration) { |
|
| 164 | if ($migration->getState()->getStatus() != State::STATUS_EXECUTED) { |
|
| 165 | continue; |
|
| 166 | } |
|
| 167 | ||
| 168 | //Isolate migration commands in a capsule |
|
| 169 | $migration = $migration->withCapsule($capsule); |
|
| 170 | ||
| 171 | //Executing migration inside global transaction |
|
| 172 | $this->execute(function () use ($migration) { |
|
| 173 | $migration->down(); |
|
| 174 | }); |
|
| 175 | ||
| 176 | //Flushing DB record |
|
| 177 | $this->stateTable()->delete([ |
|
| 178 | 'migration' => $migration->getState()->getName() |
|
| 179 | ])->run(); |
|
| 180 | ||
| 181 | return $migration; |
|
| 182 | } |
|
| 183 | ||
| 184 | return null; |
|
| 185 | } |
|
| 186 | ||
| 187 | /** |
|
| 188 | * Migration table, all migration information will be stored in it. |
|