Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/common/lib/sqlite-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import path from 'path';
// SQLite-compatible drop-ins are preserved.
const STOCK_DB_DROPIN_MARKER = 'This file is auto-generated and copied from the sqlite plugin.';

function generateDbDropinContent( template: string, sqliteDirname: string ): string {
const updatedContent = template.replace(
"'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",
`realpath( __DIR__ . '/mu-plugins/${ sqliteDirname }' )`
);
const dbPathDefinition = `// Studio keeps the SQLite database at a fixed path (wp-content/database/.ht.sqlite).
if ( ! defined( 'DB_PATH' ) && ! defined( 'FQDB' ) && ! defined( 'DB_DIR' ) && ! defined( 'DB_FILE' ) ) {
\tdefine( 'DB_PATH', __DIR__ . '/database/.ht.sqlite' );
}
`;
return updatedContent.replace(
/^<\?php\s*/,
( openingTag ) => `${ openingTag }${ dbPathDefinition }`
);
}

// Abstract base class for SQLite integration across different contexts
export abstract class SqliteIntegrationProvider {
abstract getSqliteDirname(): string;
Expand Down Expand Up @@ -100,10 +116,7 @@ export abstract class SqliteIntegrationProvider {
path.join( sqliteSourcePath, 'db.copy' ),
'utf8'
);
const updatedContent = dbCopyContent.replace(
"'{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",
`realpath( __DIR__ . '/mu-plugins/${ sqliteDirname }' )`
);
const updatedContent = generateDbDropinContent( dbCopyContent, sqliteDirname );
await fs.promises.writeFile( path.join( wpContentPath, 'db.php' ), updatedContent );
}

Expand Down
45 changes: 45 additions & 0 deletions packages/common/lib/tests/sqlite-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,58 @@ platformTestSuite( 'SqliteIntegrationProvider', ( { normalize } ) => {
} );

describe( 'installSqliteIntegration', () => {
// Shaped like the plugin's db.copy: header with the stock marker, then the loader.
const STOCK_DB_COPY = [
'<?php',
'/**',
' * Plugin Name: SQLite integration (Drop-in)',
' *',
' * This file is auto-generated and copied from the sqlite plugin.',
' */',
"define( 'SQLITE_DB_DROPIN_VERSION', '1.8.0' );",
"$sqlite_plugin_implementation_folder_path = '{SQLITE_IMPLEMENTATION_FOLDER_PATH}';",
"require_once $sqlite_plugin_implementation_folder_path . '/wp-includes/sqlite/db.php';",
].join( '\n' );

beforeEach( () => {
volFromJSON( {
[ normalize( `wp-files/${ SQLITE_DIRNAME }/db.copy` ) ]:
"SQLIntegration path: '{SQLITE_IMPLEMENTATION_FOLDER_PATH}'",
} );
} );

it( 'should define a fixed DB_PATH before the plugin loads', async () => {
volFromJSON( {
[ normalize( `wp-files/${ SQLITE_DIRNAME }/db.copy` ) ]: STOCK_DB_COPY,
} );

await provider.installSqliteIntegration( MOCK_SITE_PATH );

const generated = vi.mocked( fs.promises.writeFile ).mock.calls.at( -1 )?.[ 1 ] as string;
const definition = generated.indexOf(
"if ( ! defined( 'DB_PATH' ) && ! defined( 'FQDB' ) && ! defined( 'DB_DIR' ) && ! defined( 'DB_FILE' ) ) {\n\tdefine( 'DB_PATH', __DIR__ . '/database/.ht.sqlite' );\n}"
);
expect( generated.startsWith( '<?php\n' ) ).toBe( true );
expect( definition ).toBeGreaterThan( 0 );
expect( definition ).toBeLessThan(
generated.indexOf( 'require_once $sqlite_plugin_implementation_folder_path' )
);
} );

it( 'should keep the generated drop-in recognizable as the stock Studio drop-in', async () => {
volFromJSON( {
[ normalize( `wp-files/${ SQLITE_DIRNAME }/db.copy` ) ]: STOCK_DB_COPY,
} );
await provider.installSqliteIntegration( MOCK_SITE_PATH );
const generated = vi.mocked( fs.promises.writeFile ).mock.calls.at( -1 )?.[ 1 ] as string;

volFromJSON( {
[ normalize( `${ MOCK_SITE_PATH }/wp-content/db.php` ) ]: generated,
} );

expect( await provider.shouldReplaceDbDropin( MOCK_SITE_PATH ) ).toBe( true );
} );

it( 'should create database directory', async () => {
await provider.installSqliteIntegration( MOCK_SITE_PATH );

Expand Down
2 changes: 1 addition & 1 deletion scripts/download-wp-server-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { z } from 'zod';
import { extractZip } from '../packages/common/lib/extract-zip.ts';
import { fetch, sharedDispatcher, throwForHttpStatus, withRetry } from './lib/with-retry.ts';

const SQLITE_DATABASE_INTEGRATION_VERSION = 'v3.0.2';
const SQLITE_DATABASE_INTEGRATION_VERSION = 'v3.1.0';
const SQLITE_DATABASE_INTEGRATION_RELEASE_URL = `https://github.com/WordPress/sqlite-database-integration/releases/download/${ SQLITE_DATABASE_INTEGRATION_VERSION }/plugin-sqlite-database-integration.zip`;

async function fetchWithRetry( name: string, url: string ): Promise< Buffer > {
Expand Down
Loading