Skip to content

Commit 85e995d

Browse files
committed
Add error handling and documentation
1 parent d7b47a9 commit 85e995d

File tree

5 files changed

+170
-41
lines changed

5 files changed

+170
-41
lines changed

commands/migrate/down.cfc

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
1+
/**
2+
* Rollback one or all of the migrations already ran against your database.
3+
*/
14
component extends="commandbox-migrations.models.BaseMigrationCommand" {
25

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+
) {
416
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
517

6-
checkForInstalledMigrationTable();
18+
try {
19+
checkForInstalledMigrationTable();
720

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+
}
1034
}
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+
}
2051
}
2152

2253
print.line();

commands/migrate/install.cfc

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
1+
/**
2+
* Installs the cfmigrations table in to your database.
3+
*
4+
* The cfmigrations table keeps track of the migrations ran against your database.
5+
* It must be installed before running any migrations.
6+
*/
17
component extends="commandbox-migrations.models.BaseMigrationCommand" {
28

3-
function run( string migrationsDirectory = "resources/database/migrations" ) {
4-
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
9+
/**
10+
* @verbose If true, errors will output a full stack trace.
11+
*/
12+
function run( boolean verbose = false ) {
13+
try {
14+
if ( migrationService.isMigrationTableInstalled() ) {
15+
return error( "Migration table already installed." );
16+
}
517

6-
if ( migrationService.isMigrationTableInstalled() ) {
7-
return error( "Migration table already installed." );
18+
migrationService.install();
19+
print.line( "Migration table installed!" ).line();
820
}
21+
catch ( any e ) {
22+
if ( verbose ) {
23+
rethrow;
24+
}
925

10-
migrationService.install();
11-
print.line( "Migration table installed!" ).line();
26+
switch ( e.type ) {
27+
case "expression":
28+
return error( e.message, e.detail );
29+
case "database":
30+
var migration = e.tagContext[ 4 ];
31+
var templateName = listLast( migration.template, "/" );
32+
var newline = "#chr(10)##chr(13)#";
33+
return error( e.detail, "#templateName##newline##e.queryError#" );
34+
default:
35+
rethrow;
36+
}
37+
}
1238
}
1339

1440
}

commands/migrate/refresh.cfc

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
/**
2+
* Rollback all committed migrations and then apply all migrations in order.
3+
*/
14
component extends="commandbox-migrations.models.BaseMigrationCommand" {
25

3-
function run( string migrationsDirectory = "resources/database/migrations" ) {
6+
/**
7+
* @migrationsDirectory Specify the relative location of the migration files
8+
* @verbose If true, errors output a full stack trace
9+
*/
10+
function run(
11+
string migrationsDirectory = "resources/database/migrations",
12+
boolean verbose = false
13+
) {
414
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
515

6-
runCommand( "migrate down" );
7-
runCommand( "migrate up" );
16+
command( "migrate down" )
17+
.params( argumentCollection = { verbose = arguments.verbose } )
18+
.run();
19+
20+
command( "migrate up" )
21+
.params( argumentCollection = { verbose = arguments.verbose } )
22+
.run();
823
}
924

1025
}

commands/migrate/uninstall.cfc

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
1+
/**
2+
* Uninstalls the cfmigrations table from your database.
3+
*
4+
* The cfmigrations table keeps track of the migrations ran against your database.
5+
* Uninstall it when you are removing cfmigrations from your application.
6+
*/
17
component extends="commandbox-migrations.models.BaseMigrationCommand" {
28

3-
function run( string migrationsDirectory = "resources/database/migrations" ) {
4-
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
9+
/**
10+
* @verbose If true, errors will output a full stack trace.
11+
*/
12+
function run( boolean verbose = false ) {
13+
try {
14+
if ( ! migrationService.isMigrationTableInstalled() ) {
15+
return error( "No Migration table detected." );
16+
}
517

6-
if ( ! migrationService.isMigrationTableInstalled() ) {
7-
return error( "No Migration table detected." );
18+
migrationService.uninstall();
19+
print.line( "Migration table uninstalled!" ).line();
820
}
21+
catch ( any e ) {
22+
if ( verbose ) {
23+
rethrow;
24+
}
925

10-
migrationService.uninstall();
11-
print.line( "Migration table uninstalled!" ).line();
26+
switch ( e.type ) {
27+
case "expression":
28+
return error( e.message, e.detail );
29+
case "database":
30+
var migration = e.tagContext[ 4 ];
31+
var templateName = listLast( migration.template, "/" );
32+
var newline = "#chr(10)##chr(13)#";
33+
return error( e.detail, "#templateName##newline##e.queryError#" );
34+
default:
35+
rethrow;
36+
}
37+
}
1238
}
1339

1440
}

commands/migrate/up.cfc

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
1+
/**
2+
* Apply one or all pending migrations against your database.
3+
*/
14
component extends="commandbox-migrations.models.BaseMigrationCommand" {
25

3-
function run( boolean once = false, string migrationsDirectory = "resources/database/migrations" ) {
6+
/**
7+
* @once Only apply 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+
) {
416
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
517

6-
checkForInstalledMigrationTable();
18+
try {
19+
checkForInstalledMigrationTable();
720

8-
if ( ! migrationService.hasMigrationsToRun( "up" ) ) {
9-
print.line().yellowLine( "No migrations to run." ).line();
21+
if ( ! migrationService.hasMigrationsToRun( "up" ) ) {
22+
print.line().yellowLine( "No migrations to run." ).line();
23+
}
24+
else if ( once ) {
25+
migrationService.runNextMigration( "up", function( migration ) {
26+
print.whiteLine( "#migration.componentName# migrated successfully!" );
27+
} );
28+
}
29+
else {
30+
migrationService.runAllMigrations( "up", function( migration ) {
31+
print.whiteLine( "#migration.componentName# migrated successfully!" );
32+
} );
33+
}
1034
}
11-
else if ( once ) {
12-
migrationService.runNextMigration( "up", function( migration ) {
13-
print.whiteLine( "#migration.componentName# migrated successfully!" );
14-
} );
15-
}
16-
else {
17-
migrationService.runAllMigrations( "up", function( migration ) {
18-
print.whiteLine( "#migration.componentName# migrated 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+
}
2051
}
2152

2253
print.line();

0 commit comments

Comments
 (0)