| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\API\Endpoints\Migrations; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Framework\Database\DB; |
| 7 |
use Give\Framework\Migrations\Contracts\BatchMigration; |
| 8 |
use Give\Framework\Migrations\Contracts\Migration; |
| 9 |
use Give\Framework\Migrations\Contracts\ReversibleMigration; |
| 10 |
use Give\Framework\Migrations\Controllers\BatchMigrationRunner; |
| 11 |
use Give\Framework\Migrations\MigrationsRegister; |
| 12 |
use Give\MigrationLog\MigrationLogFactory; |
| 13 |
use Give\MigrationLog\MigrationLogStatus; |
| 14 |
use WP_REST_Request; |
| 15 |
use WP_REST_Response; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class RunMigration |
| 19 |
* @package Give\API\Endpoints\Migrations |
| 20 |
* |
| 21 |
* @since 4.0.0 run batch migrations |
| 22 |
* @since 2.10.0 |
| 23 |
*/ |
| 24 |
class RunMigration extends Endpoint |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var MigrationsRegister |
| 28 |
*/ |
| 29 |
private $migrationRegister; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var MigrationLogFactory |
| 33 |
*/ |
| 34 |
private $migrationLogFactory; |
| 35 |
|
| 36 |
/** |
| 37 |
* RunMigration constructor. |
| 38 |
* |
| 39 |
* @param MigrationsRegister $migrationsRegister |
| 40 |
* @param MigrationLogFactory $migrationLogFactory |
| 41 |
*/ |
| 42 |
public function __construct( |
| 43 |
MigrationsRegister $migrationsRegister, |
| 44 |
MigrationLogFactory $migrationLogFactory |
| 45 |
) { |
| 46 |
$this->migrationRegister = $migrationsRegister; |
| 47 |
$this->migrationLogFactory = $migrationLogFactory; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @inheritDoc |
| 52 |
*/ |
| 53 |
public function registerRoute() |
| 54 |
{ |
| 55 |
register_rest_route( |
| 56 |
'give-api/v2', |
| 57 |
'migrations/run-migration', |
| 58 |
[ |
| 59 |
[ |
| 60 |
'methods' => 'POST', |
| 61 |
'callback' => [$this, 'runMigration'], |
| 62 |
'permission_callback' => [$this, 'permissionsCheck'], |
| 63 |
'args' => [ |
| 64 |
'id' => [ |
| 65 |
'type' => 'string', |
| 66 |
'required' => true, |
| 67 |
], |
| 68 |
], |
| 69 |
], |
| 70 |
'schema' => [$this, 'getSchema'], |
| 71 |
] |
| 72 |
); |
| 73 |
|
| 74 |
register_rest_route( |
| 75 |
'give-api/v2', |
| 76 |
'migrations/run-batch-migration', |
| 77 |
[ |
| 78 |
[ |
| 79 |
'methods' => 'POST', |
| 80 |
'callback' => [$this, 'runBatchMigration'], |
| 81 |
'permission_callback' => [$this, 'permissionsCheck'], |
| 82 |
'args' => [ |
| 83 |
'id' => [ |
| 84 |
'type' => 'string', |
| 85 |
'required' => true, |
| 86 |
], |
| 87 |
], |
| 88 |
], |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
register_rest_route( |
| 93 |
'give-api/v2', |
| 94 |
'migrations/reschedule-failed-actions', |
| 95 |
[ |
| 96 |
[ |
| 97 |
'methods' => 'POST', |
| 98 |
'callback' => [$this, 'rescheduleFailedActions'], |
| 99 |
'permission_callback' => [$this, 'permissionsCheck'], |
| 100 |
'args' => [ |
| 101 |
'id' => [ |
| 102 |
'type' => 'string', |
| 103 |
'required' => true, |
| 104 |
], |
| 105 |
], |
| 106 |
], |
| 107 |
] |
| 108 |
); |
| 109 |
|
| 110 |
/** |
| 111 |
* @since 4.3.0 |
| 112 |
*/ |
| 113 |
register_rest_route( |
| 114 |
'give-api/v2', |
| 115 |
'migrations/rollback-migration', |
| 116 |
[ |
| 117 |
[ |
| 118 |
'methods' => 'POST', |
| 119 |
'callback' => [$this, 'rollbackMigration'], |
| 120 |
'permission_callback' => [$this, 'permissionsCheck'], |
| 121 |
'args' => [ |
| 122 |
'id' => [ |
| 123 |
'type' => 'string', |
| 124 |
'required' => true, |
| 125 |
'validate_callback' => function ($param) { |
| 126 |
$migrationClass = $this->migrationRegister->getMigration($param); |
| 127 |
|
| 128 |
return is_subclass_of($migrationClass, ReversibleMigration::class); |
| 129 |
}, |
| 130 |
], |
| 131 |
], |
| 132 |
], |
| 133 |
'schema' => [$this, 'getSchema'], |
| 134 |
] |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function getSchema() |
| 142 |
{ |
| 143 |
return [ |
| 144 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 145 |
'title' => 'logs', |
| 146 |
'type' => 'object', |
| 147 |
'properties' => [ |
| 148 |
'id' => [ |
| 149 |
'type' => 'string', |
| 150 |
'description' => esc_html__('Migration ID', 'give'), |
| 151 |
], |
| 152 |
], |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @param WP_REST_Request $request |
| 158 |
* |
| 159 |
* @return WP_REST_Response |
| 160 |
*/ |
| 161 |
public function runMigration(WP_REST_Request $request): WP_REST_Response |
| 162 |
{ |
| 163 |
$migrationId = $request->get_param('id'); |
| 164 |
$migrationLog = $this->migrationLogFactory->make($migrationId); |
| 165 |
|
| 166 |
// Begin transaction |
| 167 |
DB::beginTransaction(); |
| 168 |
|
| 169 |
try { |
| 170 |
$migrationClass = $this->migrationRegister->getMigration($migrationId); |
| 171 |
/** |
| 172 |
* @var Migration $migration |
| 173 |
*/ |
| 174 |
$migration = give($migrationClass); |
| 175 |
$migration->run(); |
| 176 |
// Save migration status |
| 177 |
$migrationLog |
| 178 |
->setStatus(MigrationLogStatus::SUCCESS) |
| 179 |
->setError(null) |
| 180 |
->save(); |
| 181 |
|
| 182 |
DB::commit(); |
| 183 |
|
| 184 |
return new WP_REST_Response(['status' => true]); |
| 185 |
} catch (Exception $exception) { |
| 186 |
DB::rollback(); |
| 187 |
|
| 188 |
$migrationLog |
| 189 |
->setStatus(MigrationLogStatus::FAILED) |
| 190 |
->setError([ |
| 191 |
'status' => __('Migration failed', 'give'), |
| 192 |
'error' => [ |
| 193 |
'message' => $exception->getMessage(), |
| 194 |
'code' => $exception->getCode(), |
| 195 |
'file' => $exception->getFile(), |
| 196 |
'line' => $exception->getLine(), |
| 197 |
], |
| 198 |
]) |
| 199 |
->save(); |
| 200 |
} |
| 201 |
|
| 202 |
return new WP_REST_Response( |
| 203 |
[ |
| 204 |
'status' => false, |
| 205 |
'message' => $exception->getMessage(), |
| 206 |
] |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Run batch migration |
| 213 |
* |
| 214 |
* @since 4.0.0 |
| 215 |
*/ |
| 216 |
public function runBatchMigration(WP_REST_Request $request): WP_REST_Response |
| 217 |
{ |
| 218 |
$migrationId = $request->get_param('id'); |
| 219 |
$migrationClass = $this->migrationRegister->getMigration($migrationId); |
| 220 |
|
| 221 |
if ( ! is_subclass_of($migrationClass, BatchMigration::class)) { |
| 222 |
return new WP_REST_Response([ |
| 223 |
'status' => false, |
| 224 |
'message' => 'Migration is not an instance of ' . BatchMigration::class, |
| 225 |
]); |
| 226 |
} |
| 227 |
|
| 228 |
try { |
| 229 |
// We are not running migration directly, |
| 230 |
// we just have to set migration status to PENDING and Migration Runner will handle it |
| 231 |
$migrationLog = $this->migrationLogFactory->make($migrationId); |
| 232 |
$migrationLog->setStatus(MigrationLogStatus::PENDING); |
| 233 |
$migrationLog->save(); |
| 234 |
} catch (Exception $e) { |
| 235 |
return new WP_REST_Response([ |
| 236 |
'status' => false, |
| 237 |
'message' => $e->getMessage(), |
| 238 |
]); |
| 239 |
} |
| 240 |
|
| 241 |
return new WP_REST_Response(['status' => true]); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Reschedule failed actions |
| 246 |
* |
| 247 |
* @since 4.0.0 |
| 248 |
*/ |
| 249 |
public function rescheduleFailedActions(WP_REST_Request $request): WP_REST_Response |
| 250 |
{ |
| 251 |
$migrationId = $request->get_param('id'); |
| 252 |
$migrationClass = $this->migrationRegister->getMigration($migrationId); |
| 253 |
$migration = give($migrationClass); |
| 254 |
|
| 255 |
if ( ! is_subclass_of($migration, BatchMigration::class)) { |
| 256 |
return new WP_REST_Response([ |
| 257 |
'status' => false, |
| 258 |
'message' => 'Migration is not an instance of ' . BatchMigration::class, |
| 259 |
]); |
| 260 |
} |
| 261 |
|
| 262 |
try { |
| 263 |
(new BatchMigrationRunner($migration))->rescheduleFailedActions(); |
| 264 |
} catch (Exception $e) { |
| 265 |
return new WP_REST_Response([ |
| 266 |
'status' => false, |
| 267 |
'message' => $e->getMessage(), |
| 268 |
]); |
| 269 |
} |
| 270 |
|
| 271 |
return new WP_REST_Response(['status' => true]); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
/** |
| 276 |
* @since 4.3.0 |
| 277 |
*/ |
| 278 |
public function rollbackMigration(WP_REST_Request $request): WP_REST_Response |
| 279 |
{ |
| 280 |
$migrationId = $request->get_param('id'); |
| 281 |
$migrationClass = $this->migrationRegister->getMigration($migrationId); |
| 282 |
$migration = give($migrationClass); |
| 283 |
$migrationLog = $this->migrationLogFactory->make($migrationId); |
| 284 |
|
| 285 |
if ($migration instanceof ReversibleMigration) { |
| 286 |
try { |
| 287 |
$migration->reverse(); |
| 288 |
$migrationLog->setStatus(MigrationLogStatus::REVERSED); |
| 289 |
} catch (Exception $e) { |
| 290 |
$migrationLog |
| 291 |
->setStatus(MigrationLogStatus::FAILED) |
| 292 |
->setError([ |
| 293 |
'status' => __('Rollback failed', 'give'), |
| 294 |
'error' => [ |
| 295 |
'message' => $e->getMessage(), |
| 296 |
'code' => $e->getCode(), |
| 297 |
'file' => $e->getFile(), |
| 298 |
'line' => $e->getLine(), |
| 299 |
], |
| 300 |
]); |
| 301 |
|
| 302 |
return new WP_REST_Response([ |
| 303 |
'status' => false, |
| 304 |
'message' => $e->getMessage(), |
| 305 |
]); |
| 306 |
} |
| 307 |
|
| 308 |
$migrationLog->save(); |
| 309 |
|
| 310 |
return new WP_REST_Response(['status' => true]); |
| 311 |
} |
| 312 |
|
| 313 |
return new WP_REST_Response(['status' => false]); |
| 314 |
} |
| 315 |
|
| 316 |
} |
| 317 |
|