| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\Migrations; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Framework\Database\DB; |
| 7 |
use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 8 |
use Give\Framework\Migrations\Contracts\BatchMigration; |
| 9 |
use Give\Framework\Migrations\Contracts\Migration; |
| 10 |
use Give\Framework\Migrations\Controllers\BatchMigrationRunner; |
| 11 |
use Give\Log\Log; |
| 12 |
use Give\MigrationLog\MigrationLogFactory; |
| 13 |
use Give\MigrationLog\MigrationLogRepository; |
| 14 |
use Give\MigrationLog\MigrationLogStatus; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class MigrationsRunner |
| 18 |
* |
| 19 |
* @since 2.9.0 |
| 20 |
*/ |
| 21 |
class MigrationsRunner |
| 22 |
{ |
| 23 |
/** |
| 24 |
* List of completed migrations. |
| 25 |
* |
| 26 |
* @since 2.9.0 |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $completedMigrations; |
| 31 |
|
| 32 |
/** |
| 33 |
* @since 2.9.0 |
| 34 |
* |
| 35 |
* @var MigrationsRegister |
| 36 |
*/ |
| 37 |
private $migrationRegister; |
| 38 |
|
| 39 |
/** |
| 40 |
* @since 2.10.0 |
| 41 |
* |
| 42 |
* @var MigrationLogFactory |
| 43 |
*/ |
| 44 |
private $migrationLogFactory; |
| 45 |
|
| 46 |
/** |
| 47 |
* @since 2.10.0 |
| 48 |
* @var MigrationLogRepository |
| 49 |
*/ |
| 50 |
private $migrationLogRepository; |
| 51 |
|
| 52 |
/** |
| 53 |
* MigrationsRunner constructor. |
| 54 |
* |
| 55 |
* @param MigrationsRegister $migrationRegister |
| 56 |
* @param MigrationLogFactory $migrationLogFactory |
| 57 |
* @param MigrationLogRepository $migrationLogRepository |
| 58 |
*/ |
| 59 |
public function __construct( |
| 60 |
MigrationsRegister $migrationRegister, |
| 61 |
MigrationLogFactory $migrationLogFactory, |
| 62 |
MigrationLogRepository $migrationLogRepository |
| 63 |
) { |
| 64 |
$this->migrationRegister = $migrationRegister; |
| 65 |
$this->migrationLogFactory = $migrationLogFactory; |
| 66 |
$this->migrationLogRepository = $migrationLogRepository; |
| 67 |
$this->completedMigrations = $this->migrationLogRepository->getCompletedMigrationsIDs(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Run database migrations. |
| 72 |
* |
| 73 |
* @since 4.0.0 add support for batch processing |
| 74 |
* @since 2.9.0 |
| 75 |
*/ |
| 76 |
public function run() |
| 77 |
{ |
| 78 |
if ( ! $this->hasMigrationToRun()) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Stop Migration Runner if there are failed migrations |
| 83 |
if ($this->migrationLogRepository->getFailedMigrationsCountByIds( |
| 84 |
$this->migrationRegister->getRegisteredIds() |
| 85 |
)) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$migrations = $this->migrationRegister->getMigrations(); |
| 90 |
|
| 91 |
foreach ($migrations as $migrationClass) { |
| 92 |
$migrationId = $migrationClass::id(); |
| 93 |
|
| 94 |
if (in_array($migrationId, $this->completedMigrations, true)) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
$migrationLog = $this->migrationLogFactory->make($migrationId); |
| 99 |
|
| 100 |
try { |
| 101 |
/** |
| 102 |
* @var Migration|BatchMigration $migration |
| 103 |
*/ |
| 104 |
$migration = give($migrationClass); |
| 105 |
|
| 106 |
if ($migration instanceof BatchMigration) { |
| 107 |
$status = (new BatchMigrationRunner($migration))->run(); |
| 108 |
|
| 109 |
if ($status === MigrationLogStatus::RUNNING) { |
| 110 |
give()->notices->register_notice( |
| 111 |
[ |
| 112 |
'id' => $migrationId, |
| 113 |
'description' => esc_html__('GiveWP is running database updates in the background. You will be notified as soon as it completes.', |
| 114 |
'give'), |
| 115 |
] |
| 116 |
); |
| 117 |
|
| 118 |
// Update status to RUNNING |
| 119 |
if (MigrationLogStatus::RUNNING !== $migrationLog->getStatus()) { |
| 120 |
$migrationLog->setStatus(MigrationLogStatus::RUNNING); |
| 121 |
$migrationLog->save(); |
| 122 |
} |
| 123 |
|
| 124 |
break; |
| 125 |
} |
| 126 |
|
| 127 |
if ($status === MigrationLogStatus::INCOMPLETE) { |
| 128 |
$listTableLink = sprintf( |
| 129 |
'<a href="%s">%s</a>', |
| 130 |
admin_url('edit.php?post_type=give_forms&page=give-tools&tab=data'), |
| 131 |
esc_html__('Resume update', 'give') |
| 132 |
); |
| 133 |
|
| 134 |
give()->notices->register_notice( |
| 135 |
[ |
| 136 |
'id' => $migrationId, |
| 137 |
'type' => 'warning', |
| 138 |
'description' => sprintf( |
| 139 |
__('Incomplete database update: "%s". %s', 'give'), |
| 140 |
$migration::title(), |
| 141 |
$listTableLink |
| 142 |
), |
| 143 |
] |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
$migrationLog->setStatus($status); |
| 148 |
} else { |
| 149 |
$migration->run(); |
| 150 |
$migrationLog->setStatus(MigrationLogStatus::SUCCESS); |
| 151 |
} |
| 152 |
} catch (Exception $exception) { |
| 153 |
DB::rollback(); |
| 154 |
$migrationLog |
| 155 |
->setStatus(MigrationLogStatus::FAILED) |
| 156 |
->setError([ |
| 157 |
'status' => __('Migration failed', 'give'), |
| 158 |
'error' => [ |
| 159 |
'message' => $exception->getMessage(), |
| 160 |
'code' => $exception->getCode(), |
| 161 |
'file' => $exception->getFile(), |
| 162 |
'line' => $exception->getLine(), |
| 163 |
], |
| 164 |
]); |
| 165 |
|
| 166 |
give()->notices->register_notice( |
| 167 |
[ |
| 168 |
'id' => 'migration-failure', |
| 169 |
'description' => sprintf( |
| 170 |
'%1$s <a href="https://givewp.com/support/">https://givewp.com/support</a>', |
| 171 |
esc_html__( |
| 172 |
'There was a problem running the migrations. Please reach out to GiveWP support for assistance:', |
| 173 |
'give' |
| 174 |
) |
| 175 |
), |
| 176 |
] |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
try { |
| 181 |
$migrationLog->save(); |
| 182 |
} catch (DatabaseQueryException $e) { |
| 183 |
Log::error( |
| 184 |
'Failed to save migration log', |
| 185 |
[ |
| 186 |
'Error Message' => $e->getMessage(), |
| 187 |
'Query Errors' => $e->getQueryErrors(), |
| 188 |
] |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
// Stop Migration Runner if migration has failed |
| 193 |
if ($migrationLog->getStatus() === MigrationLogStatus::FAILED) { |
| 194 |
break; |
| 195 |
} |
| 196 |
|
| 197 |
// Commit transaction if successful |
| 198 |
DB::commit(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Return whether or not all migrations completed. |
| 204 |
* |
| 205 |
* @since 2.9.0 |
| 206 |
* |
| 207 |
* @return bool |
| 208 |
*/ |
| 209 |
public function hasMigrationToRun() |
| 210 |
{ |
| 211 |
return (bool)array_diff($this->migrationRegister->getRegisteredIds(), $this->completedMigrations); |
| 212 |
} |
| 213 |
} |
| 214 |
|