@@ -168,9 +168,25 @@ private function get_information_schema_table_names(): array {
168168 * @return array<string, WP_Parser_Node> The WordPress CREATE TABLE statements.
169169 */
170170 private function get_wp_create_table_statements (): array {
171+ // Bail out when not in a WordPress environment.
171172 if ( ! defined ( 'ABSPATH ' ) ) {
172173 return array ();
173174 }
175+
176+ /*
177+ * In WP CLI, $wpdb may not be set. In that case, we can't load the schema.
178+ * We need to bail out and use the standard non-WordPress-specific behavior.
179+ */
180+ global $ wpdb ;
181+ if ( ! isset ( $ wpdb ) ) {
182+ // Outside of WP CLI, let's trigger a warning.
183+ if ( ! defined ( 'WP_CLI ' ) || ! WP_CLI ) {
184+ trigger_error ( 'The $wpdb global is not initialized. ' , E_USER_WARNING );
185+ }
186+ return array ();
187+ }
188+
189+ // Ensure the "wp_get_db_schema()" function is defined.
174190 if ( file_exists ( ABSPATH . 'wp-admin/includes/schema.php ' ) ) {
175191 require_once ABSPATH . 'wp-admin/includes/schema.php ' ;
176192 }
@@ -183,7 +199,7 @@ private function get_wp_create_table_statements(): array {
183199 * the database connection. Let's only populate the table names using
184200 * the "$table_prefix" global so we can get correct table names.
185201 */
186- global $ wpdb , $ table_prefix ;
202+ global $ table_prefix ;
187203 $ wpdb ->set_prefix ( $ table_prefix );
188204
189205 // Get schema for global tables.
0 commit comments