From b67203feaf50d77ce6401e8ff707e59f03f2bf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Galeran?= Date: Tue, 1 Dec 2020 15:56:34 +0100 Subject: [PATCH] improvement(migrate): better status output (#4447) https://github.com/prisma/migrations-team/issues/133 --- .../src/__tests__/MigrateStatus.test.ts | 52 +++++++++++++---- .../migrate/src/commands/MigrateStatus.ts | 56 +++++++++++-------- 2 files changed, 73 insertions(+), 35 deletions(-) diff --git a/src/packages/migrate/src/__tests__/MigrateStatus.test.ts b/src/packages/migrate/src/__tests__/MigrateStatus.test.ts index 083cc6af6cb5..34a6c31502aa 100644 --- a/src/packages/migrate/src/__tests__/MigrateStatus.test.ts +++ b/src/packages/migrate/src/__tests__/MigrateStatus.test.ts @@ -62,6 +62,8 @@ describe('sqlite', () => { - If you fixed the database manually (hotfix): prisma migrate resolve --applied "20201231000000_failed" --early-access-feature + + Read more in our docs: https://pris.ly/migrate-resolve `) expect(ctx.mocked['console.info'].mock.calls.join('\n')) @@ -69,8 +71,12 @@ describe('sqlite', () => { Prisma schema loaded from prisma/schema.prisma Status - - 1 migration - - 1 failed migration: 20201231000000_failed + 1 migration found in prisma/migrations + + Following migration have failed: + 20201231000000_failed + + During development if the failed migration(s) have not been deployed to a production database you can then fix the migration(s) and run prisma migrate dev --early-access-feature. `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() @@ -93,8 +99,15 @@ describe('sqlite', () => { Prisma schema loaded from prisma/schema.prisma Status - - 1 migration - - 1 unapplied migration: 20201231000000_ + 1 migration found in prisma/migrations + + Following migration have not yet been applied: + 20201231000000_ + + To apply migrations in development run prisma migrate dev --early-access-feature. + To apply migrations in production run prisma migrate deploy --early-access-feature. + + Read more in our docs: https://pris.ly/migrate-deploy `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot() @@ -103,14 +116,17 @@ describe('sqlite', () => { it('existing-db-1-migration', async () => { ctx.fixture('existing-db-1-migration') const result = MigrateStatus.new().parse(['--early-access-feature']) - await expect(result).resolves.toMatchInlineSnapshot(`No problem detected.`) + await expect(result).resolves.toMatchInlineSnapshot( + `Database schema is up to date!`, + ) expect(ctx.mocked['console.info'].mock.calls.join('\n')) .toMatchInlineSnapshot(` Prisma schema loaded from prisma/schema.prisma Status - - 1 migration + 1 migration found in prisma/migrations + `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() @@ -133,8 +149,15 @@ describe('sqlite', () => { Prisma schema loaded from prisma/schema.prisma Status - - 1 migration - - 1 unapplied migration: 20201231000000_init + 1 migration found in prisma/migrations + + Following migration have not yet been applied: + 20201231000000_init + + To apply migrations in development run prisma migrate dev --early-access-feature. + To apply migrations in production run prisma migrate deploy --early-access-feature. + + Read more in our docs: https://pris.ly/migrate-deploy `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot() @@ -152,7 +175,8 @@ describe('sqlite', () => { Prisma schema loaded from prisma/schema.prisma Status - - No migration found + No migration found in prisma/migrations + `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot() @@ -170,7 +194,8 @@ describe('sqlite', () => { Prisma schema loaded from prisma/schema.prisma Status - - No migration found + No migration found in prisma/migrations + `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() expect(ctx.mocked['console.error'].mock.calls).toMatchSnapshot() @@ -208,14 +233,17 @@ describe('sqlite', () => { it('reset', async () => { ctx.fixture('reset') const result = MigrateStatus.new().parse(['--early-access-feature']) - await expect(result).resolves.toMatchInlineSnapshot(`No problem detected.`) + await expect(result).resolves.toMatchInlineSnapshot( + `Database schema is up to date!`, + ) expect(ctx.mocked['console.info'].mock.calls.join('\n')) .toMatchInlineSnapshot(` Prisma schema loaded from prisma/schema.prisma Status - - 1 migration + 1 migration found in prisma/migrations + `) expect(ctx.mocked['console.log'].mock.calls).toMatchSnapshot() diff --git a/src/packages/migrate/src/commands/MigrateStatus.ts b/src/packages/migrate/src/commands/MigrateStatus.ts index 3e45987016a5..d9730641f0fd 100644 --- a/src/packages/migrate/src/commands/MigrateStatus.ts +++ b/src/packages/migrate/src/commands/MigrateStatus.ts @@ -123,7 +123,7 @@ Delete the current migrations folder to continue and read the documentation for // This is a *read-only* command (modulo shadow database). // - ↩️ **RPC**: ****`diagnoseMigrationHistory`, then four cases based on the response. // 4. Otherwise, there is no problem migrate is aware of. We could still display: - // - Which migrations have been edited since applied (maybe too noisy) + // - Edited since applied only relevant when using dev, they are ignored for deploy // - Pending migrations (those in the migrations folder that haven't been applied yet) // - If there are no pending migrations, tell the user everything looks OK and up to date. @@ -140,27 +140,32 @@ Delete the current migrations folder to continue and read the documentation for if (listMigrationDirectoriesResult.migrations.length > 0) { const migrations = listMigrationDirectoriesResult.migrations console.info( - `- ${migrations.length} migration${migrations.length > 1 ? 's' : ''}`, + `${migrations.length} migration${ + migrations.length > 1 ? 's' : '' + } found in prisma/migrations\n`, ) } else { - console.info(`- No migration found`) - } - - if (diagnoseResult.editedMigrationNames.length > 0) { - const editedMigrations = diagnoseResult.editedMigrationNames - console.info( - `- ${editedMigrations.length} edited migration${ - editedMigrations.length > 1 ? 's' : '' - }: ${editedMigrations.join(', ')}`, - ) + console.info(`No migration found in prisma/migrations\n`) } if (diagnoseResult.history?.diagnostic === 'databaseIsBehind') { const unappliedMigrations = diagnoseResult.history.unappliedMigrationNames console.info( - `- ${unappliedMigrations.length} unapplied migration${ + `Following migration${ unappliedMigrations.length > 1 ? 's' : '' - }: ${unappliedMigrations.join(', ')}`, + } have not yet been applied: +${unappliedMigrations.join('\n')} + +To apply migrations in development run ${chalk.bold.greenBright( + getCommandWithExecutor(`prisma migrate dev --early-access-feature`), + )}. +To apply migrations in production run ${chalk.bold.greenBright( + getCommandWithExecutor( + `prisma migrate deploy --early-access-feature`, + ), + )}. + +Read more in our docs: https://pris.ly/migrate-deploy`, ) } @@ -199,9 +204,14 @@ ${chalk.bold.greenBright( const failedMigrations = diagnoseResult.failedMigrationNames console.info( - `- ${failedMigrations.length} failed migration${ + `Following migration${ failedMigrations.length > 1 ? 's' : '' - }: ${failedMigrations.join(' ,')}\n`, + } have failed: +${failedMigrations.join('\n')} + +During development if the failed migration(s) have not been deployed to a production database you can then fix the migration(s) and run ${chalk.bold.greenBright( + getCommandWithExecutor(`prisma migrate dev --early-access-feature`), + )}.\n`, ) if ( @@ -212,23 +222,23 @@ ${chalk.bold.greenBright( ${chalk.grey(diagnoseResult.drift.rollback)}`) } - const migrationId = failedMigrations[0] - return `The failed migration(s) can be marked as rolled back or applied: - If you rolled back the migration(s) manually: ${chalk.bold.greenBright( getCommandWithExecutor( - `prisma migrate resolve --rolledback "${migrationId}" --early-access-feature`, + `prisma migrate resolve --rolledback "${failedMigrations[0]}" --early-access-feature`, ), )} - If you fixed the database manually (hotfix): ${chalk.bold.greenBright( getCommandWithExecutor( - `prisma migrate resolve --applied "${migrationId}" --early-access-feature`, + `prisma migrate resolve --applied "${failedMigrations[0]}" --early-access-feature`, ), -)}` +)} + +Read more in our docs: https://pris.ly/migrate-resolve` } else if ( diagnoseResult.drift?.diagnostic === 'driftDetected' && (diagnoseResult.history?.diagnostic === 'databaseIsBehind' || @@ -242,7 +252,7 @@ ${chalk.bold.greenBright( const migrationId = diagnoseResult.history.unappliedMigrationNames - return `Prisma Migrate detected that the current database structure is not in sync with your Prisma schema. + return `The current database schema is not in sync with your Prisma schema. This is the script to roll back manually: ${chalk.grey(diagnoseResult.drift.rollback)} @@ -264,7 +274,7 @@ You have 2 options )} to create a new migration matching the drift.` } else { console.info() // empty line - return `No problem detected.` + return `Database schema is up to date!` } }