Skip to content

Commit a9af46b

Browse files
committed
Simplify SQLite installation caching
Use the existing installation-directory copy for SQLite databases. It already includes the database files and db-path.php, including the randomized directories introduced for SQLite Database Integration 3.1. Remove the separate fixed-filename snapshot and its validity and cleanup logic. Cache reuse now depends on the installation directory existing, so an incomplete copy within a suite no longer triggers a rebuild based on the missing snapshot. Each suite still starts with an empty cache. Add a focused regression test for path preservation and independent databases across a cold installation and two warm restores. WordPress/sqlite-database-integration#502 WordPress/sqlite-database-integration#512
1 parent 3384827 commit a9af46b

2 files changed

Lines changed: 43 additions & 57 deletions

File tree

‎features/testing.feature‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,46 @@ Feature: Test that WP-CLI loads.
3737
sqlite
3838
"""
3939

40+
@require-sqlite
41+
Scenario: Restore independent SQLite databases from the installation cache
42+
Given a WP installation in 'cached'
43+
When I run `wp eval 'echo substr( defined( "DB_PATH" ) ? DB_PATH : FQDB, strlen( ABSPATH ) );'` from 'cached'
44+
Then save STDOUT as {DATABASE_PATH}
45+
46+
When I run `php -r "rename('cached', 'first');"`
47+
And I run `wp eval 'update_option( "blogname", "First installation" );'` from 'first'
48+
49+
# Reuse the subdirectory to select the same installation cache.
50+
Given a WP installation in 'cached'
51+
Then the cached/{DATABASE_PATH} file should exist
52+
When I run `wp eval 'echo get_option( "blogname" );'` from 'cached'
53+
Then STDOUT should be:
54+
"""
55+
WP CLI Site
56+
"""
57+
58+
When I run `php -r "rename('cached', 'second');"`
59+
And I run `wp eval 'update_option( "blogname", "Second installation" );'` from 'second'
60+
61+
Given a WP installation in 'cached'
62+
Then the cached/{DATABASE_PATH} file should exist
63+
When I run `wp eval 'echo get_option( "blogname" );'` from 'cached'
64+
Then STDOUT should be:
65+
"""
66+
WP CLI Site
67+
"""
68+
69+
When I run `wp eval 'echo get_option( "blogname" );'` from 'first'
70+
Then STDOUT should be:
71+
"""
72+
First installation
73+
"""
74+
When I run `wp eval 'echo get_option( "blogname" );'` from 'second'
75+
Then STDOUT should be:
76+
"""
77+
Second installation
78+
"""
79+
4080
@require-mysql
4181
Scenario: Uses MySQL
4282
Given a WP install

‎src/Context/FeatureContext.php‎

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,50 +1953,11 @@ public function install_wp( $subdir = '', $version = '' ): void {
19531953

19541954
$install_cache_path = self::$install_cache_dir . '/install_' . md5( implode( ':', $install_args ) . ':subdir=' . $subdir . ':object_cache=' . getenv( 'WP_CLI_TEST_OBJECT_CACHE' ) );
19551955

1956-
$install_cache_is_valid = is_dir( $install_cache_path )
1957-
&& ( 'sqlite' !== self::$db_type || file_exists( "{$install_cache_path}.sqlite" ) );
1958-
1959-
if ( ! $install_cache_is_valid && file_exists( $install_cache_path ) ) {
1960-
if ( is_dir( $install_cache_path ) ) {
1961-
$iterator = new \RecursiveIteratorIterator(
1962-
new \RecursiveDirectoryIterator( $install_cache_path, \FilesystemIterator::SKIP_DOTS ),
1963-
\RecursiveIteratorIterator::CHILD_FIRST
1964-
);
1965-
foreach ( $iterator as $fileinfo ) {
1966-
if ( $fileinfo->isDir() ) {
1967-
rmdir( $fileinfo->getPathname() );
1968-
} else {
1969-
unlink( $fileinfo->getPathname() );
1970-
}
1971-
}
1972-
rmdir( $install_cache_path );
1973-
} else {
1974-
unlink( $install_cache_path );
1975-
}
1976-
1977-
$sqlite_cache = "{$install_cache_path}.sqlite";
1978-
if ( file_exists( $sqlite_cache ) && is_file( $sqlite_cache ) ) {
1979-
unlink( $sqlite_cache );
1980-
}
1981-
1982-
$sql_cache = "{$install_cache_path}.sql";
1983-
if ( file_exists( $sql_cache ) && is_file( $sql_cache ) ) {
1984-
unlink( $sql_cache );
1985-
}
1986-
}
1987-
if ( $install_cache_is_valid ) {
1956+
// The cache includes the SQLite database, stored under "wp-content/database".
1957+
if ( is_dir( $install_cache_path ) ) {
19881958
self::copy_dir( $install_cache_path, $run_dir );
19891959

1990-
// This is the sqlite equivalent of restoring a database dump in MySQL
1991-
if ( 'sqlite' === self::$db_type ) {
1992-
$sqlite_dest_dir = "$run_dir/wp-content/database";
1993-
if ( ! is_dir( $sqlite_dest_dir ) ) {
1994-
mkdir( $sqlite_dest_dir, 0755, true );
1995-
}
1996-
if ( file_exists( "{$install_cache_path}.sqlite" ) ) {
1997-
copy( "{$install_cache_path}.sqlite", "$sqlite_dest_dir/.ht.sqlite.php" );
1998-
}
1999-
} else {
1960+
if ( 'sqlite' !== self::$db_type ) {
20001961
$ssl_flag = 'mariadb' === self::$db_type ? ' --skip-ssl-verify-server-cert' : '';
20011962
self::run_sql( self::$mysql_binary . ' --no-defaults' . $ssl_flag, [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
20021963
}
@@ -2021,21 +1982,6 @@ public function install_wp( $subdir = '', $version = '' ): void {
20211982
}
20221983
self::run_sql( $command, [ 'result-file' => "{$install_cache_path}.sql" ], true /*add_database*/ );
20231984
}
2024-
2025-
if ( 'sqlite' === self::$db_type ) {
2026-
// This is the sqlite equivalent of creating a database dump in MySQL
2027-
// Support both the new (.ht.sqlite.php) and legacy (.ht.sqlite) file names.
2028-
$sqlite_source = "$run_dir/wp-content/database/.ht.sqlite.php";
2029-
if ( ! file_exists( $sqlite_source ) ) {
2030-
$sqlite_source = "$run_dir/wp-content/database/.ht.sqlite";
2031-
}
2032-
if ( file_exists( $sqlite_source ) ) {
2033-
copy( $sqlite_source, "{$install_cache_path}.sqlite" );
2034-
} elseif ( file_exists( "{$install_cache_path}.sqlite" ) ) {
2035-
// Ensure we don't keep a stale cached SQLite DB if the source wasn't produced
2036-
unlink( "{$install_cache_path}.sqlite" );
2037-
}
2038-
}
20391985
}
20401986
}
20411987

0 commit comments

Comments
 (0)