Skip to content

Commit ecbd36e

Browse files
committed
Initial commit
0 parents  commit ecbd36e

File tree

13 files changed

+321
-0
lines changed

13 files changed

+321
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
modules
2+
TASKS.md

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Eric Peterson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

ModuleConfig.cfc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
component {
2+
3+
this.dependencies = [ "cfmigrations" ];
4+
5+
function configure() {}
6+
7+
}

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# CFMigrations Commands
2+
3+
## Run your cfmigrations from CommandBox
4+
5+
You need to set up some information in your `box.json`:
6+
7+
```json
8+
"cfmigrations": {
9+
"defaultGrammar": "BaseGrammar",
10+
"connectionInfo": {
11+
"class": "${DB_CLASS}",
12+
"connectionString": "${DB_CONNECTIONSTRING}",
13+
"username": "${DB_USER}",
14+
"password": "${DB_PASSWORD}"
15+
}
16+
}
17+
```
18+
19+
There are two main sections. The `defaultGrammar` sets the correct Database Grammar for `qb` to use to build your schema. Available grammar options can be found in the [qb documentation](https://elpete.gitbooks.io/qb/content/).
20+
21+
> You don't have to use qb's `SchemaBuilder` to use `cfmigrations`. Just run your own migrations using `queryExecute` and you can have complete control over your sql.
22+
23+
The `connectionInfo` object is the information to create an on the fly connection in CommandBox to run your migrations. This is the same struct you would use to add an application datasource in Lucee. (Note: it must be Lucee compatible since that is what CommandBox runs on under-the-hood.)
24+
25+
You may notice that the values are surrounded in an escape sequence (`${}`). This is how CommandBox injects environment variables into your `box.json` file. Why environment variables? Because you don't want to commit your database credentials in to source control. Also, you want to be able to have different values in different environments. Whether you have dedicated servers or are running your application in containers, you can find the supported way to add environment variables to your platform.
26+
27+
For local development using CommandBox, I recommend using the package [`commandbox-dotenv`](https://forgebox.io/view/commandbox-dotenv). This package lets you define environment variables in a `.env` file in the root of your project. CommandBox will add these to your server when starting it up and also to the CommandBox instance if you load or reload the shell in a directory with a `.env` file. That is how we will get our environment variables available for `cfmigrations-commands`.
28+
29+
With `commandbox-dotenv` installed, create a `.env` file in the root of you project. At the very least, it will look like this:
30+
31+
```
32+
DB_CLASS=org.gjt.mm.mysql.Driver
33+
DB_CONNECTIONSTRING=jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true
34+
DB_USER=test
35+
DB_PASSWORD=pass1234
36+
```
37+
38+
I recommend adding this file to your `.gitignore`
39+
40+
```
41+
.env
42+
```
43+
44+
An added step to help new users get up to speed with the needed environment variables for your project is to add an `.env.example` file to the root of your project as well. This file would have all the keys needed, but no values filled out. Like so:
45+
46+
```
47+
DB_CLASS=
48+
DB_CONNECTIONSTRING=
49+
DB_USER=
50+
DB_PASSWORD=
51+
```
52+
53+
You would update your `.gitignore` file to not ignore the `.env.example` file:
54+
55+
```
56+
.env
57+
!.env.example
58+
```
59+
60+
61+

box.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name":"CFMigrations Commands",
3+
"version":"0.0.0",
4+
"location":"elpete/commandbox-migrations#v0.0.0",
5+
"author":"Eric Peterson",
6+
"homepage":"https://github.com/elpete/commandbox-migrations",
7+
"documentation":"https://github.com/elpete/commandbox-migrations",
8+
"repository":{
9+
"type":"git",
10+
"URL":"https://github.com/elpete/commandbox-migrations"
11+
},
12+
"bugs":"https://github.com/elpete/commandbox-migrations/issues",
13+
"slug":"commandbox-migrations",
14+
"shortDescription":"Run your cfmigrations from CommandBox",
15+
"instructions":"https://github.com/elpete/commandbox-migrations",
16+
"type":"commandbox-modules",
17+
"keywords":[
18+
"server",
19+
"database",
20+
"migrations",
21+
"cfmigrations"
22+
],
23+
"scripts":{
24+
"postVersion":"package set location='elpete/commandbox-migrations#v`package version`'",
25+
"onRelease":"publish",
26+
"postPublish":"!git push && !git push --tags"
27+
},
28+
"private":false,
29+
"license":[
30+
{
31+
"type":"MIT",
32+
"URL":"https://github.com/elpete/commandbox-migrations/blob/master/LICENSE"
33+
}
34+
],
35+
"dependencies":{
36+
"cfmigrations": "^1.0.0"
37+
},
38+
"installPaths":{
39+
"cfmigrations": "modules/cfmigrations"
40+
}
41+
}

commands/migrate/create.cfc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Create a new migration CFC in an existing application.
3+
* Make sure you are running this command in the root of your app.
4+
*
5+
* It prepends the date at the beginning of the file name so
6+
* you can keep your migrations in the correct order.
7+
*/
8+
component {
9+
10+
/**
11+
* @name Name of the migration to create without the .cfc.
12+
* @directory The base directory to create your migration in. Creates the directory if it does not exist.
13+
* @open Open the file once generated
14+
*/
15+
function run(
16+
required string name,
17+
string directory = "resources/database/migrations",
18+
boolean open = false
19+
) {
20+
// This will make each directory canonical and absolute
21+
arguments.directory = fileSystemUtil.resolvePath( arguments.directory );
22+
23+
// Validate directory
24+
if( !directoryExists( arguments.directory ) ) {
25+
directoryCreate( arguments.directory );
26+
}
27+
28+
var timestamp = dateTimeFormat( now(), "yyyy_mm_dd_hhnnss" );
29+
var migrationPath = "#arguments.directory#/#timestamp#_#arguments.name#.cfc";
30+
31+
var migrationContent = fileRead( "/cfmigrations-commands/templates/Migration.txt" );
32+
33+
file action="write" file="#migrationPath#" mode="777" output="#trim( migrationContent )#";
34+
35+
print.greenLine( "Created #migrationPath#" );
36+
37+
// Open file?
38+
if ( arguments.open ) {
39+
openPath( migrationPath );
40+
}
41+
42+
return;
43+
}
44+
45+
}

commands/migrate/down.cfc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
component extends="cfmigrations-commands.models.BaseMigrationCommand" {
2+
3+
function run( boolean once = false, string migrationsDirectory = "resources/database/migrations" ) {
4+
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
5+
6+
checkForInstalledMigrationTable();
7+
8+
if ( ! migrationService.hasMigrationsToRun( "down" ) ) {
9+
print.line().yellowLine( "No migrations to rollback." ).line();
10+
}
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+
} );
20+
}
21+
22+
print.line();
23+
}
24+
25+
}

commands/migrate/install.cfc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
component extends="cfmigrations-commands.models.BaseMigrationCommand" {
2+
3+
function run( string migrationsDirectory = "resources/database/migrations" ) {
4+
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
5+
6+
if ( migrationService.isMigrationTableInstalled() ) {
7+
return error( "Migration table already installed." );
8+
}
9+
10+
migrationService.install();
11+
print.line( "Migration table installed!" ).line();
12+
}
13+
14+
}

commands/migrate/refresh.cfc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
component extends="cfmigrations-commands.models.BaseMigrationCommand" {
2+
3+
function run( string migrationsDirectory = "resources/database/migrations" ) {
4+
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
5+
6+
runCommand( "migrate down" );
7+
runCommand( "migrate up" );
8+
}
9+
10+
}

commands/migrate/uninstall.cfc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
component extends="cfmigrations-commands.models.BaseMigrationCommand" {
2+
3+
function run( string migrationsDirectory = "resources/database/migrations" ) {
4+
migrationService.setMigrationsDirectory( "#getCWD()#/#arguments.migrationsDirectory#" );
5+
6+
if ( ! migrationService.isMigrationTableInstalled() ) {
7+
return error( "No Migration table detected." );
8+
}
9+
10+
migrationService.uninstall();
11+
print.line( "Migration table uninstalled!" ).line();
12+
}
13+
14+
}

0 commit comments

Comments
 (0)