Skip to content

Commit

Permalink
Merge pull request #1682 from Automattic/forno-1719/fix/export-sql-pr…
Browse files Browse the repository at this point in the history
…ogress-tracking
  • Loading branch information
abdullah-kasim authored Feb 8, 2024
2 parents 5414f32 + 53973ba commit c4edeba
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
2 changes: 1 addition & 1 deletion __tests__/commands/export-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe( 'commands/ExportSQLCommand', () => {
const confirmEnoughStorageSpy = jest.spyOn( exportCommand, 'confirmEnoughStorage' );

beforeAll( () => {
confirmEnoughStorageSpy.mockResolvedValue( true );
confirmEnoughStorageSpy.mockResolvedValue( { continue: true, isPromptShown: false } );
downloadSpy.mockResolvedValue( 'test-backup.sql.gz' );
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe( 'backup-storage-availability', () => {

await expect(
backupStorageAvailability.validateAndPromptDiskSpaceWarningForBackupImport()
).resolves.toBe( true );
).resolves.toStrictEqual( { continue: true, isPromptShown: true } );

expect( confirmRunSpy ).toHaveBeenCalled();
} );
Expand All @@ -46,7 +46,7 @@ describe( 'backup-storage-availability', () => {

await expect(
backupStorageAvailability.validateAndPromptDiskSpaceWarningForBackupImport()
).resolves.toBe( true );
).resolves.toStrictEqual( { continue: true, isPromptShown: false } );

expect( confirmRunSpy ).not.toHaveBeenCalled();
} );
Expand All @@ -73,7 +73,7 @@ describe( 'backup-storage-availability', () => {

await expect(
backupStorageAvailability.validateAndPromptDiskSpaceWarningForDevEnvBackupImport()
).resolves.toBe( true );
).resolves.toStrictEqual( { continue: true, isPromptShown: true } );

expect( confirmRunSpy ).toHaveBeenCalledTimes( timesPrompted );
}
Expand All @@ -92,7 +92,7 @@ describe( 'backup-storage-availability', () => {

await expect(
backupStorageAvailability.validateAndPromptDiskSpaceWarningForDevEnvBackupImport()
).resolves.toBe( false );
).resolves.toStrictEqual( { continue: false, isPromptShown: true } );

expect( confirmRunSpy ).toHaveBeenCalledTimes( 1 );
} );
Expand All @@ -114,7 +114,7 @@ describe( 'backup-storage-availability', () => {

await expect(
backupStorageAvailability.validateAndPromptDiskSpaceWarningForDevEnvBackupImport()
).resolves.toBe( true );
).resolves.toStrictEqual( { continue: true, isPromptShown: Boolean( timesPrompted ) } );

expect( confirmRunSpy ).toHaveBeenCalledTimes( timesPrompted );
}
Expand All @@ -132,7 +132,7 @@ describe( 'backup-storage-availability', () => {

await expect(
backupStorageAvailability.validateAndPromptDiskSpaceWarningForDevEnvBackupImport()
).resolves.toBe( true );
).resolves.toStrictEqual( { continue: true, isPromptShown: false } );

expect( confirmRunSpy ).not.toHaveBeenCalled();
} );
Expand Down
13 changes: 10 additions & 3 deletions src/commands/export-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,16 @@ export class ExportSQLCommand {
);
this.progressTracker.stepSuccess( this.steps.CREATE );

const storageConfirmed = await this.progressTracker.handleContinuePrompt( async () => {
return await this.confirmEnoughStorage( await this.getExportJob() );
}, 3 );
const storageConfirmed = await this.progressTracker.handleContinuePrompt(
async setPromptShown => {
const status = await this.confirmEnoughStorage( await this.getExportJob() );
if ( status.isPromptShown ) {
setPromptShown();
}

return status.continue;
}
);

if ( storageConfirmed ) {
this.progressTracker.stepSuccess( this.steps.CONFIRM_ENOUGH_STORAGE );
Expand Down
46 changes: 38 additions & 8 deletions src/lib/backup-storage-availability/backup-storage-availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { formatMetricBytes } from '../cli/format';

const oneGiBInBytes = 1024 * 1024 * 1024;

export interface PromptStatus {
continue: boolean;
isPromptShown: boolean;
}

export class BackupStorageAvailability {
archiveSize: number;

Expand Down Expand Up @@ -86,7 +91,7 @@ export class BackupStorageAvailability {
}

// eslint-disable-next-line id-length
async validateAndPromptDiskSpaceWarningForBackupImport(): Promise< boolean > {
async validateAndPromptDiskSpaceWarningForBackupImport(): Promise< PromptStatus > {
const isStorageAvailable =
( await this.getStorageAvailableInVipPath() ) > this.getArchiveSize();
if ( ! isStorageAvailable ) {
Expand All @@ -97,16 +102,25 @@ export class BackupStorageAvailability {
) } of free space in your machine to download this database backup. Do you still want to continue with downloading the database backup?`,
} );

return await confirmPrompt.run();
return {
continue: await confirmPrompt.run(),
isPromptShown: true,
};
}

return true;
return {
continue: true,
isPromptShown: false,
};
}

// eslint-disable-next-line id-length
async validateAndPromptDiskSpaceWarningForDevEnvBackupImport(): Promise< boolean > {
async validateAndPromptDiskSpaceWarningForDevEnvBackupImport(): Promise< PromptStatus > {
let storageAvailableInMainMachinePrompted = false;

// there's two prompts, so as long as one prompt is shown, we need to set isPromptShown
let isPromptShown = false;

if ( ! ( await this.isStorageAvailableInMainMachine() ) ) {
const storageRequired = this.getStorageRequiredInMainMachine();
const storageAvailableInVipPath = this.bytesToHuman(
Expand All @@ -121,10 +135,15 @@ Do you still want to continue with importing the database backup?
`,
} );

isPromptShown = true;

storageAvailableInMainMachinePrompted = await confirmPrompt.run();

if ( ! storageAvailableInMainMachinePrompted ) {
return false;
return {
continue: false,
isPromptShown,
};
}
}

Expand All @@ -141,17 +160,28 @@ Do you still want to continue with importing the database backup?
Do you still want to continue with importing the database backup?`,
} );

return await confirmPrompt.run();
isPromptShown = true;

return {
continue: await confirmPrompt.run(),
isPromptShown,
};
}
} catch ( error ) {
if ( error instanceof DockerMachineNotFoundError ) {
// skip storage available check
return true;
return {
continue: true,
isPromptShown,
};
}

throw error;
}

return true;
return {
continue: true,
isPromptShown,
};
}
}
23 changes: 17 additions & 6 deletions src/lib/cli/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,24 @@ export class ProgressTracker {
}

async handleContinuePrompt< PromptReturn >(
prompt: () => Promise< PromptReturn >
prompt: ( setPromptShown: () => void ) => Promise< PromptReturn >
): Promise< PromptReturn > {
this.print();
this.stopPrinting();

const returnValue = await prompt();
this.displayFromStep = [ ...this.getSteps().values() ].findIndex(
step => step.status === StepStatus.RUNNING
);
let isPromptShown = false;

const setPromptShown = () => {
isPromptShown = true;
};

const returnValue = await prompt( setPromptShown );

if ( isPromptShown ) {
this.displayFromStep = [ ...this.getSteps().values() ].findIndex(
step => step.status === StepStatus.RUNNING
);
}
let hasPrintedOnce = false;

const printingStartedPromise = new Promise< void >( resolve => {
Expand All @@ -218,7 +227,9 @@ export class ProgressTracker {
linesToSkip += EOL;
}

process.stdout.write( linesToSkip );
if ( isPromptShown ) {
process.stdout.write( linesToSkip );
}

hasPrintedOnce = true;

Expand Down

0 comments on commit c4edeba

Please sign in to comment.