import { getGeneratorSuccessMessage , getSchemaPathSync , getGenerators } from '@prisma/sdk' ; import chalk from 'chalk' ; import Debug from '@prisma/debug' ; import fs from 'fs' ; import logUpdate from 'log-update' ; import path from 'path' ; import { MigrateEngine } from './MigrateEngine' ; import type { EngineResults , EngineArgs } from './types' ; import { enginesVersion } from '@prisma/engines-version' ; import { NoSchemaFoundError } from './utils/errors' ; const debug = Debug ( 'prisma:migrate' ) ; const packageJson = eval ( `require('../package.json')` ) ; export class Migrate { get devMigrationsDir ( ) : string { return path . join ( path . dirname ( this . schemaPath ) , 'migrations/dev' ) ; } public engine : MigrateEngine ; private schemaPath : string ; public migrationsDirectoryPath : string ; constructor ( schemaPath ? , enabledPreviewFeatures ? ) { this . schemaPath = this . getSchemaPath ( schemaPath ) ; this . migrationsDirectoryPath = path . join ( path . dirname ( this . schemaPath ) , 'migrations' ) ; this . engine = new MigrateEngine ( { projectDir : path . dirname ( this . schemaPath ) , schemaPath : this . schemaPath , enabledPreviewFeatures , } ) ; } public stop ( ) { this . engine . stop ( ) ; } public getSchemaPath ( schemaPathFromOptions ? ) { const schemaPath = getSchemaPathSync ( schemaPathFromOptions ) ; if ( ! schemaPath ) { throw new NoSchemaFoundError ( ) ; } return schemaPath ; } public getDatamodel ( ) { return fs . readFileSync ( this . schemaPath , 'utf-8' ) ; } public reset ( ) { return this . engine . reset ( ) ; } public createMigration ( params ) { return this . engine . createMigration ( params ) ; } public diagnoseMigrationHistory ( { optInToShadowDatabase , } ) { return this . engine . diagnoseMigrationHistory ( { migrationsDirectoryPath : this . migrationsDirectoryPath , optInToShadowDatabase , } ) ; } public listMigrationDirectories ( ) { return this . engine . listMigrationDirectories ( { migrationsDirectoryPath : this . migrationsDirectoryPath , } ) ; } public devDiagnostic ( ) { return this . engine . devDiagnostic ( { migrationsDirectoryPath : this . migrationsDirectoryPath , } ) ; } public async markMigrationApplied ( { migrationId } ) { return await this . engine . markMigrationApplied ( { migrationsDirectoryPath : this . migrationsDirectoryPath , migrationName : migrationId , } ) ; } public markMigrationRolledBack ( { migrationId } ) { return this . engine . markMigrationRolledBack ( { migrationName : migrationId , } ) ; } public applyMigrations ( ) { return this . engine . applyMigrations ( { migrationsDirectoryPath : this . migrationsDirectoryPath , } ) ; } public evaluateDataLoss ( ) { const datamodel = this . getDatamodel ( ) ; return this . engine . evaluateDataLoss ( { migrationsDirectoryPath : this . migrationsDirectoryPath , prismaSchema : datamodel , } ) ; } public async push ( { force = false } ) { const datamodel = this . getDatamodel ( ) ; const { warnings , unexecutable , executedSteps } = await this . engine . schemaPush ( { force , schema : datamodel , } ) ; return { executedSteps , warnings , unexecutable , } ; } public async tryToRunGenerate ( ) { const message = [ ] ; console . info ( ) ; logUpdate ( `Running generate... ${ chalk . dim ( '(Use --skip-generate to skip the generators)' ) }` ) ; const generators = await getGenerators ( { schemaPath : this . schemaPath , printDownloadProgress : true , version : enginesVersion , cliVersion : packageJson . version , } ) ; for ( const generator of generators ) { logUpdate ( `Running generate... - ${ generator . getPrettyName ( ) }` ) ; const before = Date . now ( ) ; try { await generator . generate ( ) ; const after = Date . now ( ) ; message . push ( getGeneratorSuccessMessage ( generator , after - before ) ) ; generator . stop ( ) ; } catch ( e ) { message . push ( `${ e . message }` ) ; generator . stop ( ) ; } } logUpdate ( message . join ( '\n' ) ) ; } }