Skip to content

Commit 4163f69

Browse files
authored
Support DB_PATH as the primary database path constant (#512)
## Summary Add **`DB_PATH`** as the primary SQLite database setting for database connections, schema installation, Site Health, and storage lock placement. It cannot be combined with `DB_DIR`, `DB_FILE`, `FQDBDIR`, or `FQDB`. **Legacy settings remain supported when `DB_PATH` is absent**, but database paths and storage directories must be absolute. Storage under a secret randomized path remains the default. The drop-in then defines `DB_PATH` with the resolved path. Invalid values fail explicitly without falling back to another database. `WP_SQLite_Storage` now exposes `with_secret_path( $root )` and `with_explicit_path( $path )`, with a private constructor. Callers resolve the configuration before creating storage. In-memory databases (`:memory:`) require no filesystem storage or locking. ## Why A single full-path setting makes the active database easier to configure and identify. The older path constants are deprecated in PHPDoc while their behavior remains available for backward compatibility. Builds on #502. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Configure SQLite with an absolute `DB_PATH` or `:memory:`. When `DB_PATH` is not set, SQLite continues to use the default or legacy database path. * Legacy database settings are deprecated and cannot be combined with `DB_PATH`. * **Bug Fixes** * Invalid database paths are rejected rather than silently falling back to a legacy location. * Site-health reporting now reflects the selected database path and its size. * In-memory databases no longer require filesystem locking. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a8468b5 commit 4163f69

7 files changed

Lines changed: 898 additions & 134 deletions

File tree

‎packages/plugin-sqlite-database-integration/constants.php‎

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,58 @@
1313
}
1414

1515
/**
16-
* Notice:
17-
* Your scripts have the permission to create directories or files on your server.
18-
* If you write in your wp-config.php like below, we take these definitions.
19-
* define('DB_DIR', '/full_path_to_the_database_directory/');
20-
* define('DB_FILE', 'database_file_name');
16+
* DB_PATH is the absolute database file path, or ":memory:" for an in-memory database.
17+
*
18+
* Conflicting deprecated constants trigger a warning. DB_PATH takes precedence.
19+
* The database directory is also used for storage locks and must be writable by PHP.
20+
* When not configured, the drop-in defines DB_PATH after initializing the storage.
21+
*
22+
* Example: define( 'DB_PATH', '/private/wordpress/database.sqlite' );
23+
*/
24+
if ( 'sqlite' === DB_ENGINE && defined( 'DB_PATH' ) ) {
25+
if ( ! is_string( DB_PATH ) ) {
26+
throw new RuntimeException( 'DB_PATH must be a string.' );
27+
}
28+
29+
$database_directory = ':memory:' === DB_PATH ? null : rtrim( dirname( DB_PATH ), '/' . DIRECTORY_SEPARATOR );
30+
31+
if ( defined( 'DB_DIR' ) && rtrim( (string) DB_DIR, '/' . DIRECTORY_SEPARATOR ) !== $database_directory ) {
32+
trigger_error( 'DB_DIR conflicts with DB_PATH.', E_USER_WARNING );
33+
}
34+
if ( defined( 'DB_FILE' ) && ( ':memory:' === DB_PATH || DB_FILE !== basename( DB_PATH ) ) ) {
35+
trigger_error( 'DB_FILE conflicts with DB_PATH.', E_USER_WARNING );
36+
}
37+
if ( defined( 'FQDBDIR' ) && rtrim( (string) FQDBDIR, '/' . DIRECTORY_SEPARATOR ) !== $database_directory ) {
38+
trigger_error( 'FQDBDIR conflicts with DB_PATH.', E_USER_WARNING );
39+
}
40+
if ( defined( 'FQDB' ) && FQDB !== DB_PATH ) {
41+
trigger_error( 'FQDB conflicts with DB_PATH.', E_USER_WARNING );
42+
}
43+
unset( $database_directory );
44+
}
45+
46+
/**
47+
* DB_DIR selects the database directory when DB_PATH is not configured.
48+
*
49+
* @deprecated 3.1.0 Define DB_PATH instead.
50+
*/
51+
52+
/**
53+
* DB_FILE selects a filename inside DB_DIR or FQDBDIR.
54+
*
55+
* @deprecated 3.1.0 Define DB_PATH instead.
2156
*/
2257

2358
/**
2459
* FQDBDIR is a directory where the sqlite database file is placed.
25-
* If DB_DIR is defined, it is used as FQDBDIR.
60+
* Defaults to the directory containing DB_PATH, or the legacy directory setting.
2661
*
27-
* @deprecated 3.0.0 Define DB_DIR instead of overriding FQDBDIR.
62+
* @deprecated 3.0.0 Define DB_PATH instead of overriding FQDBDIR.
2863
*/
2964
if ( ! defined( 'FQDBDIR' ) ) {
30-
if ( defined( 'DB_DIR' ) ) {
65+
if ( defined( 'DB_PATH' ) && is_string( DB_PATH ) && ':memory:' !== DB_PATH ) {
66+
define( 'FQDBDIR', rtrim( dirname( DB_PATH ), '/\\' ) . '/' );
67+
} elseif ( ! defined( 'DB_PATH' ) && defined( 'DB_DIR' ) ) {
3168
define( 'FQDBDIR', rtrim( DB_DIR, '/\\' ) . '/' );
3269
} elseif ( defined( 'WP_CONTENT_DIR' ) ) {
3370
define( 'FQDBDIR', WP_CONTENT_DIR . '/database/' );
@@ -39,12 +76,16 @@
3976
/**
4077
* FQDB is the absolute path to the SQLite database file.
4178
*
42-
* If DB_FILE is defined, FQDB is defined here using FQDBDIR. When SQLite is
43-
* used without DB_FILE, managed storage defines FQDB after resolving the
44-
* randomized database path.
79+
* Defaults to DB_PATH, or FQDBDIR combined with DB_FILE. Otherwise, the drop-in
80+
* defines FQDB after resolving the secret database path.
4581
*
46-
* @deprecated 3.0.0 Define DB_DIR and DB_FILE instead of overriding FQDB.
82+
* @deprecated 3.0.0 Define DB_PATH instead of overriding FQDB.
4783
*/
48-
if ( ! defined( 'FQDB' ) && defined( 'DB_FILE' ) ) {
49-
define( 'FQDB', FQDBDIR . DB_FILE );
84+
if ( ! defined( 'FQDB' ) ) {
85+
if ( defined( 'DB_PATH' ) ) {
86+
define( 'FQDB', DB_PATH );
87+
} elseif ( defined( 'DB_FILE' ) ) {
88+
// Avoid compile-time warnings for invalid constants on PHP 7.2.
89+
define( 'FQDB', constant( 'FQDBDIR' ) . constant( 'DB_FILE' ) );
90+
}
5091
}

‎packages/plugin-sqlite-database-integration/health-check.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ function sqlite_plugin_filter_debug_data( $info ) {
3333

3434
$info['wp-database']['fields']['database_file'] = array(
3535
'label' => __( 'Database file', 'sqlite-database-integration' ),
36-
'value' => FQDB,
36+
'value' => DB_PATH,
3737
'private' => true,
3838
);
3939

4040
$info['wp-database']['fields']['database_size'] = array(
4141
'label' => __( 'Database size', 'sqlite-database-integration' ),
42-
'value' => size_format( filesize( FQDB ) ),
42+
'value' => ':memory:' === DB_PATH
43+
? __( 'Not available', 'sqlite-database-integration' )
44+
: size_format( filesize( DB_PATH ) ),
4345
);
4446

4547
unset( $info['wp-database']['fields']['extension'] );

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public function db_connect( $allow_bail = true ) {
447447
$dbh = new WP_MySQL_On_SQLite(
448448
sprintf(
449449
'mysql-on-sqlite:path=%s;dbname=%s',
450-
str_replace( ';', ';;', FQDB ),
450+
str_replace( ';', ';;', DB_PATH ),
451451
str_replace( ';', ';;', $this->dbname )
452452
),
453453
null,

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-storage.php‎

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*
66
* The storage handles the path of the SQLite database:
77
* - An explicit database path is used as is.
8-
* - A managed database is stored under a randomized protected path.
9-
* - A database in a legacy path is migrated to the managed storage.
8+
* - A database with a secret path is stored in a randomized protected directory.
9+
* - A database in a legacy path is migrated to a secret path.
1010
*
1111
* The storage implements a locking mechanism for initialization and maintenance.
1212
* Locking uses a dedicated empty SQLite database and a maintenance file:
@@ -49,9 +49,10 @@ class WP_SQLite_Storage {
4949
private const TOKEN_BYTE_LENGTH = 16;
5050

5151
/**
52-
* Managed database root directory.
52+
* Directory with the storage lock files and, for a secret path, the database.
53+
* Null for an in-memory database.
5354
*
54-
* @var string
55+
* @var string|null
5556
*/
5657
private $database_root;
5758

@@ -65,21 +66,21 @@ class WP_SQLite_Storage {
6566
/**
6667
* Absolute path of the database path file.
6768
*
68-
* @var string
69+
* @var string|null
6970
*/
7071
private $database_path_file;
7172

7273
/**
7374
* Absolute path of the SQLite locking database.
7475
*
75-
* @var string
76+
* @var string|null
7677
*/
7778
private $lock_path;
7879

7980
/**
8081
* Absolute path of the storage maintenance marker.
8182
*
82-
* @var string
83+
* @var string|null
8384
*/
8485
private $maintenance_path;
8586

@@ -105,28 +106,55 @@ class WP_SQLite_Storage {
105106
private $database_lock_timeout = 10000;
106107

107108
/**
108-
* Create a SQLite storage manager.
109+
* Create a storage manager for a database with a secret path.
110+
*
111+
* The database is stored in a randomized directory inside the root directory.
112+
* Legacy databases in the root directory are migrated to it.
113+
*
114+
* @param string $root Root directory of the database storage.
115+
* @return self The storage manager.
116+
* @throws RuntimeException When the root directory is not an absolute path.
117+
*/
118+
public static function with_secret_path( string $root ): self {
119+
if ( ! self::is_absolute_path( $root ) ) {
120+
throw new RuntimeException( 'The SQLite database directory must be an absolute filesystem path.' );
121+
}
122+
123+
$storage = new self();
124+
$storage->set_database_root( $root );
125+
return $storage;
126+
}
127+
128+
/**
129+
* Create a storage manager for an explicit database path.
109130
*
110-
* @param string|null $database_root Managed database root. Defaults to FQDBDIR.
111-
* @param string|null $database_path Explicit database path or ":memory:". Managed storage is used when null.
131+
* @param string $path Absolute database path or ":memory:".
132+
* @return self The storage manager.
112133
* @throws RuntimeException When the database path is invalid.
113134
*/
114-
public function __construct( ?string $database_root = null, ?string $database_path = null ) {
115-
if ( '' === $database_path ) {
116-
throw new RuntimeException( 'The SQLite database path is invalid.' );
135+
public static function with_explicit_path( string $path ): self {
136+
if ( ':memory:' !== $path ) {
137+
if ( ! self::is_absolute_path( $path ) ) {
138+
throw new RuntimeException( 'The SQLite database path must be an absolute filesystem path or ":memory:".' );
139+
}
140+
if ( is_dir( $path ) || in_array( substr( $path, -1 ), array( '/', DIRECTORY_SEPARATOR ), true ) ) {
141+
throw new RuntimeException( 'The SQLite database path must point to a file, not a directory.' );
142+
}
117143
}
118-
$this->database_root = rtrim( $database_root ?? FQDBDIR, '/\\' ) . '/';
119-
$this->database_path = $database_path;
120-
$this->database_path_file = $this->database_root . self::DATABASE_PATH_FILENAME;
121-
$this->lock_path = $this->database_root . self::LOCK_FILENAME;
122-
$this->maintenance_path = $this->database_root . self::MAINTENANCE_FILENAME;
144+
145+
$storage = new self();
146+
$storage->database_path = $path;
147+
if ( ':memory:' !== $path ) {
148+
$storage->set_database_root( dirname( $path ) );
149+
}
150+
return $storage;
123151
}
124152

125153
/**
126154
* Initialize the SQLite database storage.
127155
*
128156
* Uses an explicit file path or ":memory:" as configured. Otherwise, initializes
129-
* managed storage with a randomized path and migrates legacy databases as needed.
157+
* a secret randomized path and migrates legacy databases as needed.
130158
*
131159
* @return string Absolute path to the SQLite database file, or ":memory:".
132160
* @throws RuntimeException When the storage cannot be initialized.
@@ -153,13 +181,13 @@ public function initialize(): string {
153181
return $this->database_path;
154182
}
155183

156-
// Initialized managed database path.
184+
// Initialized secret database path.
157185
$database_path = $this->read_recorded_database_path();
158186
if ( null !== $database_path && @is_file( $database_path ) ) {
159187
return $database_path;
160188
}
161189

162-
// Initialize or repair the managed storage under the storage lock.
190+
// Initialize or repair the secret path under the storage lock.
163191
// Preserve a lock already held by this instance for a larger operation.
164192
$this->lock();
165193
try {
@@ -210,6 +238,11 @@ public function initialize(): string {
210238
* @throws RuntimeException When the lock cannot be acquired.
211239
*/
212240
public function lock(): void {
241+
// An in-memory database has no files to lock.
242+
if ( ':memory:' === $this->database_path ) {
243+
return;
244+
}
245+
213246
$was_locked = null !== $this->storage_lock_connection;
214247

215248
// Serialize maintenance through the dedicated locking database.
@@ -245,7 +278,7 @@ public function lock(): void {
245278
if ( null === $database_path ) {
246279
$database_path = $this->read_recorded_database_path();
247280

248-
// The managed database may still be at a legacy path.
281+
// The database may still be at a legacy path.
249282
if ( null === $database_path || ! @is_file( $database_path ) ) {
250283
$database_path = $this->database_root . self::DATABASE_FILENAME;
251284
}
@@ -282,6 +315,46 @@ public function unlock(): void {
282315
}
283316
}
284317

318+
/**
319+
* Create a storage manager. Use with_secret_path() or with_explicit_path().
320+
*/
321+
private function __construct() {
322+
}
323+
324+
/**
325+
* Set the storage root directory and the paths derived from it.
326+
*
327+
* @param string $database_root Storage root directory.
328+
*/
329+
private function set_database_root( string $database_root ): void {
330+
$this->database_root = rtrim( $database_root, '/\\' ) . '/';
331+
$this->database_path_file = $this->database_root . self::DATABASE_PATH_FILENAME;
332+
$this->lock_path = $this->database_root . self::LOCK_FILENAME;
333+
$this->maintenance_path = $this->database_root . self::MAINTENANCE_FILENAME;
334+
}
335+
336+
/**
337+
* Check whether a path is an absolute filesystem path.
338+
*
339+
* @param string $path Filesystem path.
340+
* @return bool Whether the path is absolute.
341+
*/
342+
private static function is_absolute_path( string $path ): bool {
343+
if ( '' === $path || false !== strpos( $path, "\0" ) ) {
344+
return false;
345+
}
346+
347+
if ( '/' === DIRECTORY_SEPARATOR ) {
348+
return '/' === $path[0];
349+
} else {
350+
// Match Windows absolute path with a drive letter or UNC share.
351+
return 1 === preg_match(
352+
'~^(?:[a-zA-Z]:/|//[^/]+/[^/]+/)~',
353+
str_replace( '\\', '/', $path )
354+
);
355+
}
356+
}
357+
285358
/**
286359
* Wait until no maintenance process holds an active storage lock.
287360
*/
@@ -373,7 +446,7 @@ function_exists( 'opcache_invalidate' )
373446
}
374447

375448
/**
376-
* Move a legacy database into a managed path.
449+
* Move a legacy database into a secret path.
377450
*
378451
* @param string $legacy_path Absolute path of the legacy database file.
379452
* @param string $database_path Absolute destination path.

0 commit comments

Comments
 (0)