diff --git a/src/Install.php b/src/Install.php index 18d9d49..c971443 100644 --- a/src/Install.php +++ b/src/Install.php @@ -266,6 +266,18 @@ private function add_foreign_keys() { private function add_foreign_key( $item ) { global $wpdb; + /** + * Suppress errors. + * + * We suppress errors because adding foreign keys to for example + * a `$wpdb->users` MyISAM table will trigger the following error: + * + * "Error in query (1005): Can't create table '●●●●●●●●. # Sql-●●●●●●●●●●' (errno: 150)" + * + * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/wp-db.php#L1544-L1559 + */ + $suppress_errors = $wpdb->suppress_errors( true ); + /** * Check if foreign key exists * @@ -303,6 +315,8 @@ private function add_foreign_key( $item ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared. $result = $wpdb->query( $item->query ); + $wpdb->suppress_errors( $suppress_errors ); + if ( false === $result ) { throw new \Exception( \sprintf( @@ -313,6 +327,8 @@ private function add_foreign_key( $item ) { ); } } + + $wpdb->suppress_errors( $suppress_errors ); } /** @@ -356,6 +372,24 @@ private function convert_user_meta() { ); } + /** + * Collate caluse. + * + * Force a specific collate to fix: + * "Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and + * (utf8mb4_unicode_520_ci,IMPLICIT) for operation '='. ". + * + * @link https://dev.mysql.com/doc/refman/8.0/en/charset-collate.html + */ + $collate_clause = ''; + + if ( ! empty( $wpdb->collate ) ) { + $collate_clause = \sprintf( + 'COLLATE %s', + $wpdb->collate + ); + } + $query = " INSERT IGNORE INTO $wpdb->pronamic_pay_mollie_customer_users ( customer_id, @@ -368,7 +402,7 @@ private function convert_user_meta() { $wpdb->pronamic_pay_mollie_customers AS mollie_customer INNER JOIN $wpdb->usermeta AS wp_user_meta - ON wp_user_meta.meta_value = mollie_customer.mollie_id COLLATE $wpdb->collate + ON wp_user_meta.meta_value = mollie_customer.mollie_id $collate_clause INNER JOIN $wpdb->users AS wp_user ON wp_user_meta.user_id = wp_user.ID