Skip to content

Commit 75544c2

Browse files
authored
Add SQLite database application ID and new consistent file extension (#329)
This PR adds the `3948349` integer as the [SQLite Application ID](https://sqlite.org/fileformat.html#application_id) that is standard procedure to take when a SQLite database is used as an application file format[^1]. A new consistent file extension has been chosen. 1. `PRAGMA application_id = SQLITE_DB_APPLICATION_ID` is set for new and old databases 2. New databases are created with the new file extension 3. Legacy files are renamed for backward compatibility [^1]: https://sqlite.org/appfileformat.html
2 parents d87bc8f + c4a9241 commit 75544c2

6 files changed

Lines changed: 58 additions & 12 deletions

File tree

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
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

tests/WP_SQLite_Driver_Tests.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public function setUp(): void {
1515
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
1616

1717
$this->engine = new WP_SQLite_Driver(
18-
new WP_SQLite_Connection( array( 'pdo' => $this->sqlite ) ),
18+
new WP_SQLite_Connection(
19+
array(
20+
'pdo' => $this->sqlite,
21+
'application_id' => 42, // Test application ID.
22+
)
23+
),
1924
'wp'
2025
);
2126
$this->engine->query(
@@ -51,6 +56,11 @@ private function assertQueryError( $sql, $error_message ) {
5156
$this->assertSame( $error_message, $exception->getMessage() );
5257
}
5358

59+
public function testApplicationID() {
60+
$app_id = $this->sqlite->query( 'PRAGMA application_id' )->fetchColumn();
61+
$this->assertSame( 42, (int) $app_id );
62+
}
63+
5464
public function testRegexp() {
5565
$this->assertQuery(
5666
"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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class WP_SQLite_Connection {
6363
* @type int|null $timeout Optional. SQLite timeout in seconds.
6464
* The time to wait for a writable lock.
6565
* @type string|null $journal_mode Optional. SQLite journal mode.
66+
* @type int|null $application_id Optional. SQLite application ID.
6667
* }
6768
*
6869
* @throws InvalidArgumentException When some connection options are invalid.
@@ -96,6 +97,10 @@ public function __construct( array $options ) {
9697
if ( $journal_mode && in_array( $journal_mode, self::SQLITE_JOURNAL_MODES, true ) ) {
9798
$this->query( 'PRAGMA journal_mode = ' . $journal_mode );
9899
}
100+
101+
if ( isset( $options['application_id'] ) ) {
102+
$this->query( 'PRAGMA application_id = ' . (int) $options['application_id'] );
103+
}
99104
}
100105

101106
/**

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class WP_SQLite_DB extends wpdb {
3030
*/
3131
private $allow_unsafe_unquoted_parameters = true;
3232

33+
/**
34+
* The application ID for the SQLite database.
35+
*
36+
* @see https://www.sqlite.org/pragma.html#pragma_application_id
37+
*/
38+
const SQLITE_DB_APPLICATION_ID = 3948349;
39+
3340
/**
3441
* Connects to the SQLite database.
3542
*
@@ -306,6 +313,28 @@ public function db_connect( $allow_bail = true ) {
306313
if ( isset( $GLOBALS['@pdo'] ) ) {
307314
$pdo = $GLOBALS['@pdo'];
308315
}
316+
317+
// Migrate the database file from the legacy default name (".ht.sqlite") to
318+
// the new default name (".ht.sqlite.php"). This only runs when using the
319+
// default file name and the new file does not already exist.
320+
if ( ! defined( 'DB_FILE' ) && ! file_exists( FQDB ) ) {
321+
$old_db_path = FQDBDIR . '.ht.sqlite';
322+
323+
if ( file_exists( $old_db_path ) ) {
324+
if ( ! rename( $old_db_path, FQDB ) ) {
325+
wp_die( 'Failed to rename database file.', 'Error!' );
326+
}
327+
328+
foreach ( array( '-wal', '-shm' ) as $suffix ) {
329+
if ( file_exists( $old_db_path . $suffix ) ) {
330+
if ( ! rename( $old_db_path . $suffix, FQDB . $suffix ) ) {
331+
wp_die( 'Failed to rename database file.', 'Error!' );
332+
}
333+
}
334+
}
335+
}
336+
}
337+
309338
if ( defined( 'WP_SQLITE_AST_DRIVER' ) && WP_SQLITE_AST_DRIVER ) {
310339
if ( null === $this->dbname || '' === $this->dbname ) {
311340
$this->bail(
@@ -321,9 +350,10 @@ public function db_connect( $allow_bail = true ) {
321350
try {
322351
$connection = new WP_SQLite_Connection(
323352
array(
324-
'pdo' => $pdo,
325-
'path' => FQDB,
326-
'journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
353+
'pdo' => $pdo,
354+
'path' => FQDB,
355+
'journal_mode' => defined( 'SQLITE_JOURNAL_MODE' ) ? SQLITE_JOURNAL_MODE : null,
356+
'application_id' => self::SQLITE_DB_APPLICATION_ID,
327357
)
328358
);
329359
$this->dbh = new WP_SQLite_Driver( $connection, $this->dbname );

0 commit comments

Comments
 (0)