| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\Migrations\Actions; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\Framework\Migrations\Contracts\Migration; |
| 7 |
use Give\Framework\Migrations\MigrationsRunner; |
| 8 |
|
| 9 |
class ManuallyRunMigration |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Manually runs the migration and then marks the migration as finished if successful |
| 13 |
* |
| 14 |
* @since 2.9.2 |
| 15 |
* |
| 16 |
* @param Migration $migration |
| 17 |
* |
| 18 |
*/ |
| 19 |
public function __invoke(Migration $migration) |
| 20 |
{ |
| 21 |
global $wpdb; |
| 22 |
|
| 23 |
$wpdb->query('START TRANSACTION'); |
| 24 |
|
| 25 |
try { |
| 26 |
$migration->run(); |
| 27 |
} catch (Exception $exception) { |
| 28 |
$wpdb->query('ROLLBACK'); |
| 29 |
|
| 30 |
give_record_log('Migration Failed', print_r($exception, true), 0, 'update'); |
| 31 |
give()->notices->register_notice( |
| 32 |
[ |
| 33 |
'id' => 'migration-failure', |
| 34 |
'description' => sprintf( |
| 35 |
'%1$s <a href="https://givewp.com/support/">https://givewp.com/support</a>', |
| 36 |
esc_html__( |
| 37 |
'There was a problem running the migrations. Please reach out to GiveWP support for assistance:', |
| 38 |
'give' |
| 39 |
) |
| 40 |
), |
| 41 |
] |
| 42 |
); |
| 43 |
|
| 44 |
throw $exception; |
| 45 |
} |
| 46 |
|
| 47 |
// Commit transaction if successful |
| 48 |
$wpdb->query('COMMIT'); |
| 49 |
|
| 50 |
$this->updateMigrationsSetting($migration::id()); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Updates the completed migrations to include the migration if not yet included |
| 55 |
* |
| 56 |
* @since 2.9.2 |
| 57 |
* |
| 58 |
* @param string $migrationId |
| 59 |
*/ |
| 60 |
private function updateMigrationsSetting($migrationId) |
| 61 |
{ |
| 62 |
$completedMigrations = get_option(MigrationsRunner::MIGRATION_OPTION); |
| 63 |
|
| 64 |
if (in_array($migrationId, $completedMigrations, true)) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$completedMigrations[] = $migrationId; |
| 69 |
|
| 70 |
update_option(MigrationsRunner::MIGRATION_OPTION, $completedMigrations); |
| 71 |
} |
| 72 |
} |
| 73 |
|