|
| 1 | +/** |
| 2 | +* Rollback one or all of the migrations already ran against your database. |
| 3 | +*/ |
1 | 4 | component extends="commandbox-migrations.models.BaseMigrationCommand" { |
2 | 5 |
|
3 | | - function run( boolean once = false, string migrationsDirectory = "resources/database/migrations" ) { |
| 6 | + /** |
| 7 | + * @once Only rollback a single migration. |
| 8 | + * @migrationsDirectory Specify the relative location of the migration files |
| 9 | + * @verbose If true, errors output a full stack trace |
| 10 | + */ |
| 11 | + function run( |
| 12 | + boolean once = false, |
| 13 | + string migrationsDirectory = "resources/database/migrations", |
| 14 | + boolean verbose = false |
| 15 | + ) { |
4 | 16 | migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" ); |
5 | 17 |
|
6 | | - checkForInstalledMigrationTable(); |
| 18 | + try { |
| 19 | + checkForInstalledMigrationTable(); |
7 | 20 |
|
8 | | - if ( ! migrationService.hasMigrationsToRun( "down" ) ) { |
9 | | - print.line().yellowLine( "No migrations to rollback." ).line(); |
| 21 | + if ( ! migrationService.hasMigrationsToRun( "down" ) ) { |
| 22 | + print.line().yellowLine( "No migrations to rollback." ).line(); |
| 23 | + } |
| 24 | + else if ( once ) { |
| 25 | + migrationService.runNextMigration( "down", function( migration ) { |
| 26 | + print.whiteLine( "#migration.componentName# rolled back successfully!" ); |
| 27 | + } ); |
| 28 | + } |
| 29 | + else { |
| 30 | + migrationService.runAllMigrations( "down", function( migration ) { |
| 31 | + print.whiteLine( "#migration.componentName# rolled back successfully!" ); |
| 32 | + } ); |
| 33 | + } |
10 | 34 | } |
11 | | - else if ( once ) { |
12 | | - migrationService.runNextMigration( "down", function( migration ) { |
13 | | - print.whiteLine( "#migration.componentName# rolled back successfully!" ); |
14 | | - } ); |
15 | | - } |
16 | | - else { |
17 | | - migrationService.runAllMigrations( "down", function( migration ) { |
18 | | - print.whiteLine( "#migration.componentName# rolled back successfully!" ); |
19 | | - } ); |
| 35 | + catch ( any e ) { |
| 36 | + if ( verbose ) { |
| 37 | + rethrow; |
| 38 | + } |
| 39 | + |
| 40 | + switch ( e.type ) { |
| 41 | + case "expression": |
| 42 | + return error( e.message, e.detail ); |
| 43 | + case "database": |
| 44 | + var migration = e.tagContext[ 4 ]; |
| 45 | + var templateName = listLast( migration.template, "/" ); |
| 46 | + var newline = "#chr(10)##chr(13)#"; |
| 47 | + return error( e.detail, "#templateName##newline##e.queryError#" ); |
| 48 | + default: |
| 49 | + rethrow; |
| 50 | + } |
20 | 51 | } |
21 | 52 |
|
22 | 53 | print.line(); |
|
0 commit comments