Make WordPress Core

Opened 4 years ago

Last modified 10 days ago

#57793 new feature request

Add native support for SQLite

Reported by: aristath Owned by:
Priority: normal Milestone: Future Release
Component: Database Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description (last modified by aristath)

A few months ago, a proposal was published to natively support SQLite in WordPress Core (​https://make.wordpress.org/core/2022/09/12/lets-make-wordpress-officially-support-sqlite/).
That proposal outlines a lot of the benefits of supporting SQLite in Core.

The proposal has received a lot of positive feedback, both from the community and the WordPress leadership so this Trac ticket is the next step, to promote collaboration and discussion.

The performance team has been working on an implementation that adds support for SQLite via the SQLite Database Integration plugin (​https://wordpress.org/plugins/sqlite-database-integration/). Before being published as a stand-alone plugin, it was implemented as a module in the Performance Lab plugin (​https://wordpress.org/plugins/performance-lab/) where it received a lot of user-testing.

Change History (22)

#1 @aristath
4 years ago

  • Description modified (diff)

With the plugin active, all WordPress PHPUnit tests pass (with just a few exceptions that need patching in Core when the featured plugin gets merged, see ​https://github.com/WordPress/sqlite-database-integration/pull/9#issuecomment-1437018625 for details).

#2 @aristath
4 years ago

  • Description modified (diff)

This ticket was mentioned in ​PR #3220 on ​WordPress/wordpress-develop by ​@aristath.


4 years ago
#3

  • Keywords has-patch added

#4 @swissspidy
4 years ago

In the blog post I see lots of unanswered questions about keeping it as a canonical plugin rather than merging it into core. Given that the post is already a few months old, I'd suggest writing a follow-up post with the latest updates (like the improved security thanks to the new parser implementation, which is pretty neat), which can then be discussed in dev chat as well.

#5 @aristath
4 years ago

Thank you for the feedback @swisspidy!

In the blog post I see lots of unanswered questions about keeping it as a canonical plugin rather than merging it into core.

Using the featured plugin is a great way to allow users to test the implementation, iron-out any issues etc. However, long-term it doesn't make sense to use this as a plugin.
Keeping it as a canonical plugin basically negates most of the SQLite benefits... A plugin can only be installed on an existing site - which would require having a MySQL database.

Given that the post is already a few months old, I'd suggest writing a follow-up post with the latest updates (like the improved security thanks to the new parser implementation, which is pretty neat), which can then be discussed in dev chat as well.

That's a good idea, adding it to my TODO list 👍

Last edited 4 years ago by aristath (previous) (diff)

​@zieladam commented on ​PR #3220:


4 years ago
#6

Update: This PR now translates queries using the ​MySQL lexer from sqlite-database-integration instead of ​regular expressions from wp-sqlite-db. The new implementation is a lot safer and more reliable.

This ticket was mentioned in ​Slack in #hosting-community by javier. ​View the logs.


4 years ago

​@aristath commented on ​PR #3220:


4 years ago
#8

I pushed a change as a proof-of-concept for how the installation screen can change.
It's not perfect, but it works and showcases how users can pick a database-type, and what that may look like.
If we don't want to provide users the option to select the db-type via the UI, we can revert the changes in these 2 files:

  • src/wp-admin/setup-config.php
  • wp-config-sample.php

​sangaman commented on ​PR #3220:


3 years ago
#9

I'm excited for this. I'd be happy to test this PR on my personal website whenever it's nearing readiness.

#10 @joemcgill
3 years ago

  • Milestone Awaiting Review → Future Release

Adding this to the Future Release milestone for consideration once a stronger use case is presented for why this should be in core and not simply continue as a feature plugin.

​vadimkantorov commented on ​PR #3220:


3 years ago
#11

This is really nice also because it would better enable usage of WordPress as a Static Site Generator (as it simplifies operating WordPress using file system only, for both settings and content)

​mrchrisadams commented on ​PR #3220:


3 years ago
#12

hi folks.

I've seen a bit of pushback against SQLite because of both the license it uses, along with their policy for accepting contributions.

If this is still a bone of contention, it's worth knowing that there is a API compatible fork of SQLite called libSQL, which has a code of conduct, and accepts contributions, and uses an OSI approved open source licence.

Here's the manifesto published, explaining why it exists:
​https://turso.tech/libsql-manifesto

Here's the project:
​https://github.com/tursodatabase/libsql

It's also worth knowing that there is work ongoing to support libSQL in Laravel, and they detail their reasons for doing so too:
​https://github.com/laravel/framework/discussions/49340

For what it's worth, I'm a fan of SQLite (or SQLite compatible) support making it into Wordpress, so I'm happy to see it in core, as I actually think it could make for a nicer (albeit different) onboarding experience than the famous 5 minute install.

​@dingo_d commented on ​PR #3220:


3 years ago
#13

Just a quick question, when using the ​SQLite drop-in, does anybody get errors on wp 6.5 regarding the minimum MySQL version?

This ticket was mentioned in ​Slack in #core-php by dd32. ​View the logs.


3 years ago

​vadimkantorov commented on ​PR #3220:


2 years ago
#15

This would also lead a way to SSG mode where the content is stored in individual text files (as markdown or json). Writing a script for populating a temp SQLite database from a bunch of text/json files in directories should be quite easy

An important functionality for wpcli should be conversion from an existing MySQL db to a SQLite db.
Handrolled hacks are easy to come by, but having a more or a less CI-tested one on large realistic databases would be of great value

​@aristath commented on ​PR #3220:


2 years ago
#16

An important functionality for wpcli should be conversion from an existing MySQL Wordpress db to a SQLite db file

That's already possible from the terminal, but I completely agree... A WP-CLI script specifically for that task would simplify things a lot 👍

​vadimkantorov commented on ​PR #3220:


2 years ago
#17

Also, maybe a UI admin button for downloading/exporting existing MySQL db as a SQLite file would be useful (for the cases where we don't have an ssh access to the hosted installation)

​vadimkantorov commented on ​PR #3220:


2 years ago
#18

Also, an interesting idea could be implementing a SQLite vtab extension which would allow directly working with posts/pages stored in a file directory and e.g. config in a json file on disk

​vadimkantorov commented on ​PR #3220:


2 years ago
#19

Also, maybe a UI admin button for downloading/exporting existing MySQL db as a SQLite file would be useful

And maybe this export functionality could be merged in the mainline even before the merge of the full Sqlite support changeset...

​vadimkantorov commented on ​PR #3220:


2 years ago
#20

In general, curious what is the status of this feature/merging-into-the-mainline with the core Wordpress team? Looking forward to benefit from this feature in mainline :)

​ecotechie commented on ​PR #3220:


2 years ago
#21

Just a quick question, when using the ​SQLite drop-in, does anybody get errors on wp 6.5 regarding the minimum MySQL version?

Yup. It seems we are now looking for MySQL and fail otherwise?

This ticket was mentioned in ​PR #15245 on ​WordPress/wordpress-develop by ​JanJakes.


10 days ago
#22

  • Keywords has-unit-tests added

[!WARNING]
WORK IN PROGRESS: A draft to guide the design of the SQLite driver for integration in Core.

This adds experimental SQLite support to WordPress. With SQLite, WordPress stores its data in a single database file on the server, and it needs no MySQL or MariaDB server.

WordPress keeps sending MySQL queries. The bundled ​MySQL-on-SQLite driver from the ​SQLite Database Integration project parses each query and runs it on SQLite. It's the same driver that already powers WordPress Playground, WordPress Studio, the SQLite Database Integration plugin, and other projects.

https://github.com/user-attachments/assets/ce29290b-ec80-43a5-a773-5cff68884ca9

https://github.com/user-attachments/assets/eb1abc73-269a-4227-bec3-1e8f25e7dba3

Changes

The changes are split into commits by area:

  1. Bundled driver: Adds MySQL-on-SQLite 3.1.2 and a script to update it.
  2. Database: wpdb runs on SQLite when DB_ENGINE is sqlite, using the database file at DB_PATH. Existing drop-ins keep working.
  3. Setup: The wp-config.php setup offers SQLite (experimental). It uses a DB_PATH set by the host, or a random file name in wp-content/database. The database is created only when the installation runs, so DB_PATH can still be changed before that.
  4. Site Health: Shows SQLite details, checks the SQLite version, and recommends moving the database outside the web root.
  5. Tests and tooling: The local environment and CI can run with SQLite.

The driver is bundled in three new directories:

src/wp-includes/
├── parser/               # Generic parser
└── database/
    ├── mysql-parser/     # MySQL lexer, parser, and grammar
    └── mysql-on-sqlite/  # MySQL-on-SQLite driver

Why

SQLite needs no database server, which makes WordPress easier to run for development, testing, and small sites. Playground and Studio already rely on it through the plugin. Supporting it in Core removes the need for a db.php drop-in, and lets Core test and maintain it.

This is a fresh take on WordPress/wordpress-develop#3220. It uses the newer driver, which parses queries with the full MySQL grammar, instead of rewriting them with a lexer and regular expressions. It also keeps wpdb intact, instead of replacing it with a subclass.

Known issues

10 tests still fail with SQLite, so the SQLite jobs fail for now. All other tests pass with SQLite, for both single site and multisite. The failures come from gaps in the driver's MySQL emulation, which will be fixed in the driver:

  • UPDATE reports matched rows instead of changed rows (4 tests).
  • REPLACE reports 1 affected row instead of 2 (1 test).
  • INFORMATION_SCHEMA has no AUTO_INCREMENT value (1 test).
  • The emulated MySQL 8.0.38 reports utf8 instead of utf8mb3 (2 tests), and integer display widths (1 test).
  • SHOW VARIABLES has no collation_connection (1 test).

TODO

  • [ ] Use the right release in the @since tags. They use 7.3.0 now.
  • [ ] Decide whether wp-config-sample.php should document DB_ENGINE for all sites.
  • [ ] Decide whether readme.html should list SQLite in the requirements.
  • [ ] Make the update check report SQLite to WordPress.org, instead of the emulated MySQL version.
  • [ ] Fix the MySQL emulation gaps in the driver upstream, and bundle the new release.
  • [ ] Consider a one-time marker, so that only the setup run that wrote wp-config.php can create the database. Today, setup-config.php?step=3 recreates a missing database file for anyone while wp-config.php exists, so a site that lost its database could be installed again.

Testing

To try the setup, remove wp-config.php, open /wp-admin/setup-config.php, and select SQLite (experimental).

To use SQLite in the local environment, set LOCAL_DB_TYPE=sqlite in .env, and run:

npm run env:restart
npm run env:install
npm run test:php

Updating this PR

To update this branch to the latest WordPress trunk or the latest SQLite Database Integration release, give a capable coding agent this prompt:

Update the branch of this pull request. Rebase it on the latest trunk of
WordPress/wordpress-develop. If WordPress/sqlite-database-integration has a newer
release, bundle it with `tools/mysql-on-sqlite/installer.sh --version=<version>`.

Keep the commit history as clear as it is: the same commits, in the same order, one
per area. Put every change in the commit where it belongs, using fixup commits and
an autosquash rebase, and update a commit message when the behavior it describes
changes. Don't edit the bundled driver files by hand.

Make sure that PHPCS, PHPStan, and the E2E tests pass, and that the PHPUnit tests
pass with both MySQL and SQLite, except for the known driver issues. Update the
driver version wherever it's mentioned, including this PR's description. Then
force-push the branch.

Trac ticket: https://core.trac.wordpress.org/ticket/57793

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code, ChatGPT
Model(s): Claude Opus 5.5, Astra 6
Used for: Implementation, testing, reviews, and descriptions under my direction.

Note: See TracTickets for help on using tickets.