From 1643fd56061c87cdce593f65318b886116e0a69d Mon Sep 17 00:00:00 2001 From: Herb Date: Mon, 28 Apr 2025 15:11:20 -0400 Subject: [PATCH 1/7] Issue #461 add utf8mb4 command --- commands/db.bee.inc | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index a3e4eea..202d2c6 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -104,6 +104,20 @@ function db_bee_command() { 'bee db-query "SELECT name, mail FROM {users} WHERE uid > 0 LIMIT 2"' => bt('Show the name and email for the first 2 users.'), ), ), + 'utf8mb4-convert' => array( + 'description' => 'Convert the database and tables to utf8mb4 encoding.', + 'callback' => 'utf8mb4_convert_bee_callback', + 'command_requirements' => array( + 'backdrop_root' => TRUE, + 'backdrop_installed' => TRUE, + ), + 'group' => 'database', + 'aliases' => array('utf8mb4'), + 'bootstrap' => BEE_BOOTSTRAP_FULL, + 'examples' => array( + 'bee utf8mb4-convert' => 'Convert the main database to utf8mb4 encoding.', + ), + ), ); } @@ -451,3 +465,71 @@ function dbq_bee_callback($arguments, $options) { bee_message($e->getMessage(), 'error'); } } + +/** + * Command callback: utf-8 mb4 conversion. + */ +function utf8mb4_convert_bee_callback($arguments, $options) { + $connection = Database::getConnection(); + $messages = array(); + $utf8mb4_active = $connection->utf8mb4IsActive(); + $utf8mb4_supported = $connection->utf8mb4IsSupported(); + $utf8mb4_tables_converted = state_get('database_utf8mb4_active', FALSE); + + if (!$utf8mb4_active || !$utf8mb4_supported) { + bee_message('utf8mb4 not supported.', 'error'); + return; + } + + // Already active and supported. Indicate completion. + if ($utf8mb4_active && $utf8mb4_supported && $utf8mb4_tables_converted) { + bee_message('utf8mb4 is already enabled on your site, no further action is needed.'); + return; + } + + global $databases; + + // Enter maintenance mode. + state_set('maintenance_mode', TRUE); + + // Build the list of tables to convert. + $database = $databases['default']['default']; + $like = ''; + if (!empty($database['prefix'])) { + $like = ' LIKE "' . $database['prefix'] . '%"'; + } + $table_names = db_query('SHOW TABLES' . $like)->fetchCol(); + + // Instantiate a character set converter object. + $converter = new DatabaseCharsetConverter(); + + try { + $result = $converter->convertDatabase($databases['default']['default']['database']); + if (!$result) { + $messages[] = t('The database %name could not be converted.', array('%name' => $databases['default']['default']['database'])); + bee_message(implode(', ', $messages), 'error'); + return; + } + } + catch (PDOException $e) { + $messages[] = $e->getMessage(); + bee_message(implode(', ', $messages), 'error'); + return; + } + + foreach ($table_names as $table_name) { + try { + $result = $converter->convertTable($table_name); + if (!$result) { + $messages[] = t('The table %name could not be converted.', array('%name' => $table_name)); + } + } + catch (PDOException $e) { + $messages[] = $e->getMessage(); + } + } + bee_message(implode(', ', $messages)); + state_set('maintenance_mode', FALSE); + state_set('database_utf8mb4_active', TRUE); + bee_message('Database and tables successfully converted.'); +} From 4e31d704bcbc1028c45e038d4211555f796aa513 Mon Sep 17 00:00:00 2001 From: Herb Date: Tue, 29 Apr 2025 10:15:31 -0400 Subject: [PATCH 2/7] Add confirmation --- commands/db.bee.inc | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index 202d2c6..bbe40f7 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -470,6 +470,8 @@ function dbq_bee_callback($arguments, $options) { * Command callback: utf-8 mb4 conversion. */ function utf8mb4_convert_bee_callback($arguments, $options) { + global $_bee_yes_mode; + $connection = Database::getConnection(); $messages = array(); $utf8mb4_active = $connection->utf8mb4IsActive(); @@ -489,11 +491,21 @@ function utf8mb4_convert_bee_callback($arguments, $options) { global $databases; + // Build the list of tables to convert. + $database = $databases['default']['default']; + + if (!$_bee_yes_mode) { + // Prompt to continue. + if (!bee_confirm(bt('The upgrade process is irreversible and in the event of an error, you will want to restore a previous version. You are recommended to create a database backup before proceeding; you can do this with the 'db-export' command if you have not already created one. Do you want to proceed with the database conversion of !database now?', array( + '!database' => $database, + )), FALSE)) { + return; + } + } + // Enter maintenance mode. state_set('maintenance_mode', TRUE); - // Build the list of tables to convert. - $database = $databases['default']['default']; $like = ''; if (!empty($database['prefix'])) { $like = ' LIKE "' . $database['prefix'] . '%"'; From 63c27087338e44ca34d3bcb7ce6a8c8e66a3fb1b Mon Sep 17 00:00:00 2001 From: Herb Date: Tue, 29 Apr 2025 10:17:44 -0400 Subject: [PATCH 3/7] Update example --- commands/db.bee.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index bbe40f7..5915019 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -115,7 +115,8 @@ function db_bee_command() { 'aliases' => array('utf8mb4'), 'bootstrap' => BEE_BOOTSTRAP_FULL, 'examples' => array( - 'bee utf8mb4-convert' => 'Convert the main database to utf8mb4 encoding.', + 'bee utf8mb4-convert' => 'Convert the main database to utf8mb4 encoding. You will then be prompted to confirm.', + 'bee --yes utf8mb4-convert' => 'Convert the main database to utf8mb4 encoding. You will NOT be prompted to confirm.', ), ), ); From 85ca53c56ef9079d6e6a0078b99b3cfa22c8f48f Mon Sep 17 00:00:00 2001 From: Herb Date: Tue, 29 Apr 2025 10:18:58 -0400 Subject: [PATCH 4/7] Fix quotes --- commands/db.bee.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index 5915019..197b9dd 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -497,7 +497,7 @@ function utf8mb4_convert_bee_callback($arguments, $options) { if (!$_bee_yes_mode) { // Prompt to continue. - if (!bee_confirm(bt('The upgrade process is irreversible and in the event of an error, you will want to restore a previous version. You are recommended to create a database backup before proceeding; you can do this with the 'db-export' command if you have not already created one. Do you want to proceed with the database conversion of !database now?', array( + if (!bee_confirm(bt('The upgrade process is irreversible and in the event of an error, you will want to restore a previous version. You are recommended to create a database backup before proceeding; you can do this with the db-export command if you have not already created one. Do you want to proceed with the database conversion of !database now?', array( '!database' => $database, )), FALSE)) { return; From a091ad4fd51d1f857d5b02a7d669b4c6e8a99a1b Mon Sep 17 00:00:00 2001 From: Herb Date: Wed, 30 Apr 2025 15:23:57 -0400 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Martin Price --- commands/db.bee.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index 197b9dd..bfd1481 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -497,8 +497,8 @@ function utf8mb4_convert_bee_callback($arguments, $options) { if (!$_bee_yes_mode) { // Prompt to continue. - if (!bee_confirm(bt('The upgrade process is irreversible and in the event of an error, you will want to restore a previous version. You are recommended to create a database backup before proceeding; you can do this with the db-export command if you have not already created one. Do you want to proceed with the database conversion of !database now?', array( - '!database' => $database, + if (!bee_confirm(bt('The upgrade process is irreversible and in the event of an error, you will want to restore a previous version. You are recommended to create a database backup before proceeding; you can do this with the "db-export" command if you have not already created one. Do you want to proceed with the conversion of tables in the "!database" database now?', array( + '!database' => $database['database'], )), FALSE)) { return; } From b15ab3670a87a16c5c7851bb2d40507e3ef3ecbc Mon Sep 17 00:00:00 2001 From: Herb Date: Wed, 30 Apr 2025 15:25:35 -0400 Subject: [PATCH 6/7] Update db.bee.inc --- commands/db.bee.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index bfd1481..739083a 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -517,9 +517,9 @@ function utf8mb4_convert_bee_callback($arguments, $options) { $converter = new DatabaseCharsetConverter(); try { - $result = $converter->convertDatabase($databases['default']['default']['database']); + $result = $converter->convertDatabase($database['database']); if (!$result) { - $messages[] = t('The database %name could not be converted.', array('%name' => $databases['default']['default']['database'])); + $messages[] = t('The database %name could not be converted.', array('%name' => $database['database'])); bee_message(implode(', ', $messages), 'error'); return; } From 7e54c202049d24bbbdb1ef8c41c6375a0e50fa7e Mon Sep 17 00:00:00 2001 From: Herb Date: Wed, 30 Apr 2025 15:26:34 -0400 Subject: [PATCH 7/7] Update db.bee.inc --- commands/db.bee.inc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/commands/db.bee.inc b/commands/db.bee.inc index 739083a..13938fe 100644 --- a/commands/db.bee.inc +++ b/commands/db.bee.inc @@ -491,8 +491,6 @@ function utf8mb4_convert_bee_callback($arguments, $options) { } global $databases; - - // Build the list of tables to convert. $database = $databases['default']['default']; if (!$_bee_yes_mode) { @@ -507,6 +505,7 @@ function utf8mb4_convert_bee_callback($arguments, $options) { // Enter maintenance mode. state_set('maintenance_mode', TRUE); + // Build the list of tables to convert. $like = ''; if (!empty($database['prefix'])) { $like = ' LIKE "' . $database['prefix'] . '%"';