Handle edge case of order table being unexpectedly not present.

This commit is contained in:
Vedanshu Jain 2023-07-13 16:29:58 +05:30
parent 406f7b6174
commit bde6fbf01e
4 changed files with 27 additions and 9 deletions

View File

@ -248,9 +248,9 @@ class CustomOrdersTableController {
return $tools_array;
}
if ( $this->is_feature_visible() ) {
if ( $this->custom_orders_table_usage_is_enabled() || $this->data_synchronizer->data_sync_is_enabled() ) {
$disabled = true;
$message = __( 'This will delete the custom orders tables. The tables can be deleted only if the "High-Performance order storage" feature is disabled (via Settings > Advanced > Features).', 'woocommerce' );
$message = __( 'This will delete the custom orders tables. The tables can be deleted only if the "High-Performance order storage" is not authoritative and sync is disabled (via Settings > Advanced > Features).', 'woocommerce' );
} else {
$disabled = false;
$message = __( 'This will delete the custom orders tables. To create them again enable the "High-Performance order storage" feature (via Settings > Advanced > Features).', 'woocommerce' );

View File

@ -197,9 +197,13 @@ class DataSynchronizer implements BatchProcessorInterface {
return (int) $pending_count;
}
}
$orders_table = $this->data_store::get_orders_table_name();
$order_post_types = wc_get_order_types( 'cot-migration' );
$order_post_type_placeholder = implode( ', ', array_fill( 0, count( $order_post_types ), '%s' ) );
$orders_table = $this->data_store::get_orders_table_name();
if ( empty( $order_post_types ) ) {
$this->error_logger->debug(
sprintf(
@ -212,7 +216,17 @@ class DataSynchronizer implements BatchProcessorInterface {
return 0;
}
$order_post_type_placeholder = implode( ', ', array_fill( 0, count( $order_post_types ), '%s' ) );
if ( 'yes' !== get_option( self::ORDERS_TABLE_CREATED ) ) {
$count = $wpdb->get_var(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholder is prepared.
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->posts where post_type in ( $order_post_type_placeholder )",
$order_post_types
)
// phpcs:enable
);
return $count;
}
if ( $this->custom_orders_table_is_authoritative() ) {
$missing_orders_count_sql = "

View File

@ -114,9 +114,9 @@ class FeaturesController {
'disable_ui' => false,
),
// Options for HPOS features are added in CustomOrdersTableController to keep the logic in same place.
'custom_order_tables' => array( // This exists for back-compat, otherwise is always enabled and hidden from WC 8.0.
'custom_order_tables' => array( // This exists for back-compat only, otherwise it's value is superseded by $hpos_authoritative option.
'name' => __( 'High-Performance order storage (COT)', 'woocommerce' ),
'enabled_by_default' => true,
'enabled_by_default' => false,
),
$hpos_authoritative => array(
'name' => __( 'High performance order storage', 'woocommerce' ),
@ -232,7 +232,8 @@ class FeaturesController {
}
$default_value = $this->feature_is_enabled_by_default( $feature_id ) ? 'yes' : 'no';
return 'yes' === get_option( $this->feature_enable_option_name( $feature_id ), $default_value );
$value = 'yes' === get_option( $this->feature_enable_option_name( $feature_id ), $default_value );
return $value;
}
/**

View File

@ -33,7 +33,10 @@ class DatabaseUtil {
* @return array An array containing the names of the tables that currently don't exist in the database.
*/
public function get_missing_tables( string $creation_queries ): array {
global $wpdb;
$suppress_errors = $wpdb->suppress_errors( true );
$dbdelta_output = $this->dbdelta( $creation_queries, false );
$wpdb->suppress_errors( $suppress_errors );
$parsed_output = $this->parse_dbdelta_output( $dbdelta_output );
return $parsed_output['created_tables'];
}