Skip to content
Closed
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
42 changes: 42 additions & 0 deletions apps/cli/commands/tests/import.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
import fs from 'fs';
import path from 'path';
import * as tar from 'tar';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import {
cleanupCliEnv,
Expand Down Expand Up @@ -187,4 +188,45 @@ describe.skipIf( ! cliE2ePrerequisitesMet() )( 'CLI e2e: studio import', () => {
expect( await getBlogname( env, sitePath ) ).toBe( FIXTURE_BLOGNAME );
}
);

// Regression test for https://github.com/Automattic/studio/issues/3518
// where db.php overwrites SQLite drop-in during restore causing database import error
// "Could not determine the version of the SQLite integration plugin".
it(
'imports a backup containing db.php drop-in',
{ tags: [ 'e2e' ], timeout: 300_000 },
async () => {
if ( ! env ) {
throw new Error( 'CLI e2e env was not initialised' );
}

const workDir = path.join( env.root, 'db-php' );
const cwd = path.join( workDir, 'contents' );
fs.mkdirSync( cwd, { recursive: true } );
await tar.x( { file: path.join( FIXTURES_DIR, 'jetpack-backup.tar.gz' ), cwd } );

fs.writeFileSync(
path.join( cwd, 'wp-content', 'db.php' ),
'<?php\n/**\n * Plugin Name: Query Monitor Database Class (Drop-in)\n */\nclass QM_DB extends wpdb {}\n'
);

const file = path.join( workDir, 'jetpack-backup-db-php-replaced.tar.gz' );
await tar.c( { file, cwd, gzip: true }, fs.readdirSync( cwd ) );
const sitePath = await createStoppedSite(
env,
'Foreign DB Import E2E Site',
'import-db-php'
);

const result = await runCli( [ 'import', file, '--path', sitePath ], env );
expect( result.code, result.stderr ).toBe( 0 );

const db = fs.readFileSync( path.join( sitePath, 'wp-content', 'db.php' ), 'utf8' );
expect( db ).toContain( 'SQLITE_DB_DROPIN_VERSION' );
expect( db ).not.toContain( 'QM_DB' );

assertImportedSiteOnDisk( sitePath );
expect( await getBlogname( env, sitePath ) ).toBe( FIXTURE_BLOGNAME );
}
);
} );
4 changes: 4 additions & 0 deletions apps/cli/lib/import-export/import/importers/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import semver from 'semver';
import trash from 'trash';
import { SiteData } from 'cli/lib/cli-config/core';
import { runWpCliCommand } from 'cli/lib/run-wp-cli-command';
import { installSqliteIntegration } from 'cli/lib/sqlite-integration';
import { ImportExportEventEmitter } from '../../events';
import { BackupContents, MetaFileData } from '../types';
import { updateSiteUrl } from '../update-site-url';
Expand Down Expand Up @@ -77,6 +78,9 @@ abstract class BaseImporter extends ImportExportEventEmitter implements Importer
let processedFiles = 0;
const totalFiles = sortedSqlFiles.length;

// db.php may be non-SQLite drop-in from backup
await installSqliteIntegration( site.path );

for ( const sqlFile of sortedSqlFiles ) {
const sqlTempFile = `${ generateBackupFilename( 'sql' ) }.sql`;
const tmpPath = path.join( site.path, sqlTempFile );
Expand Down
Loading