diff --git a/packages/core/src/scripts/migrate/check.ts b/packages/core/src/scripts/migrate/check.ts new file mode 100644 index 00000000000..ee69a5f2317 --- /dev/null +++ b/packages/core/src/scripts/migrate/check.ts @@ -0,0 +1,21 @@ +import { runMigrateWithDbUrl, withMigrate } from '../../lib/migrations'; +import { loadBuiltConfig } from '../../lib/config/loadConfig'; +import { ExitError } from '../utils'; +import { getSchemaPaths } from '../../artifacts'; + +export async function checkMigrations(cwd: string) { + const config = loadBuiltConfig(cwd); + await withMigrate(getSchemaPaths(cwd).prisma, async migrate => { + const evaluateDataLossResult = await runMigrateWithDbUrl( + config.db.url, + config.db.shadowDatabaseUrl, + () => migrate.evaluateDataLoss() + ); + if (evaluateDataLossResult.migrationSteps) { + console.log('Migrations are out of date - run `keystone migrate generate` to update them'); + throw new ExitError(1); + } else { + console.log('✅ Migrations are up to date'); + } + }); +} diff --git a/packages/core/src/scripts/migrate/index.ts b/packages/core/src/scripts/migrate/index.ts index 6a5c822d9cb..ee3e3c94893 100644 --- a/packages/core/src/scripts/migrate/index.ts +++ b/packages/core/src/scripts/migrate/index.ts @@ -1,5 +1,6 @@ import { ExitError } from '../utils'; import { applyMigrations } from './apply'; +import { checkMigrations } from './check'; import { generateMigrations } from './generate'; export async function migrate(cwd: string, input: string[], shouldDropDatabase: boolean) { @@ -9,6 +10,8 @@ export async function migrate(cwd: string, input: string[], shouldDropDatabase: return generateMigrations(cwd, shouldDropDatabase); case 'apply': return applyMigrations(cwd); + case 'check': + return checkMigrations(cwd); default: console.log(`${migrateCommand} is not a migrate command that keystone accepts`); throw new ExitError(1);