fix(reporters): show number of skipped migrations correctly in command output

This commit is contained in:
Joakim Carlstein 2024-02-05 14:12:39 +01:00 committed by Joakim Carlstein
parent 69bd88afdb
commit ef45be9233
4 changed files with 33 additions and 23 deletions

View file

@ -0,0 +1,6 @@
---
'@emigrate/reporter-pino': patch
'@emigrate/cli': patch
---
Show number of skipped migrations correctly in the command output

View file

@ -50,7 +50,6 @@ export const migrationRunner = async <T extends MigrationMetadata | MigrationMet
onError,
migrationFilter = () => true,
}: MigrationRunnerParameters<T>): Promise<Error | undefined> => {
const collectedMigrations: Array<MigrationMetadata | MigrationMetadataFinished> = [];
const validatedMigrations: Array<MigrationMetadata | MigrationMetadataFinished> = [];
const migrationsToLock: MigrationMetadata[] = [];
@ -93,8 +92,6 @@ export const migrationRunner = async <T extends MigrationMetadata | MigrationMet
continue;
}
collectedMigrations.push(migration);
if (isFinishedMigration(migration)) {
skip ||= migration.status === 'failed' || migration.status === 'skipped';
@ -138,7 +135,7 @@ export const migrationRunner = async <T extends MigrationMetadata | MigrationMet
}
}
await reporter.onCollectedMigrations?.(collectedMigrations);
await reporter.onCollectedMigrations?.(validatedMigrations);
let optionError: Error | undefined;

View file

@ -270,7 +270,6 @@ const getHeaderMessage = (
let skippedCount = 0;
let failedCount = 0;
let unlockableCount = 0;
for (const migration of migrations) {
const isLocked = lockedMigrations.some((lockedMigration) => lockedMigration.name === migration.name);
@ -284,8 +283,6 @@ const getHeaderMessage = (
failedCount += 1;
} else if (migration.status === 'skipped') {
skippedCount += 1;
} else {
unlockableCount += 1;
}
}
}
@ -293,7 +290,6 @@ const getHeaderMessage = (
const parts = [
bold(`${lockedMigrations.length} of ${migrations.length}`),
dim(statusText),
unlockableCount > 0 ? yellow(`(${unlockableCount} locked)`) : '',
skippedCount > 0 ? yellowBright(`(${skippedCount} skipped)`) : '',
failedCount > 0 ? redBright(`(${failedCount} failed)`) : '',
].filter(Boolean);

View file

@ -69,29 +69,40 @@ class PinoReporter implements Required<EmigrateReporter> {
const migrations = this.#migrations ?? [];
if (migrations.length === 0) {
this.#logger.info('No pending migrations found');
this.#logger.info('No migrations found');
return;
}
const statusText = this.#command === 'list' ? 'migrations are pending' : 'pending migrations to run';
if (migrations.length === lockedMigrations.length) {
this.#logger.info(
{ migrationCount: lockedMigrations.length },
`${lockedMigrations.length} pending migrations to run`,
);
this.#logger.info({ migrationCount: lockedMigrations.length }, `${lockedMigrations.length} ${statusText}`);
return;
}
const nonLockedMigrations = migrations.filter(
(migration) => !lockedMigrations.some((lockedMigration) => lockedMigration.name === migration.name),
);
const failedMigrations = nonLockedMigrations.filter(
(migration) => 'status' in migration && migration.status === 'failed',
);
const unlockableCount = this.#command === 'up' ? nonLockedMigrations.length - failedMigrations.length : 0;
let skippedCount = 0;
let failedCount = 0;
for (const migration of migrations) {
const isLocked = lockedMigrations.some((lockedMigration) => lockedMigration.name === migration.name);
if (isLocked) {
continue;
}
if ('status' in migration) {
if (migration.status === 'failed') {
failedCount += 1;
} else if (migration.status === 'skipped') {
skippedCount += 1;
}
}
}
const parts = [
`${lockedMigrations.length} of ${migrations.length} pending migrations to run`,
unlockableCount > 0 ? `(${unlockableCount} locked)` : '',
failedMigrations.length > 0 ? `(${failedMigrations.length} failed)` : '',
`${lockedMigrations.length} of ${migrations.length} ${statusText}`,
skippedCount > 0 ? `(${skippedCount} skipped)` : '',
failedCount > 0 ? `(${failedCount} failed)` : '',
].filter(Boolean);
this.#logger.info({ migrationCount: lockedMigrations.length }, parts.join(' '));