Refactor and move DB related methods to util class.

This commit is contained in:
vedanshujain 2022-02-08 21:44:15 +05:30
parent 2e613bbd0c
commit 3d29e8329c
4 changed files with 52 additions and 27 deletions

View File

@ -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.
*

View File

@ -35,7 +35,7 @@ class CustomOrdersTableController {
*
* @var DataSynchronizer
*/
private $data_synchronizer;
public $data_synchronizer;
/**
* Is the feature visible?

View File

@ -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() );
}
/**

View File

@ -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 );
}
}