Skip to content

Conversation

JanJakes
Copy link
Contributor

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
  5. 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
Contributor 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
Contributor 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
Contributor 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
Contributor 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

@JanJakes I realized I was testing Export above, so the change doesn't fix the behavior. When I set after_wp_config_load for export command, it reads DB_NAME from wp-config.php correctly.

However, it still doesn't read WP_SQLITE_AST_DRIVER that is set in Studio through defineWpConfigConsts.

Copy link
Contributor

Choose a reason for hiding this comment

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

WP_SQLITE_AST_DRIVER constant set in Studio through defineWpConfigConsts enables the driver for the Studio site running in a browser.

It doesn't work for sqlite export as it is executed through a non-Playground process. Would it make sense to use --driver=[ast|legacy] for that part?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wojtekn Great! Let's use after_wp_config_load for all the commands, then? As for AST, yes, let's make it configurable on the CLI 👍

@JanJakes
Copy link
Contributor 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.

@wojtekn
Copy link
Contributor

wojtekn commented Jul 3, 2025

I pushed a few more changes:

  • --enable-ast-driver param to enable the AST driver for CLI calls
  • simplified $new_driver_enabled check to fix loading order issues
  • moved commands to after_wp_config_load event

@wojtekn
Copy link
Contributor

wojtekn commented Jul 4, 2025

I reverted after_wp_config_load event change as it's no longer needed after WordPress/sqlite-database-integration#203 was merged.

This ensures the DB_NAME constant from "wp-config.php" is used,
and aligns these commands with similar "wp db" commands.

With WordPress/sqlite-database-integration#213,
the correctness of the database name will also be verified.
Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

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

It works great with sqlite database integration 2.2.3 and this Studio PR Automattic/studio#1506

@wojtekn wojtekn merged commit d426211 into main Jul 15, 2025
34 checks passed
@wojtekn wojtekn deleted the new-sqlite-driver branch July 15, 2025 13:27
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.

3 participants