Skip to content

Commit 3fa8152

Browse files
committed
Add SQLite database application ID and new consistent file extension
1 parent d87bc8f commit 3fa8152

8 files changed

Lines changed: 53 additions & 8 deletions

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
],
6666
"wp-test-php": [
6767
"@wp-test-ensure-env @no_additional_args",
68-
"rm -rf wordpress/src/wp-content/database/.ht.sqlite @no_additional_args",
68+
"rm -rf wordpress/src/wp-content/database/.ht.sqlite.php @no_additional_args",
6969
"npm --prefix wordpress run test:php -- @additional_args"
7070
],
7171
"wp-test-e2e": [
@@ -74,7 +74,7 @@
7474
],
7575
"wp-test-clean": [
7676
"npm --prefix wordpress run env:clean",
77-
"rm -rf wordpress/src/wp-content/database/.ht.sqlite"
77+
"rm -rf wordpress/src/wp-content/database/.ht.sqlite.php"
7878
]
7979
}
8080
}

constants.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,20 @@
4848
if ( defined( 'DB_FILE' ) ) {
4949
define( 'FQDB', FQDBDIR . DB_FILE );
5050
} else {
51-
define( 'FQDB', FQDBDIR . '.ht.sqlite' );
51+
define( 'FQDB', FQDBDIR . '.ht.sqlite.php' );
5252
}
5353
}
5454

55+
/**
56+
* The application ID identifying an SQLite database created by this driver.
57+
*
58+
* This value is stored in the SQLite database file header and can be used to
59+
* identify databases managed by this driver.
60+
*
61+
* See: https://www.sqlite.org/pragma.html#pragma_application_id
62+
*/
63+
define( 'SQLITE_DB_APPLICATION_ID', 3948349 );
64+
5565
// Allow enabling the SQLite AST driver via environment variable.
5666
if ( ! defined( 'WP_SQLITE_AST_DRIVER' ) && isset( $_ENV['WP_SQLITE_AST_DRIVER'] ) && 'true' === $_ENV['WP_SQLITE_AST_DRIVER'] ) {
5767
define( 'WP_SQLITE_AST_DRIVER', true );

tests/WP_SQLite_Driver_Tests.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ private function assertQueryError( $sql, $error_message ) {
5151
$this->assertSame( $error_message, $exception->getMessage() );
5252
}
5353

54+
public function testApplicationID() {
55+
$app_id = $this->sqlite->query( 'PRAGMA application_id' )->fetchColumn();
56+
$this->assertSame( SQLITE_DB_APPLICATION_ID, (int) $app_id );
57+
}
58+
5459
public function testRegexp() {
5560
$this->assertQuery(
5661
"INSERT INTO _options (option_name, option_value) VALUES ('rss_0123456789abcdef0123456789abcdef', '1');"

tests/WP_SQLite_Translator_Tests.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private function assertQuery( $sql, $error_substring = null ) {
5757
return $retval;
5858
}
5959

60+
public function testApplicationID() {
61+
$app_id = $this->sqlite->query( 'PRAGMA application_id' )->fetchColumn();
62+
$this->assertSame( SQLITE_DB_APPLICATION_ID, (int) $app_id );
63+
}
64+
6065
public function testRegexp() {
6166
$this->assertQuery(
6267
"INSERT INTO _options (option_name, option_value) VALUES ('rss_0123456789abcdef0123456789abcdef', '1');"

tests/bootstrap.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3+
// Configure the test environment.
4+
error_reporting( E_ALL );
5+
define( 'FQDB', ':memory:' );
6+
define( 'FQDBDIR', __DIR__ . '/../testdb' );
7+
38
require_once __DIR__ . '/wp-sqlite-schema.php';
9+
require_once __DIR__ . '/../constants.php';
410
require_once __DIR__ . '/../wp-pdo-mysql-on-sqlite.php';
511
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-query-rewriter.php';
612
require_once __DIR__ . '/../wp-includes/sqlite/class-wp-sqlite-lexer.php';
@@ -14,11 +20,6 @@
1420
define( 'WP_SQLITE_UNSAFE_ENABLE_UNSUPPORTED_VERSIONS', true );
1521
}
1622

17-
// Configure the test environment.
18-
error_reporting( E_ALL );
19-
define( 'FQDB', ':memory:' );
20-
define( 'FQDBDIR', __DIR__ . '/../testdb' );
21-
2223
// Polyfill WPDB globals.
2324
$GLOBALS['table_prefix'] = 'wptests_';
2425
$GLOBALS['wpdb'] = new class() {

wp-includes/sqlite-ast/class-wp-sqlite-connection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public function __construct( array $options ) {
9696
if ( $journal_mode && in_array( $journal_mode, self::SQLITE_JOURNAL_MODES, true ) ) {
9797
$this->query( 'PRAGMA journal_mode = ' . $journal_mode );
9898
}
99+
100+
// Set the application ID to identify this as a WordPress SQLite database.
101+
$this->query( 'PRAGMA application_id = ' . SQLITE_DB_APPLICATION_ID );
99102
}
100103

101104
/**

wp-includes/sqlite/class-wp-sqlite-db.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,24 @@ public function db_connect( $allow_bail = true ) {
306306
if ( isset( $GLOBALS['@pdo'] ) ) {
307307
$pdo = $GLOBALS['@pdo'];
308308
}
309+
310+
// Migrate the database file from the legacy default name (".ht.sqlite") to
311+
// the new default name (".ht.sqlite.php"). This only runs when using the
312+
// default file name and the new file does not already exist.
313+
if ( ! defined( 'DB_FILE' ) && ! file_exists( FQDB ) ) {
314+
$old_db_path = FQDBDIR . '.ht.sqlite';
315+
316+
if ( file_exists( $old_db_path ) ) {
317+
rename( $old_db_path, FQDB );
318+
319+
foreach ( array( '-wal', '-shm' ) as $suffix ) {
320+
if ( file_exists( $old_db_path . $suffix ) ) {
321+
rename( $old_db_path . $suffix, FQDB . $suffix );
322+
}
323+
}
324+
}
325+
}
326+
309327
if ( defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER ) {
310328
if ( null === $this->dbname || '' === $this->dbname ) {
311329
$this->bail(

wp-includes/sqlite/class-wp-sqlite-translator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ public function __construct( $pdo = null ) {
427427
if ( defined( 'SQLITE_JOURNAL_MODE' ) && in_array( SQLITE_JOURNAL_MODE, $valid_journal_modes, true ) ) {
428428
$this->pdo->query( 'PRAGMA journal_mode = ' . SQLITE_JOURNAL_MODE );
429429
}
430+
431+
// Set the application ID to identify this as a WordPress SQLite database.
432+
$this->pdo->query( 'PRAGMA application_id = ' . SQLITE_DB_APPLICATION_ID );
430433
}
431434

432435
/**

0 commit comments

Comments
 (0)