Skip to content

Commit f5d505c

Browse files
committed
fix: Use properties for custom migration managers
The properties defined for a custom QBMigrationManager were not being used. Added test coverage to ensure that custom managers defined in `config/ColdBox.cfc` will be configured with the appropriate settings.
1 parent d4b64aa commit f5d505c

File tree

5 files changed

+75
-13
lines changed

5 files changed

+75
-13
lines changed

ModuleConfig.cfc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ component {
1212
"managers": {
1313
"default": {
1414
"manager": "cfmigrations.models.QBMigrationManager",
15-
"properties": { "defaultGrammar": "BaseGrammar" }
15+
"properties": { "defaultGrammar": "AutoDiscover@qb" }
1616
}
1717
}
1818
};

models/MigrationService.cfc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
component singleton accessors="true" {
1+
component accessors="true" {
22

33
property name="wirebox" inject="wirebox";
44
property name="environment" inject="coldbox:setting:environment" default="development";
@@ -19,11 +19,11 @@ component singleton accessors="true" {
1919
* @properties
2020
*/
2121
MigrationService function init(
22-
any manager,
23-
string migrationsDirectory,
24-
string seedsDirectory,
25-
any seedEnvironments,
26-
struct properties
22+
any manager = "cfmigrations.models.QBMigrationManager",
23+
string migrationsDirectory = "/resources/database/migrations",
24+
string seedsDirectory = "/resources/database/seeds",
25+
any seedEnvironments = [ "development" ],
26+
struct properties = {}
2727
) {
2828
variables.managerProperties = {};
2929
var args = arguments;
@@ -33,7 +33,7 @@ component singleton accessors="true" {
3333
} )
3434
.each( function( key ) {
3535
if ( isSimpleValue( args[ key ] ) ) {
36-
variables[ key ] = args[ key ];
36+
invoke( this, "set" & key, { key: args[ key ] } );
3737
} else if ( key == "properties" ) {
3838
variables.managerProperties = args[ key ];
3939
}
@@ -47,10 +47,12 @@ component singleton accessors="true" {
4747
}
4848

4949
function onDIComplete() {
50-
variables.manager = variables.wirebox.getInstance(
51-
name = variables.manager,
52-
initArguments = variables.managerProperties
53-
);
50+
if ( isSimpleValue( variables.manager ) ) {
51+
variables.manager = variables.wirebox.getInstance(
52+
name = variables.manager,
53+
initArguments = variables.managerProperties
54+
);
55+
}
5456
}
5557

5658
/**

models/QBMigrationManager.cfc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ component accessors="true" {
88
property name="schema";
99
property name="useTransactions" default="true";
1010

11+
public QBMigrationManager function init() {
12+
for ( var key in arguments ) {
13+
if ( !isNull( arguments[ key ] ) ) {
14+
variables[ key ] = arguments[ key ];
15+
}
16+
}
17+
return this;
18+
}
19+
1120
boolean function isReady() {
1221
return isMigrationTableInstalled();
1322
}

tests/resources/app/config/Coldbox.cfc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,27 @@
4141

4242
moduleSettings = {
4343
"quick" : { "defaultGrammar" : "PostgresGrammar@qb" },
44-
"qb" : { "defaultGrammar" : "PostgresGrammar@qb" }
44+
"qb" : { "defaultGrammar" : "PostgresGrammar@qb" },
45+
"cfmigrations": {
46+
"managers": {
47+
"default": {
48+
"manager": "cfmigrations.models.QBMigrationManager",
49+
"migrationsDirectory": "/resources/database/migrations",
50+
"seedsDirectory": "/resources/database/seeds",
51+
"properties": {}
52+
},
53+
"db1": {
54+
"manager": "cfmigrations.models.QBMigrationManager",
55+
"migrationsDirectory": "/resources/database/db1/migrations",
56+
"seedsDirectory": "/resources/database/db1/seeds",
57+
"properties": {
58+
"defaultGrammar": "MySQLGrammar@qb",
59+
"datasource": "db1",
60+
"useTransactions": "false",
61+
}
62+
}
63+
}
64+
}
4565
};
4666

4767
// custom settings
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
2+
3+
function run() {
4+
describe( "MigrationService", function() {
5+
it( "can instantiate with a default migration manager", function() {
6+
var migrationService = application.wirebox.getInstance( "migrationService:default" );
7+
expect( migrationService.getMigrationsDirectory() ).toBe( "/resources/database/migrations" );
8+
expect( migrationService.getSeedsDirectory() ).toBe( "/resources/database/seeds" );
9+
var manager = migrationService.getManager();
10+
expect( manager.getDefaultGrammar() ).toBe( "AutoDiscover@qb" );
11+
expect( manager.getDatasource() ).toBeNull();
12+
expect( manager.getMigrationsTable() ).toBe( "cfmigrations" );
13+
expect( manager.getSchema() ).toBeNull();
14+
expect( manager.getUseTransactions() ).toBeTrue();
15+
} );
16+
17+
it( "can instantiate with different named migration managers", function() {
18+
var migrationService = application.wirebox.getInstance( "migrationService:db1" );
19+
expect( migrationService.getMigrationsDirectory() ).toBe( "/resources/database/db1/migrations" );
20+
expect( migrationService.getSeedsDirectory() ).toBe( "/resources/database/db1/seeds" );
21+
var manager = migrationService.getManager();
22+
expect( manager.getDefaultGrammar() ).toBe( "MySQLGrammar@qb" );
23+
expect( manager.getDatasource() ).notToBeNull();
24+
expect( manager.getDatasource() ).toBe( "db1" );
25+
expect( manager.getMigrationsTable() ).toBe( "cfmigrations" );
26+
expect( manager.getUseTransactions() ).toBeFalse();
27+
} );
28+
} );
29+
}
30+
31+
}

0 commit comments

Comments
 (0)