Skip to content

Failed to parse the MySQL query error #263

Closed
Automattic/wp-cli-sqlite-command
#14

Description

@sejas

When importing a WordPress MySQL dump, I’m getting an error when executing wp sqlite import dump.sql, which parses the file into multiple queries and then calls driver->query($statement) for each statement. The SQLite import command is parsing the query incorrectly, and then the SQLite driver throws an error.

I wonder if there is a built-in parser in the SQLite plugin or if we could pass and execute multiple statements together instead of one by one.

Here is the related code:

https://github.com/Automattic/wp-cli-sqlite-command/blob/main/src/Import.php#L53-L54

Example Query:

/*M!999999\- enable the sandbox mode */ 
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `wp_redirection_404` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL,
  `url` mediumtext NOT NULL,
  `domain` varchar(255) DEFAULT NULL,
  `agent` varchar(255) DEFAULT NULL,
  `referrer` varchar(255) DEFAULT NULL,
  `http_code` int(11) unsigned NOT NULL DEFAULT 0,
  `request_method` varchar(10) DEFAULT NULL,
  `request_data` mediumtext DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `created` (`created`),
  KEY `referrer` (`referrer`(191)),
  KEY `ip` (`ip`)
) ENGINE=InnoDB AUTO_INCREMENT=280033 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT INTO `wp_redirection_404` (`id`, `created`, `url`, `domain`, `agent`, `referrer`, `http_code`, `request_method`, `request_data`, `ip`) VALUES (279314,'2025-09-26 08:45:02','/\\\"https:\\/\\/sejas.blog\\/newsletter\\/\\','https://sejas.blog','Mozilla/5.0 (compatible; FeedDiscovery/1.0; +https://example.com/bot)',NULL,404,'GET',NULL,'127.0.0.1');

Error

PHP Fatal error:  Uncaught WP_SQLite_Driver_Exception: Failed to parse the MySQL query. in /wordpress/wp-content/mu-plugins/sqlite-database-integration/wp-includes/sqlite-ast/class-wp-sqlite-driver.php:5035
Stack trace:
#0 /wordpress/wp-content/mu-plugins/sqlite-database-integration/wp-includes/sqlite-ast/class-wp-sqlite-driver.php(672): WP_SQLite_Driver->new_driver_exception('Failed to parse...')
#1 /tmp/sqlite-command/src/Import.php(54): WP_SQLite_Driver->query('INSERT INTO `wp...')
#2 /tmp/sqlite-command/src/Import.php(38): Automattic\WP_CLI\SQLite\Import->execute_statements('studio-backup-s...')
#3 /tmp/sqlite-command/src/SQLite_Command.php(45): Automattic\WP_CLI\SQLite\Import->run('studio-backup-s...', Array)
#4 [internal function]: Automattic\WP_CLI\SQLite\SQLite_Command->import(Array, Array)
#5 /tmp/sqlite-command/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(100): call_user_func(Array, Array, Array)
#6 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array, Array)
#7 /tmp/sqlite-command/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(497): call_user_func(Object(Closure), Array, Array)
#8 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(470): WP_CLI\Dispatcher\Subcommand->invoke(Array, Array, Array)
#9 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(493): WP_CLI\Runner->run_command(Array, Array)
#10 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(136): WP_CLI\Runner->run_command_and_exit()
#11 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1349): WP_CLI\Runner->do_early_invoke('after_wp_config...')
#12 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1293): WP_CLI\Runner->load_wordpress()
#13 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#14 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/bootstrap.php(84): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#15 phar:///tmp/wp-cli.phar/vendor/wp-cli/wp-cli/php/wp-cli.php(35): WP_CLI\bootstrap()
#16 phar:///tmp/wp-cli.phar/php/boot-phar.php(20): include('phar:///tmp/wp-...')
#17 /tmp/wp-cli.phar(4): include('phar:///tmp/wp-...')
#18 /tmp/run-cli.php(25): require('/tmp/wp-cli.pha...')
#19 {main}
  thrown in /wordpress/wp-content/mu-plugins/sqlite-database-integration/wp-includes/sqlite-ast/class-wp-sqlite-driver.php on line 5035

    at JetpackImporter.importDatabase (/Applications/Studio.app/Contents/Resources/app.asar/dist/main/index.js:14371:17)
    at async JetpackImporter.import (/Applications/Studio.app/Contents/Resources/app.asar/dist/main/index.js:14411:9)
    at async importBackup (/Applications/Studio.app/Contents/Resources/app.asar/dist/main/index.js:15754:12)
    at async importSite (/Applications/Studio.app/Contents/Resources/app.asar/dist/main/index.js:16207:20)
    at async Session.<anonymous> (node:electron/js2c/browser_init:2:107024)

Activity

  1. sejas commented on Oct 7, 2025

    @sejas
    Author

    @JanJakes , do you have any input about the MySQL parser?

  2. JanJakes commented on Oct 8, 2025

    @JanJakes
    Member

    @sejas The parser can do it, but the driver itself expects to process the queries one by one at the moment. From the outside, it's usable as follows:

    $parser = $driver->create_parser( $input );
    while ( $parser->next_query() ) {
        $ast = $parser->get_query_ast();
    
        // For now, we'd use the AST only to "cut out" the query and pass it as string again:
        $query = substr( $input, $ast->get_start(), $ast->get_length() );
        $driver->query( $query );
    }

    I think this is a quick and reasonable fix on the WP CLI SQLite command side for now.

  3. sejas commented on Oct 8, 2025

    @sejas
    Author

    @JanJakes , I created this PR Automattic/wp-cli-sqlite-command#13
    I had to remove all the comments with a regex because the AST parser was identifying them as queries to execute, which caused it to fail. For example Error: SQLite import could not execute statement: SET @saved_cs_client = @@character_set_client */;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions