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