Download Log FK Contraint check (#20478)
This PR introduces a check on the permission_id FK to ensure that it is not added multiple times on upgrades. It also names the key specifically to ensure future changes to the key can be targeted properly and removes old keys that were added since 3.4.0. * Add FK check before adding the FK. Also give the FK a name to avoid auto generated names and duplicate keys. * Remove additional OR * Remove additional OR * Final FK check query * Add foreign key cleanup routine to 3.4.3 db version * Only check on named foreign key now that we have a cleanup routine in place, use specific phpcignore * Rework formatting of SQL query * Change way to fetch and add FK, can't use procedural SQL, needs to be done via statements. * Add table name to lookup * Only clean up FK on the wc_download_log table * Remove erenouse bracket
This commit is contained in:
parent
25be9fc13c
commit
36d1c318cb
|
@ -105,6 +105,10 @@ class WC_Install {
|
|||
'wc_update_340_last_active',
|
||||
'wc_update_340_db_version',
|
||||
),
|
||||
'3.4.3' => array(
|
||||
'wc_update_343_cleanup_foreign_keys',
|
||||
'wc_update_343_db_version',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -565,7 +569,22 @@ 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 ) {
|
||||
$wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log ADD FOREIGN KEY (permission_id) REFERENCES {$wpdb->prefix}woocommerce_downloadable_product_permissions(permission_id) ON DELETE CASCADE" );
|
||||
$fk_result = $wpdb->get_row( "
|
||||
SELECT COUNT(*) AS fk_count
|
||||
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'
|
||||
" );
|
||||
if ( 0 === (int) $fk_result->fk_count ) {
|
||||
$wpdb->query( "
|
||||
ALTER TABLE `{$wpdb->prefix}wc_download_log`
|
||||
ADD CONSTRAINT `fk_wc_download_log_permission_id`
|
||||
FOREIGN KEY (`permission_id`)
|
||||
REFERENCES `{$wpdb->prefix}woocommerce_downloadable_product_permissions` (`permission_id`) ON DELETE CASCADE;
|
||||
" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1802,3 +1802,36 @@ function wc_update_340_last_active() {
|
|||
function wc_update_340_db_version() {
|
||||
WC_Install::update_db_version( '3.4.0' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove duplicate foreign keys
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function wc_update_343_cleanup_foreign_keys() {
|
||||
global $wpdb;
|
||||
|
||||
$results = $wpdb->get_results( "
|
||||
SELECT CONSTRAINT_NAME
|
||||
FROM information_schema.TABLE_CONSTRAINTS
|
||||
WHERE CONSTRAINT_SCHEMA = '{$wpdb->dbname}'
|
||||
AND CONSTRAINT_NAME LIKE '%wc_download_log_ib%'
|
||||
AND CONSTRAINT_TYPE = 'FOREIGN KEY'
|
||||
AND TABLE_NAME = '{$wpdb->prefix}wc_download_log'
|
||||
" );
|
||||
|
||||
if ( $results ) {
|
||||
foreach ( $results as $fk ) {
|
||||
$wpdb->query( "ALTER TABLE {$wpdb->prefix}wc_download_log DROP FOREIGN KEY {$fk->CONSTRAINT_NAME}" ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update DB version.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function wc_update_343_db_version() {
|
||||
WC_Install::update_db_version( '3.4.3' );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue