Skip to content

Commit e61736e

Browse files
authored
Add a database name fallback for early adopters of the new driver (#226)
The experimental new SQLite driver requires a database name to be set, while the old one doesn't. There are some scenarios in which the `DB_NAME` constant is missing in `wp-config.php`. To enable easier early adoption and testing before version 3.0, we're adding a default database name in case the `DB_NAME` constant is not set. Since the provided database name is now verified against the existing database, this should be a safe change.
1 parent 977a813 commit e61736e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

wp-includes/sqlite/db.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@
5555
require_once __DIR__ . '/class-wp-sqlite-db.php';
5656
require_once __DIR__ . '/install-functions.php';
5757

58+
/**
59+
* The DB_NAME constant is required by the new SQLite driver.
60+
*
61+
* There are some existing projects in which the DB_NAME constant is missing in
62+
* wp-config.php. To enable easier early adoption and testing of the new SQLite
63+
* driver, let's allow using a default database name when DB_NAME is not set.
64+
*
65+
* TODO: For version 3.0, enforce the DB_NAME constant and remove the fallback.
66+
*/
67+
if ( defined( 'DB_NAME' ) && '' !== DB_NAME ) {
68+
$db_name = DB_NAME;
69+
} else {
70+
$db_name = apply_filters( 'wp_sqlite_default_db_name', 'database_name_here' );
71+
}
72+
5873
/*
5974
* Debug: Cross-check with MySQL.
6075
* This is for debugging purpose only and requires files
@@ -64,9 +79,9 @@
6479
$crosscheck_tests_file_path = dirname( __DIR__, 2 ) . '/tests/class-wp-sqlite-crosscheck-db.php';
6580
if ( defined( 'SQLITE_DEBUG_CROSSCHECK' ) && SQLITE_DEBUG_CROSSCHECK && file_exists( $crosscheck_tests_file_path ) ) {
6681
require_once $crosscheck_tests_file_path;
67-
$GLOBALS['wpdb'] = new WP_SQLite_Crosscheck_DB( DB_NAME );
82+
$GLOBALS['wpdb'] = new WP_SQLite_Crosscheck_DB( $db_name );
6883
} else {
69-
$GLOBALS['wpdb'] = new WP_SQLite_DB( defined( 'DB_NAME' ) ? DB_NAME : '' );
84+
$GLOBALS['wpdb'] = new WP_SQLite_DB( $db_name );
7085

7186
// Boot the Query Monitor plugin if it is active.
7287
require_once dirname( __DIR__, 2 ) . '/integrations/query-monitor/boot.php';

0 commit comments

Comments
 (0)