Merge pull request #21940 from woocommerce/fix/21937

Make wc_download_log FK use table prefix
This commit is contained in:
Claudiu Lodromanean 2018-11-28 07:48:05 -08:00 committed by GitHub
commit fd195cfbf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 14 deletions

View File

@ -117,6 +117,9 @@ class WC_Install {
'wc_update_350_reviews_comment_type',
'wc_update_350_db_version',
),
'3.5.2' => array(
'wc_update_352_drop_download_log_fk',
)
);
/**
@ -247,7 +250,7 @@ class WC_Install {
/**
* Is this a brand new WC install?
*
* @since 3.2.0
* @since 3.2.0
* @return boolean
*/
private static function is_new_install() {
@ -257,7 +260,7 @@ class WC_Install {
/**
* Is a DB update needed?
*
* @since 3.2.0
* @since 3.2.0
* @return boolean
*/
private static function needs_db_update() {
@ -354,7 +357,8 @@ class WC_Install {
/**
* Add more cron schedules.
*
* @param array $schedules List of WP scheduled cron jobs.
* @param array $schedules List of WP scheduled cron jobs.
*
* @return array
*/
public static function cron_schedules( $schedules ) {
@ -593,19 +597,18 @@ class WC_Install {
// Add constraint to download logs if the columns matches.
if ( ! empty( $download_permissions_column_type ) && ! empty( $download_log_column_type ) && $download_permissions_column_type === $download_log_column_type ) {
$constraint_prefix = ! is_multisite() || ( is_main_site() && is_main_network() ) ? 'fk_' : str_replace( $wpdb->base_prefix, 'fk_', $wpdb->prefix );
$fk_result = $wpdb->get_row( "
SELECT COUNT(*) AS fk_count
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
AND CONSTRAINT_NAME = '{$constraint_prefix}wc_download_log_permission_id'
AND CONSTRAINT_NAME = 'fk_{$wpdb->prefix}wc_download_log_permission_id'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'
" ); // WPCS: unprepared SQL ok.
if ( 0 === (int) $fk_result->fk_count ) {
$wpdb->query( "
ALTER TABLE `{$wpdb->prefix}wc_download_log`
ADD CONSTRAINT `{$constraint_prefix}wc_download_log_permission_id`
ADD CONSTRAINT `fk_{$wpdb->prefix}wc_download_log_permission_id`
FOREIGN KEY (`permission_id`)
REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE;
" ); // WPCS: unprepared SQL ok.
@ -896,7 +899,8 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
/**
* Uninstall tables when MU blog is deleted.
*
* @param array $tables List of tables that will be deleted by WP.
* @param array $tables List of tables that will be deleted by WP.
*
* @return string[]
*/
public static function wpmu_drop_tables( $tables ) {
@ -1166,8 +1170,9 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
/**
* Show action links on the plugin screen.
*
* @param mixed $links Plugin Action links.
* @return array
* @param mixed $links Plugin Action links.
*
* @return array
*/
public static function plugin_action_links( $links ) {
$action_links = array(
@ -1180,9 +1185,10 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
/**
* Show row meta on the plugin screen.
*
* @param mixed $links Plugin Row Meta.
* @param mixed $file Plugin Base file.
* @return array
* @param mixed $links Plugin Row Meta.
* @param mixed $file Plugin Base file.
*
* @return array
*/
public static function plugin_row_meta( $links, $file ) {
if ( WC_PLUGIN_BASENAME === $file ) {
@ -1217,8 +1223,9 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
*
* @param string $plugin_to_install_id Plugin ID.
* @param array $plugin_to_install Plugin information.
*
* @throws Exception If unable to proceed with plugin installation.
* @since 2.6.0
* @since 2.6.0
*/
public static function background_installer( $plugin_to_install_id, $plugin_to_install ) {
// Explicitly clear the event.
@ -1372,8 +1379,9 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
* Install a theme from .org in the background via a cron job (used by installer - opt in).
*
* @param string $theme_slug Theme slug.
*
* @throws Exception If unable to proceed with theme installation.
* @since 3.1.0
* @since 3.1.0
*/
public static function theme_background_installer( $theme_slug ) {
// Explicitly clear the event.

View File

@ -1872,3 +1872,25 @@ function wc_update_350_reviews_comment_type() {
function wc_update_350_db_version() {
WC_Install::update_db_version( '3.5.0' );
}
/**
* Drop the fk_wc_download_log_permission_id FK as we use a new one with the table and blog prefix for MS compatability.
*
* @return void
*/
function wc_update_352_drop_download_log_fk() {
global $wpdb;
$results = $wpdb->get_results( "
SELECT CONSTRAINT_NAME
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
AND CONSTRAINT_NAME = 'fk_wc_download_log_permission_id'
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'
" );
// We only need to drop the old key as WC_Install::create_tables() takes care of creating the new FK.
if ( $results ) {
$wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log DROP FOREIGN KEY fk_wc_download_log_permission_id" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
}
}