Last active
March 6, 2023 06:22
-
-
Save todeveni/45cd4862ce8cbb14679c5d0b7bc22103 to your computer and use it in GitHub Desktop.
Convert WordPress collation to utf8mb_swedish_ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! defined( 'WP_CLI' ) ) { | |
print "Run with wp eval-file wp_collate.php\n"; | |
exit; | |
} | |
global $wpdb, $table_prefix; | |
$wp_config_path = \WP_CLI\Utils\locate_wp_config(); | |
$wp_config_transformer = new \WPConfigTransformer( $wp_config_path ); | |
if ( defined( 'DB_CHARSET' ) && 'utf8mb4' !== DB_CHARSET ) { | |
$wp_config_transformer->update( 'constant', 'DB_CHARSET', 'utf8mb4', [] ); | |
} | |
if ( defined( 'DB_COLLATE' ) && 'utf8mb4_swedish_ci' !== DB_COLLATE ) { | |
$wp_config_transformer->update( 'constant', 'DB_COLLATE', 'utf8mb4_swedish_ci', [] ); | |
} | |
$status = $wpdb->query( | |
$wpdb->prepare( | |
"ALTER DATABASE `" . DB_NAME . "` DEFAULT CHARACTER SET = %s DEFAULT COLLATE = %s", | |
'utf8mb4', | |
'utf8mb4_swedish_ci' | |
) | |
); | |
// var_dump( 'ALTER DATABASE: ' . DB_NAME . ' ' . $status ); | |
$tables = $wpdb->get_results( | |
$wpdb->prepare( | |
"SELECT *" . | |
" FROM INFORMATION_SCHEMA.TABLES" . | |
" WHERE TABLE_SCHEMA = %s" . | |
" AND TABLE_NAME LIKE %s", | |
DB_NAME, | |
$table_prefix . '%' | |
) | |
); | |
foreach ( $tables as $table ) { | |
$columns = $wpdb->get_results( | |
$wpdb->prepare( | |
"SELECT *" . | |
" FROM INFORMATION_SCHEMA.COLUMNS" . | |
" WHERE TABLE_SCHEMA = %s" . | |
" AND TABLE_NAME = %s", | |
DB_NAME, | |
$table->TABLE_NAME | |
) | |
); | |
$table->COLUMNS = $columns; | |
} | |
foreach ( $tables as $table ) { | |
if ( 'utf8mb4_swedish_ci' !== $table->TABLE_COLLATION ) { | |
$status = $wpdb->query( | |
$wpdb->prepare( | |
"ALTER TABLE `" . $table->TABLE_NAME . "` CONVERT TO CHARACTER SET %s COLLATE %s", | |
'utf8mb4', | |
'utf8mb4_swedish_ci' | |
) | |
); | |
// var_dump( 'ALTER TABLE: ' . $table->TABLE_NAME . ' ' . $status ); | |
$status = $wpdb->query( | |
$wpdb->prepare( | |
"ALTER TABLE `" . $table->TABLE_NAME . "` DEFAULT CHARACTER SET %s COLLATE %s", | |
'utf8mb4', | |
'utf8mb4_swedish_ci' | |
) | |
); | |
// var_dump( 'ALTER TABLE DEFAULT: ' . $table->TABLE_NAME . ' ' . $status ); | |
} | |
foreach ( $table->COLUMNS as $column ) { | |
if ( ! is_null( $column->COLLATION_NAME ) && 'utf8mb4_swedish_ci' !== $column->COLLATION_NAME ) { | |
$status = $wpdb->query( | |
$wpdb->prepare( | |
"ALTER TABLE `" . $table->TABLE_NAME . "` MODIFY `" . $column->COLUMN_NAME . "` " . $column->COLUMN_TYPE . " CHARACTER SET %s COLLATE %s", | |
'utf8mb4', | |
'utf8mb4_swedish_ci' | |
) | |
); | |
// var_dump( 'ALTER TABLE MODIFY: ' . $table->TABLE_NAME . ' ' . $column->COLUMN_NAME . ' ' . $status ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment