Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support compressed files that contain a SQL file in importer #363

Merged
merged 2 commits into from
Jul 18, 2024

Conversation

fluiddot
Copy link
Contributor

@fluiddot fluiddot commented Jul 16, 2024

Related to 8282-gh-Automattic/dotcom-forge.

Proposed Changes

  • I noticed when creating compressed files on macOS that hidden/meta files were also included. To exclude them in the import, I added the function isFileAllowed that is used in the different backup handlers to filter those files out. It's important to clean up the backup content when importing SQL-only files, otherwise, the following condition won't be met and the import will fail.
  • Update unit tests of listFiles to cover the changes, plus fix the issue spotted here.

return fileList.length === 1 && fileList[ 0 ].endsWith( '.sql' );

Testing Instructions

  • Apply the following patch:
diff --git forkSrcPrefix/src/lib/import-export/import/importers/importer.ts forkDstPrefix/src/lib/import-export/import/importers/importer.ts
index 4ce10b9bc24f5a7193b49ad7b62dbf4ed70bbea9..95a12b57b3e3711be8caa252dddf3193cef3ef33 100644
--- forkSrcPrefix/src/lib/import-export/import/importers/importer.ts
+++ forkDstPrefix/src/lib/import-export/import/importers/importer.ts
@@ -35,6 +35,7 @@ export class DefaultImporter implements Importer {
 
 	protected async importDatabase(): Promise< void > {
 		// will implement in a different ticket
+		console.log( 'Trying to import the following SQL files:', this.backup.sqlFiles );
 	}
 
 	protected async importWpContent( rootPath: string ): Promise< void > {
diff --git forkSrcPrefix/src/components/content-tab-import-export.tsx forkDstPrefix/src/components/content-tab-import-export.tsx
index 436c8bcc281cb9f10501e05e2c965da7e0e0ac27..30acc63733ccf9fb6eab365e1483b1ca31a1c501 100644
--- forkSrcPrefix/src/components/content-tab-import-export.tsx
+++ forkDstPrefix/src/components/content-tab-import-export.tsx
@@ -1,6 +1,46 @@
+import { useEffect, useState } from 'react';
+import { getIpcApi } from '../lib/get-ipc-api';
+import { BackupArchiveInfo } from '../lib/import-export/import/types';
+
 interface ContentTabImportExportProps {
 	selectedSite: SiteDetails;
 }
-export function ContentTabImportExport( _props: ContentTabImportExportProps ) {
-	return null;
+export function ContentTabImportExport( { selectedSite }: ContentTabImportExportProps ) {
+	const [ file, setFile ] = useState< File | null >( null );
+
+	const handleFileChange = ( e: React.ChangeEvent< HTMLInputElement > ) => {
+		const selectedFile = e.target.files ? e.target.files[ 0 ] : null;
+		if ( selectedFile ) {
+			setFile( selectedFile );
+		}
+	};
+
+	useEffect( () => {
+		if ( file ) {
+			console.log( `File ready for import: ${ file.name }` );
+		}
+	}, [ file ] );
+
+	const handleSubmit = () => {
+		if ( file ) {
+			try {
+				const backupFile: BackupArchiveInfo = {
+					type: file.type,
+					path: file.path,
+				};
+				getIpcApi().importSite( { id: selectedSite.id, backupFile } );
+			} catch ( error ) {
+				console.error( 'Error importing site:', error );
+			}
+		} else {
+			console.warn( 'No file selected for import' );
+		}
+	};
+
+	return (
+		<div>
+			<input type="file" onChange={ handleFileChange } />
+			<button onClick={ handleSubmit }>Submit</button>
+		</div>
+	);
 }
  • Run the command STUDIO_IMPORT_EXPORT=true npm start
  • Navigate to the tab Import/Export
  • Download and extract the content of the compressed file: backup-files.zip.
  • For each of the extracted files:
    • Select the file and click Submit.
    • Observe in the logs the entry: Trying to import the following SQL files: [ <TEMPORAL_FOLDER>/<PATH_TO_FILE>/wp_posts.sql ].
    • Observe that the path to the SQL file is correct based on the extracted files.

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@fluiddot fluiddot requested a review from a team July 16, 2024 12:00
@fluiddot fluiddot self-assigned this Jul 16, 2024
@fluiddot fluiddot force-pushed the add/sql-import-compress-file branch from 4cf434e to 9e2bde0 Compare July 16, 2024 12:08
Base automatically changed from add/backup_importer to trunk July 16, 2024 13:49
@fluiddot fluiddot force-pushed the add/sql-import-compress-file branch from 9e2bde0 to 343eb45 Compare July 16, 2024 14:47
Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fluiddot , The code looks good, but I'm not able to see the output of the submit button.

When I apply your patch and I run npm start, I see this error in the console:

<e> [ForkTsCheckerWebpackPlugin] ERROR in ./src/lib/import-export/import/importers/index.ts:1:15
<e> TS1261: Already included file name '/Users/macbookpro/Documents/projects/a8c/studio/src/lib/import-export/import/importers/importer.ts' differs from file name '/Users/macbookpro/Documents/projects/a8c/studio/src/lib/import-export/import/importers/Importer.ts' only in casing.
<e>   The file is in the program because:
<e>     Imported via './importer' from file '/Users/macbookpro/Documents/projects/a8c/studio/src/lib/import-export/import/importers/index.ts'
<e>     Imported via './importers/importer' from file '/Users/macbookpro/Documents/projects/a8c/studio/src/lib/import-export/import/import-manager.ts'
<e>     Imported via '../../import/importers/importer' from file '/Users/macbookpro/Documents/projects/a8c/studio/src/lib/import-export/tests/import/import-manager.test.ts'
<e>     Root file specified for compilation
<e>   > 1 | export * from './importer';

And here is the output.

WErVgS.mp4

@fluiddot
Copy link
Contributor Author

@fluiddot , The code looks good, but I'm not able to see the output of the submit button.

When I apply your patch and I run npm start, I see this error in the console:

Oh, let me take a look. Maybe something got broken when solving conflicts after rebasing trunk.

@fluiddot
Copy link
Contributor Author

@sejas I've updated the patch in the PR's description. I forgot that one of the files was renamed 😅. Let me know if now works. Thanks 🙇 !

@fluiddot fluiddot requested a review from sejas July 18, 2024 07:46
Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fluiddot , thanks for providing an easy way to test the PR.

I confirm it works as expected.
https://github.com/user-attachments/assets/4eeeac09-4d7f-4c88-8d5e-aea4c8e5f6ea

@@ -8,6 +8,16 @@ export interface BackupHandler {
extractFiles( file: BackupArchiveInfo, extractionDirectory: string ): Promise< void >;
}

const EXCLUDED_FILES_PATTERNS = [
/^__MACOSX\/.*/, // MacOS meta folder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we exclude /^\.DS_Store$/ too ? Or is that already covered by /^\..*/, // Unix hidden files at root?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. As you pointed out, we don't need to explicitly exclude .DS_Store as it will be covered by that pattern. In fact, the compressed files I added in backup-files.zip contain that hidden folder and are not being processed.

@fluiddot fluiddot merged commit 171ee77 into trunk Jul 18, 2024
11 checks passed
@fluiddot fluiddot deleted the add/sql-import-compress-file branch July 18, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants