Skip to content

Conversation

@sejas
Copy link

@sejas sejas commented Oct 3, 2025

Because filters actually don’t work in db.php, I suggest removing the wp_sqlite_default_db_name filter and introducing a new constant WP_SQLITE_DB_NAME to take precedence over DB_NAME.

  • Break your database connection:
    • Edit your wp-config.php
    • Copy the value of your DB_NAME
    • Change it to any value to break your database connection
    • Access the homepage of your site and observe the error: Error establishing a database connection
  • Fix your database connection using the new constant:
    • Edit your wp-config.php
    • Add the new constant WP_SQLITE_DB_NAME with the value you copied before. Example define( 'WP_SQLITE_DB_NAME', 'database_name_here' );
    • Access your site and observe it loads correctly

Credit to @wojtekn for the code. I just removed the filter as it seemed misleading.

@sejas
Copy link
Author

sejas commented Oct 3, 2025

@JanJakes , what do you think about using this constant to override the DB_NAME?

* TODO: For version 3.0, enforce the DB_NAME constant and remove the fallback.
*/
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
if ( defined( 'WP_SQLITE_DB_NAME' ) && '' !== WP_SQLITE_DB_NAME ) {
Copy link
Author

Choose a reason for hiding this comment

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

I added the prefix WP_SQLITE_ to be consistent with other constants required by this plugin, such as WP_SQLITE_AST_DRIVER.

@sejas sejas changed the title Add SQLITE_DB_NAME constant to take precedence over DB_NAME Add WP_SQLITE_DB_NAME constant to take precedence over DB_NAME Oct 3, 2025
@sejas
Copy link
Author

sejas commented Oct 6, 2025

Copy link

@epeicher epeicher left a comment

Choose a reason for hiding this comment

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

Thanks @sejas for improving this! I’ve tested it, and can confirm that after breaking the connection, applying these changes, and adding the new constant, the site works again.

This LGTM, so I’m accepting the PR, but leaving it to you or someone from the team to decide whether to merge it.

Database name incorrect Adding the constant Refreshing the site
Image Image Image

@JanJakes JanJakes mentioned this pull request Oct 14, 2025
@JanJakes
Copy link
Member

Closing this in favor of #266.

@JanJakes JanJakes closed this Oct 15, 2025
JanJakes added a commit that referenced this pull request Oct 15, 2025
This PR replaces
#262. It is
a simpler solution to the same problem:

When a table reference targets an information schema table, we replace
it with a subquery, injecting the configured database name dynamically:

```sql
-- The following query:
SELECT *, t.*, t.table_schema FROM information_schema.tables t

-- Will be translated to:
SELECT *, t.*, t.table_schema FROM (
  SELECT
    `TABLE_CATALOG`,
    IIF(`TABLE_SCHEMA` = 'information_schema', `TABLE_SCHEMA`, 'database_name') AS `TABLE_SCHEMA`,
    `TABLE_NAME`,
    ...
  FROM _wp_sqlite_mysql_information_schema_tables AS tables
) t
```
The same logic will be applied to table references in JOIN clauses as
well.

---

With the new SQLite driver, we keep running into issues with missing and
incorrect database name values
(#197,
#203,
#226,
#260,
#261, and
other issues).

Thinking through this further, I came to the conclusion that to provide
maximum flexibility and portability, it would be best to provide an API
in the shape of:

_"Mount `my-sqlite-file.sqlite` as `my-db-name`."_

Even if we consider adding multi-database support one day, it would
still be great to allow mounting additional SQLite files under dynamic
database names.

I don't see any downsides to doing this, except maybe that we'll have to
choose some database name in some scenarios, such as in database admin
tools. However, in these cases we can use a reasonable default or the
file name.

This is a WIP pull request demonstrating the simplest approach. For it
to be merged, I only need to resolve how the existing database name
values should be treated.
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