import type { Command } from '@prisma/sdk' ; import { arg , format , getSchemaPath , HelpError , isError , isCi , logger } from '@prisma/sdk' ; import chalk from 'chalk' ; import path from 'path' ; import prompt from 'prompts' ; import { Migrate } from '../Migrate' ; import { ExperimentalFlagWithNewMigrateError , EarlyAccessFeatureFlagWithNewMigrateError } from '../utils/flagErrors' ; import { NoSchemaFoundError , MigrateResetEnvNonInteractiveError } from '../utils/errors' ; import { printFilesFromMigrationIds } from '../utils/printFiles' ; import { throwUpgradeErrorIfOldMigrate } from '../utils/detectOldMigrate' ; import { ensureDatabaseExists } from '../utils/ensureDatabaseExists' ; import { printDatasource } from '../utils/printDatasource' ; import { executeSeedCommand , verifySeedConfigAndReturnMessage , getSeedCommandFromPackageJson } from '../utils/seed' ; export class MigrateReset implements Command { public static new ( ) { return new MigrateReset ( ) ; } private static help = format ( `
Reset your database and apply all migrations, all data will be lost

${ chalk . bold ( 'Usage' ) }

  ${ chalk . dim ( '$' ) } prisma migrate reset [options]

${ chalk . bold ( 'Options' ) }

       -h, --help   Display this help message
         --schema   Custom path to your Prisma schema
  --skip-generate   Skip triggering generators (e.g. Prisma Client)
      --skip-seed   Skip triggering seed
      -f, --force   Skip the confirmation prompt

${ chalk . bold ( 'Examples' ) }

  Reset your database and apply all migrations, all data will be lost
  ${ chalk . dim ( '$' ) } prisma migrate reset

  Specify a schema
  ${ chalk . dim ( '$' ) } prisma migrate reset --schema=./schema.prisma 

  Use --force to skip the confirmation prompt
  ${ chalk . dim ( '$' ) } prisma migrate reset --force
  ` ) ; public async parse ( argv ) { const args = arg ( argv , { '--help' : Boolean , '-h' : '--help' , '--force' : Boolean , '-f' : '--force' , '--skip-generate' : Boolean , '--skip-seed' : Boolean , '--experimental' : Boolean , '--early-access-feature' : Boolean , '--schema' : String , '--telemetry-information' : String , } ) ; if ( isError ( args ) ) { return this . help ( args . message ) ; } if ( args [ '--help' ] ) { return this . help ( ) ; } if ( args [ '--experimental' ] ) { throw new ExperimentalFlagWithNewMigrateError ( ) ; } if ( args [ '--early-access-feature' ] ) { throw new EarlyAccessFeatureFlagWithNewMigrateError ( ) ; } const schemaPath = await getSchemaPath ( args [ '--schema' ] ) ; if ( ! schemaPath ) { throw new NoSchemaFoundError ( ) ; } console . info ( chalk . dim ( `Prisma schema loaded from ${ path . relative ( process . cwd ( ) , schemaPath ) }` ) ) ; await printDatasource ( schemaPath ) ; throwUpgradeErrorIfOldMigrate ( schemaPath ) ; const wasDbCreated = await ensureDatabaseExists ( 'create' , true , schemaPath ) ; if ( wasDbCreated ) { console . info ( ) ; console . info ( wasDbCreated ) ; } console . info ( ) ; if ( ! args [ '--force' ] ) { if ( isCi ( ) && Boolean ( ( prompt as any ) . _injected ?. length ) === false ) { throw new MigrateResetEnvNonInteractiveError ( ) ; } const confirmation = await prompt ( { type : 'confirm' , name : 'value' , message : `Are you sure you want to reset your database? ${ chalk . red ( 'All data will be lost' ) }.` , } ) ; console . info ( ) ; if ( ! confirmation . value ) { console . info ( 'Reset cancelled.' ) ; process . exit ( 0 ) ; return `` ; } } const migrate = new Migrate ( schemaPath ) ; let migrationIds ; try { await migrate . reset ( ) ; const { appliedMigrationNames } = await migrate . applyMigrations ( ) ; migrationIds = appliedMigrationNames ; } finally { migrate . stop ( ) ; } if ( migrationIds . length === 0 ) { console . info ( `${ chalk . green ( 'Database reset successful\n' ) }` ) ; } else { console . info ( ) ; console . info ( `${ chalk . green ( 'Database reset successful' ) }

The following migration(s) have been applied:\n\n${ chalk ( printFilesFromMigrationIds ( 'migrations' , migrationIds , { 'migration.sql' : '' , } ) ) }` ) ; } if ( ! process . env . PRISMA_MIGRATE_SKIP_GENERATE && ! args [ '--skip-generate' ] ) { await migrate . tryToRunGenerate ( ) ; } if ( ! process . env . PRISMA_MIGRATE_SKIP_SEED && ! args [ '--skip-seed' ] ) { const seedCommandFromPkgJson = await getSeedCommandFromPackageJson ( process . cwd ( ) ) ; if ( seedCommandFromPkgJson ) { console . info ( ) ; const successfulSeeding = await executeSeedCommand ( seedCommandFromPkgJson ) ; if ( successfulSeeding ) { console . info ( `\n${ process . platform === 'win32' ? '' : '🌱  ' }The seed command has been executed.` ) ; } } else { const schemaPath = await getSchemaPath ( args [ '--schema' ] ) ; await verifySeedConfigAndReturnMessage ( schemaPath ) ; } } return `` ; } public help ( error ? ) { if ( error ) { return new HelpError ( `\n${ chalk . bold . red ( `!` ) } ${ error }\n${ MigrateReset . help }` ) ; } return MigrateReset . help ; } }