import { Command , flags } from '@oclif/command' ; import { Connection , ConnectionOptions , createConnection } from 'typeorm' ; import { LoggerProxy } from 'n8n-workflow' ; import { getLogger } from '../../src/Logger' ; import { Db } from '../../src' ; export class DbRevertMigrationCommand extends Command { static description = 'Revert last database migration' ; static examples = [ '$ n8n db:revert' ] ; static flags = { help : flags . help ( { char : 'h' } ) , } ; async run ( ) { const logger = getLogger ( ) ; LoggerProxy . init ( logger ) ; const { flags } = this . parse ( DbRevertMigrationCommand ) ; let connection ; try { await Db . init ( ) ; connection = Db . collections . Credentials ?. manager . connection ; if ( ! connection ) { throw new Error ( `No database connection available.` ) ; } const connectionOptions = Object . assign ( connection . options , { subscribers : [ ] , synchronize : false , migrationsRun : false , dropSchema : false , logging : [ 'query' , 'error' , 'schema' ] , } ) ; await connection . close ( ) ; connection = await createConnection ( connectionOptions ) ; await connection . undoLastMigration ( ) ; await connection . close ( ) ; } catch ( error ) { if ( connection ) await connection . close ( ) ; console . error ( 'Error reverting last migration. See log messages for details.' ) ; logger . error ( error . message ) ; this . exit ( 1 ) ; } this . exit ( ) ; } }