diff --git a/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts b/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts index 5a6ef0c782..964fa59da5 100644 --- a/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts +++ b/packages/core/data-transfer/src/strapi/providers/local-destination/index.ts @@ -78,7 +78,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider { } } - async #deleteAll() { + async #deleteFromRestoreOptions() { assertValidStrapi(this.strapi); return restore.deleteRecords(this.strapi, this.options.restore); } @@ -117,11 +117,15 @@ class LocalStrapiDestinationProvider implements IDestinationProvider { } await this.transaction?.attach(async (trx) => { - await this.#handleAssetsBackup(); try { if (this.options.strategy === 'restore') { - await this.#deleteAllAssets(trx); - await this.#deleteAll(); + if (this.options.restore?.assets) { + // Note: when other strategies are added, they will need to add their own backup logic + await this.#handleAssetsBackup(); + await this.#deleteAllAssets(trx); + } + // TODO: do we need to add trx? + await this.#deleteFromRestoreOptions(); } } catch (error) { throw new Error(`restore failed ${error}`); @@ -215,6 +219,7 @@ class LocalStrapiDestinationProvider implements IDestinationProvider { } async #removeAssetsBackup() { + // TODO: this should catch all thrown errors and bubble it up to engine so it can be reported as a non-fatal diagnostic message telling the user they may need to manually delete assets if (strapi.config.get('plugin.upload').provider === 'local') { assertValidStrapi(this.strapi); const backupDirectory = path.join( @@ -233,11 +238,15 @@ class LocalStrapiDestinationProvider implements IDestinationProvider { const strapi = this.strapi; const transaction = this.transaction; const backupDirectory = this.uploadsBackupDirectoryName; + const restoreAssets = this.options?.restore?.assets; + return new Writable({ objectMode: true, async final(next) { - // Deletes the backup folder - removeAssetsBackup(); + if (restoreAssets) { + // Delete the backup folder + removeAssetsBackup(); + } next(); }, async write(chunk: IAsset, _encoding, callback) { diff --git a/packages/core/data-transfer/src/strapi/providers/local-destination/strategies/restore/index.ts b/packages/core/data-transfer/src/strapi/providers/local-destination/strategies/restore/index.ts index e059f29dcd..ae54729166 100644 --- a/packages/core/data-transfer/src/strapi/providers/local-destination/strategies/restore/index.ts +++ b/packages/core/data-transfer/src/strapi/providers/local-destination/strategies/restore/index.ts @@ -22,7 +22,7 @@ interface IDeleteResults { } export const deleteRecords = async (strapi: Strapi.Strapi, options?: IRestoreOptions) => { - const entities = await deleteEntitiesRecord(strapi, options); + const entities = await deleteEntitiesRecords(strapi, options); const configuration = await deleteConfigurationRecords(strapi, options); return { @@ -32,7 +32,7 @@ export const deleteRecords = async (strapi: Strapi.Strapi, options?: IRestoreOpt }; }; -const deleteEntitiesRecord = async ( +const deleteEntitiesRecords = async ( strapi: Strapi.Strapi, options: IRestoreOptions = {} ): Promise => {