diff --git a/src/WpdbMigrator.php b/src/WpdbMigrator.php index 90ac5b52c20476d3b0f600e0f1aa2fdd5be56d8a..d35821f4cda842760a14c975ba08e5f478e88075 100644 --- a/src/WpdbMigrator.php +++ b/src/WpdbMigrator.php @@ -108,8 +108,21 @@ class WpdbMigrator implements Migrator { } $this->logger->info( 'DB update start' ); - $current_version = $this->get_current_version(); + try { + $this->do_migrate(); + } catch ( \Throwable $e ) { + // @phpstan-ignore-next-line + $error_msg = sprintf( 'Error while upgrading a database: %s', $this->wpdb->last_error ); + $this->logger->error( $error_msg ); + trigger_error( esc_html( $error_msg ), E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error + } + + $this->logger->info( 'DB update finished' ); + } + + private function do_migrate(): void { + $current_version = $this->get_current_version(); foreach ( $this->migrations_repository->get_migrations() as $migration ) { if ( $this->comparator->compare( @@ -117,21 +130,18 @@ class WpdbMigrator implements Migrator { $this->get_current_version() ) > 0 ) { + $this->logger->info( sprintf( 'DB update %s:%s', $current_version, $migration->get_version() ) ); - try { - $migration->get_migration()->up(); + + $success = $migration->get_migration()->up(); + + if ( $success ) { $this->logger->info( sprintf( 'DB update %s:%s -> ', $current_version, $migration->get_version() ) . 'OK' ); update_option( $this->option_name, (string) $migration->get_version(), true ); - } catch ( \Throwable $e ) { - // @phpstan-ignore-next-line - $error_msg = sprintf( 'Error while upgrading a database: %s', $this->wpdb->last_error ); - $this->logger->error( $error_msg ); - trigger_error( $error_msg, E_USER_WARNING ); // phpcs:ignore + } else { + throw new \RuntimeException(); } } } - - $this->logger->info( 'DB update finished' ); } - }