Skip to content

Add support for the new SQLite driver #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

JanJakes
Copy link

This PR adds support for the new SQLite driver.

When the new SQLite driver is available and enabled, it will execute all the MySQL queries using the new driver. Otherwise, the legacy SQLite driver will be used.

@JanJakes JanJakes force-pushed the new-sqlite-driver branch 4 times, most recently from aa27915 to 464ae33 Compare June 25, 2025 15:25
@JanJakes JanJakes force-pushed the new-sqlite-driver branch from 464ae33 to 275b382 Compare June 25, 2025 15:33
@wojtekn
Copy link
Contributor

wojtekn commented Jun 26, 2025

@JanJakes, thanks for adding support for AST to the SQLite command. I used those steps to test it in Studio:

  1. Clone the PR locally
  2. Run composer install --no-dev --optimize-autoloader --ignore-platform-reqs in the wp-cli-sqlite-command directory
  3. Replace wp-files/sqlite-command in the Studio code with a symlink to your local wp-cli-sqlite-command directory
  4. Apply 3658f-pb patch to the Studio repo
  5. Comment out the line that updates the SQLite command:
//await updateLatestSQLiteCommandVersion();
  1. Run npm start
  2. Click on the Import / Export tab
  3. Click on the "Export database"
  4. Observe .sql file is created
    10 Open the .sql file and validate that the dump is correct

I couldn't make it fully work yet. I needed to make a few changes, see inline comments.

*/
public static function create_driver() {
$new_driver_supported = class_exists( WP_SQLite_Driver::class );
$new_driver_enabled = $new_driver_supported && defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my tests, DB_NAME and WP_SQLITE_AST_DRIVER constants are undefined. I needed to hardcode the first one as database_name_here and $new_driver_enabled as true for testing purposes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wojtekn The "correct" value of the database name will be needed with wp sqlite import because CREATE TABLE statements need to write it in the information schema. Are we in a context where importing the wp-config.php of a given site would be conceivable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command runs on before_wp_load, so wp-config.php is not loaded. See #7 for some context.

Perhaps we could load it manually, similar to how WP CLI runner already does?

eval( WP_CLI::get_runner()->get_wp_config_code() );

Or alternatively, to limit the amount of loaded code, what if we used the WP CLI parameter instead of the WP_SQLITE_AST_DRIVER constant to enable the new driver? Like --driver=[ast|legacy]?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively, to limit the amount of loaded code, what if we used the WP CLI parameter instead of the WP_SQLITE_AST_DRIVER constant to enable the new driver? Like --driver=[ast|legacy]?

@wojtekn This would work for the AST/legacy and would be temporary anyway. But what about the DB_NAME? 🤔

There's this edge-case scenario:

  1. Someone imports a DB (we use some DB name during that process).
  2. Then a plugin code runs SELECT * FROM information_schema.tables WHERE table_schema = 'my-db-name' AND ....

In point 2, we'd like the DB name to match the DB_NAME that was set during the import.

By the way, is Playground booted anywhere before the import command is run? Because we know that site exports without DB_NAME exist, and the fallback to inject their value in wp-config.php is done in Playground at the moment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wojtekn Looking at wp db import, it seems to me they're using after_wp_config_load on the class level, and no other annotation for the import command. A couple of commands use after_wp_load.

Also explained here: https://developer.wordpress.org/cli/commands/db/import/

This command runs on the after_wp_config_load hook, after wp-config.php has been loaded into scope. Runs SQL queries using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. This does not create database by itself and only performs whatever tasks are defined in the SQL.

Maybe we could use after_wp_config_load for the import command?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JanJakes, it makes sense to try with the after_wp_config_load for the import command, especially since it would be consistent with wp db import.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wojtekn I pushed that change, let me know if it works for you.

@JanJakes
Copy link
Author

@wojtekn I pushed some changes to fix all the issues, except for DB_NAMEI'm wondering if there's a way to use the correct database name in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants