@@ -168,9 +168,25 @@ private function get_information_schema_table_names(): array {
168
168
* @return array<string, WP_Parser_Node> The WordPress CREATE TABLE statements.
169
169
*/
170
170
private function get_wp_create_table_statements (): array {
171
+ // Bail out when not in a WordPress environment.
171
172
if ( ! defined ( 'ABSPATH ' ) ) {
172
173
return array ();
173
174
}
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.
174
190
if ( file_exists ( ABSPATH . 'wp-admin/includes/schema.php ' ) ) {
175
191
require_once ABSPATH . 'wp-admin/includes/schema.php ' ;
176
192
}
@@ -183,7 +199,7 @@ private function get_wp_create_table_statements(): array {
183
199
* the database connection. Let's only populate the table names using
184
200
* the "$table_prefix" global so we can get correct table names.
185
201
*/
186
- global $ wpdb , $ table_prefix ;
202
+ global $ table_prefix ;
187
203
$ wpdb ->set_prefix ( $ table_prefix );
188
204
189
205
// Get schema for global tables.
0 commit comments