Refactor and move DB related methods to util class.
This commit is contained in:
parent
2e613bbd0c
commit
3d29e8329c
|
@ -8,6 +8,7 @@
|
|||
|
||||
use Automattic\Jetpack\Constants;
|
||||
use Automattic\WooCommerce\Internal\WCCom\ConnectionHelper as WCConnectionHelper;
|
||||
use Automattic\WooCommerce\Utilities\DBUtil;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
|
@ -344,7 +345,7 @@ class WC_Install {
|
|||
self::create_tables();
|
||||
}
|
||||
|
||||
$missing_tables = self::verify_schema( self::get_schema() );
|
||||
$missing_tables = DBUtil::verify_database_tables_exist( self::get_schema() );
|
||||
|
||||
if ( 0 < count( $missing_tables ) ) {
|
||||
if ( $modify_notice ) {
|
||||
|
@ -361,27 +362,6 @@ class WC_Install {
|
|||
return $missing_tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the table(s) already exists.
|
||||
*
|
||||
* @param string $schema Schema definition to check against.
|
||||
*
|
||||
* return array List of missing tables.
|
||||
*/
|
||||
public static function verify_schema( $schema ) {
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
$missing_tables = array();
|
||||
$queries = dbDelta( $schema, false );
|
||||
|
||||
foreach ( $queries as $table_name => $result ) {
|
||||
if ( "Created table $table_name" === $result ) {
|
||||
$missing_tables[] = $table_name;
|
||||
}
|
||||
}
|
||||
return $missing_tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset any notices added to admin.
|
||||
*
|
||||
|
|
|
@ -35,7 +35,7 @@ class CustomOrdersTableController {
|
|||
*
|
||||
* @var DataSynchronizer
|
||||
*/
|
||||
private $data_synchronizer;
|
||||
public $data_synchronizer;
|
||||
|
||||
/**
|
||||
* Is the feature visible?
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
namespace Automattic\WooCommerce\Internal\DataStores\Orders;
|
||||
|
||||
use Automattic\Jetpack\Constants;
|
||||
use Automattic\WooCommerce\Utilities\DBUtil;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
|
@ -45,7 +45,7 @@ class DataSynchronizer {
|
|||
* @return bool True if the custom orders table exist in the database.
|
||||
*/
|
||||
public function check_orders_table_exists(): bool {
|
||||
$missing_tables = \WC_Install::verify_schema( $this->data_store->get_schema() );
|
||||
$missing_tables = DBUtil::verify_database_tables_exist( $this->data_store->get_schema() );
|
||||
return count( $missing_tables ) === 0;
|
||||
}
|
||||
|
||||
|
@ -62,10 +62,9 @@ class DataSynchronizer {
|
|||
* Initiate a table regeneration process.
|
||||
*/
|
||||
public function initiate_regeneration() {
|
||||
require_once( Constants::get_constant( 'ABSPATH' ) . 'wp-admin/includes/upgrade.php' );
|
||||
update_option( self::CUSTOM_ORDERS_TABLE_DATA_REGENERATION_IN_PROGRESS, 'yes' );
|
||||
update_option( self::CUSTOM_ORDERS_TABLE_DATA_REGENERATION_DONE_COUNT, 0 );
|
||||
dbDelta( $this->data_store->get_schema() );
|
||||
DBUtil::create_database_tables( $this->data_store->get_schema() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
/**
|
||||
* A class of utilities for dealing with Database management.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Utilities;
|
||||
|
||||
use Automattic\Jetpack\Constants;
|
||||
|
||||
class DBUtil {
|
||||
|
||||
/**
|
||||
* Verify if the table(s) already exists.
|
||||
*
|
||||
* @param string $table_creation_sql Schema definition to check against.
|
||||
*
|
||||
* return array List of missing tables.
|
||||
*/
|
||||
public static function verify_database_tables_exist( $table_creation_sql ) {
|
||||
require_once( Constants::get_constant( 'ABSPATH' ) . 'wp-admin/includes/upgrade.php' );
|
||||
|
||||
$missing_tables = array();
|
||||
$queries = dbDelta( $table_creation_sql, false );
|
||||
|
||||
foreach ( $queries as $table_name => $result ) {
|
||||
if ( "Created table $table_name" === $result ) {
|
||||
$missing_tables[] = $table_name;
|
||||
}
|
||||
}
|
||||
return $missing_tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create DB tables for passed schema. Currently a wrapper for dbDelta.
|
||||
*
|
||||
* @param string $table_creation_sql Table SQL
|
||||
*
|
||||
* @return array List of tables that we were not able to create.
|
||||
*/
|
||||
public static function create_database_tables( $table_creation_sql ) {
|
||||
require_once( Constants::get_constant( 'ABSPATH' ) . 'wp-admin/includes/upgrade.php' );
|
||||
dbDelta( $table_creation_sql );
|
||||
return self::verify_database_tables_exist( $table_creation_sql );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue