suppress_errors( true ); try { foreach ( Schema::pricing( $wpdb ) as $table => $sql ) { if ( $table === \ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'bookings' ) ) { $columns = $wpdb->get_col( $wpdb->prepare( 'SHOW COLUMNS FROM %i', $table ) ); $this->check( $wpdb ); if ( ! in_array( 'pricing_snapshot', $columns, true ) ) { $wpdb->query( $wpdb->prepare( 'ALTER TABLE %i ADD pricing_snapshot longtext NULL AFTER admin_note', $table ) ); $this->check( $wpdb ); } } else { $exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ); $this->check( $wpdb ); if ( ! $exists ) { dbDelta( $sql ); $this->check( $wpdb ); } } Schema::verify( $wpdb, $table, $table === \ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'bookings' ) && in_array( 'external_contact_snapshot', $columns, true ) ? Schema::externalContacts( $wpdb )[ $table ] : ( $table === \ABeCe\Booking\Infrastructure\Database\Tables::name( $wpdb, 'bookings' ) && in_array( 'cancellation_policy_snapshot', $columns, true ) ? Schema::cancellation( $wpdb )[ $table ] : $sql ) ); } } finally { $wpdb->suppress_errors( $previous ); } } private function check( \wpdb $wpdb ): void { if ( $wpdb->last_error ) throw new RuntimeException( __( 'ABeCe Booking database migration failed. Existing data has been retained.', 'abece-booking' ) ); } }