-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBaseMigrationCommand.cfc
More file actions
188 lines (157 loc) · 7.9 KB
/
BaseMigrationCommand.cfc
File metadata and controls
188 lines (157 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
component {
property name="fileSystemUtil" inject="FileSystem";
property name="packageService" inject="PackageService";
property name="JSONService" inject="JSONService";
property name="sqlHighlighter" inject="sqlHighlighter";
property name="systemSettings" inject="SystemSettings";
property name="sqlFormatter" inject="Formatter@sqlFormatter";
function setup( required string manager, boolean setupDatasource = true ) {
var config = getCFMigrationsInfo();
if ( !config.keyExists( arguments.manager ) ) {
error( "No manager found named [#arguments.manager#]. Available managers are: #config.keyList( ", " )#" );
}
var settings = config[ arguments.manager ];
if ( len( trim( settings.migrationsDirectory ) ) ) {
settings.migrationsDirectory = fileSystemUtil.makePathRelative(
fileSystemUtil.resolvePath( settings.migrationsDirectory )
);
}
if ( settings.keyExists( "seedsDirectory" ) && len( trim( settings.seedsDirectory ) ) ) {
settings.seedsDirectory = fileSystemUtil.makePathRelative(
fileSystemUtil.resolvePath( settings.seedsDirectory )
);
if ( !directoryExists( expandPath( settings.seedsDirectory ) ) ) {
directoryCreate( expandPath( settings.seedsDirectory ) );
print.line( "Created seeds directory" )
}
}
if ( arguments.setupDatasource ) {
param settings.properties = {};
if ( settings.properties.keyExists( "connectionInfo" ) ) {
var datasource = installDatasource( settings.properties.connectionInfo );
settings.properties.delete( "connectionInfo" );
settings.properties[ "datasource" ] = datasource;
}
}
variables.migrationService = getInstance(
name = "MigrationService@cfmigrations",
initArguments = settings
);
}
function installDatasource( required struct connectionInfo, string datasourceName = "cfmigrations" ) {
var datasources = getApplicationSettings().datasources ?: {};
datasources[ "cfmigrations" ] = arguments.connectionInfo;
application action='update' datasources=datasources;
application action='update' datasource='#arguments.datasourceName#';
return arguments.datasourceName;
}
public void function setMigrationPath( required migrationsDirectory ) {
var relativePath = fileSystemUtil.makePathRelative(
fileSystemUtil.resolvePath( migrationsDirectory )
);
migrationService.setMigrationsDirectory( relativePath );
}
public string function getMigrationPath () {
return migrationService.getMigrationsDirectory();
}
private void function checkForInstalledMigrationTable() {
if ( ! variables.migrationService.isReady() ) {
if ( confirm( "Migration table not installed. Do you want to install it now? [y\n]" ) ) {
variables.migrationService.install();
} else {
error( "Aborting migration. Please install the migration table and try again." );
}
}
}
private struct function getCFMigrationsInfo() {
var cfmigrationsInfoType = "boxJSON";
var directory = getCWD();
// Check and see if a .migrations.json file exists
if ( fileExists( "#directory#/.migrations.json" ) ) {
var cfmigrationsInfo = deserializeJSON( fileRead( "#directory#/.migrations.json" ) );
variables.systemSettings.expandDeepSystemSettings( cfmigrationsInfo );
cfmigrationsInfoType = "cfmigrations";
return cfmigrationsInfo;
}
// Check and see if a .cfmigrations.json file exists (for backward compatibility)
if ( fileExists( "#directory#/.cfmigrations.json" ) ) {
var cfmigrationsInfo = deserializeJSON( fileRead( "#directory#/.cfmigrations.json" ) );
variables.systemSettings.expandDeepSystemSettings( cfmigrationsInfo );
cfmigrationsInfoType = "cfmigrations";
return cfmigrationsInfo;
}
// Check and see if box.json exists
if( !packageService.isPackage( directory ) ) {
return error( "File [#packageService.getDescriptorPath( directory )#] does not exist." );
}
print.boldUnderscoredYellowLine( "Storing cfmigrations information in box.json has been deprecated in v4 and will be removed in v5." );
print.line( "Please refer to the migration guide at https://github.com/commandbox-modules/commandbox-migrations to upgrade." );
print.line();
var boxJSON = packageService.readPackageDescriptor( directory );
var boxJSONMigrationsInfo = JSONService.show( boxJSON, "cfmigrations", {} );
if ( boxJSONMigrationsInfo.keyExists( "managers" ) ) {
var cfmigrationsInfo = boxJSONMigrationsInfo;
variables.systemSettings.expandDeepSystemSettings( cfmigrationsInfo );
cfmigrationsInfoType = "cfmigrations";
return cfmigrationsInfo;
}
print.boldUnderscoredYellowLine( "The format of the migrations configuration has changed in v4." );
print.line( "We will convert your configuration to the new format. This auto-conversion will be dropped in v5." );
print.line( "Please refer to the migration guide at https://github.com/commandbox-modules/commandbox-migrations to upgrade." );
print.line();
param boxJSONMigrationsInfo.migrationsDirectory = "resources/database/migrations";
param boxJSONMigrationsInfo.defaultGrammar = "AutoDiscover@qb";
var properties = {
"connectionInfo": boxJSONMigrationsInfo.connectionInfo,
"defaultGrammar": boxJSONMigrationsInfo.defaultGrammar
};
if ( boxJSONMigrationsInfo.keyExists( "schema" ) ) {
properties[ "schema" ] = boxJSONMigrationsInfo.schema;
}
var cfmigrationsInfo = {
"default": {
"manager": "cfmigrations.models.QBMigrationManager",
"migrationsDirectory": boxJSONMigrationsInfo.migrationsDirectory,
"properties": properties
}
};
variables.systemSettings.expandDeepSystemSettings( cfmigrationsInfo );
return cfmigrationsInfo;
}
private string function getCFMigrationsType() {
var directory = getCWD();
// Check and see if a .migrations.json file exists
if ( fileExists( "#directory#/.migrations.json" ) ) {
return "cfmigrations";
}
// Check and see if a .cfmigrations.json file exists (for backward compatibility)
if ( fileExists( "#directory#/.cfmigrations.json" ) ) {
return "cfmigrations";
}
// Check and see if box.json exists
if( !packageService.isPackage( directory ) ) {
return error( "File [#packageService.getDescriptorPath( directory )#] does not exist." );
}
var boxJSON = packageService.readPackageDescriptor( directory );
var boxJSONMigrationsInfo = JSONService.show( boxJSON, "cfmigrations", {} );
if ( boxJSONMigrationsInfo.keyExists( "managers" ) ) {
return "cfmigrations";
}
return "boxJSON";
}
function completeManagers( string paramSoFar ) {
var type = getCFMigrationsType();
if ( type == "boxJSON" ) {
return [];
}
return getCFMigrationsInfo().keyArray()
.filter( ( manager ) => startsWith( manager, paramSoFar ) )
.map( ( manager ) => ( { "name": manager, "group": "Managers" } ) );
}
private string function startsWith( required string word, required string substring ) {
if ( len( arguments.substring ) == 0 ) {
return true;
}
return left( arguments.word, len( arguments.substring ) ) == arguments.substring;
}
}