FORGEBOX Enterprise 🚀 - Take your ColdFusion (CFML) Development to Modern Times! Learn More...

CFMigrations Commands

v1.5.2 Public

commandbox-migrations

Run your cfmigrations from CommandBox

You need to set up some information in your box.json:

"cfmigrations": {
    "defaultGrammar": "BaseGrammar",
    "schema": "${DB_SCHEMA}",
    "connectionInfo": {
        "class": "${DB_CLASS}",
        "connectionString": "${DB_CONNECTIONSTRING}",
        "username": "${DB_USER}",
        "password": "${DB_PASSWORD}"
    }
}

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.

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.

The schema represents the schema to install the migrations in. This is a very important field, especially for database setups hosting mutiple schemas. Without it, commandbox-migrations will be unable to correct detect the migrations table. It may tell you that the migration table is already installed when it isn't because it detects it in a different schema.

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.)

commandbox-migrations will create a datasource named cfmigrations from the information you specify. You can use this in your queries:

queryExecute(
    "
        CREATE TABLE `users` (
            `id` INT UNSIGNED AUTO_INCREMENT,
            `email` VARCHAR(255) NOT NULL,
            `password` VARCHAR(255) NOT NULL
        )
    ",
    [],
    { datasource = "cfmigrations" }
)

commandbox-migrations will also set cfmigrations as the default datasource, so the following will work as well:

queryExecute( "
    CREATE TABLE `users` (
        `id` INT UNSIGNED AUTO_INCREMENT,
         `email` VARCHAR(255) NOT NULL,
         `password` VARCHAR(255) NOT NULL
    )
" );

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.

For local development using CommandBox, I recommend using the package 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 commandbox-migrations.

With commandbox-dotenv installed, create a .env file in the root of you project. At the very least, it will look like this:

# MYSQL VERSION
DB_SCHEMA=test_db
DB_DATABASE=test_db
DB_CLASS=org.gjt.mm.mysql.Driver
DB_CONNECTIONSTRING=jdbc:mysql: //localhost:3306/test_db?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true
DB_USER=test
DB_PASSWORD=pass1234
# MSSQL VERSION
DB_SCHEMA=dbo
DB_DATABASE=test_db
DB_CLASS=com.microsoft.sqlserver.jdbc.SQLServerDriver
DB_CONNECTIONSTRING=jdbc:sqlserver: //localhost:1433;DATABASENAME=test_db;sendStringParametersAsUnicode=true;SelectMethod=direct
DB_USER=test
DB_PASSWORD=pass1234

I recommend adding this file to your .gitignore

.env

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:

DB_SCHEMA=
DB_DATABASE=
DB_CLASS=
DB_CONNECTIONSTRING=
DB_USER=
DB_PASSWORD=

You would update your .gitignore file to not ignore the .env.example file:

.env
!.env.example

Usage

migrate init

Creates the migration struct in your box.json, if it doesn't already exist.

migrate install

Installs the migration table in to your database. This migration table keeps track of the ran migrations.

migrate create [name]

Creates a migration file with an up and down method. The file name will be prepended with the current timestamp in the format that cfmigrations expects.

migrate up [--once]

Runs all available migrations up. Passing the --once flag will only run a single migration up (if any are available).

migrate down [--once]

Runs all available migrations down. Passing the --once flag will only run a single migration down (if any are available).

migrate refresh

Runs all available migrations down and then runs all migrations up.

migrate reset

Clears out all objects from the database, including the cfmigrations table. Use this when your database is in an inconsistent state in development.

migrate fresh

Runs migrate reset, migrate install, and migrate up to give you a fresh copy of your migrated database.

migrate uninstall

Removes the cfmigrations table after running down any ran migrations.

https://github.com/coldbox-modules/commandbox-migrations

Here are all the versions for this package. Please note that you can leverage CommandBox package versioning to install any package you like. Please refer to our managing package version guide for more information.

Version Created Last Update Published By Stable Actions
Current
1.5.2 Apr 16 2019 06:21 PM Apr 16 2019 06:21 PM
Version History
1.5.1 Sep 17 2018 04:21 PM Apr 16 2019 06:21 PM
1.5.0 Sep 17 2018 03:31 PM Sep 17 2018 03:31 PM
1.4.3 Sep 17 2018 04:06 PM Sep 17 2018 04:06 PM
1.4.2 Jul 10 2018 12:12 PM Jul 10 2018 12:12 PM
1.4.1 Jul 10 2018 09:07 AM Jul 10 2018 09:07 AM
1.4.0 Jul 10 2018 09:06 AM Jul 10 2018 09:06 AM
1.3.4 Jun 25 2018 10:32 AM Jun 25 2018 10:32 AM
1.3.3 Apr 27 2018 11:21 PM Apr 27 2018 11:21 PM
1.3.2 Apr 25 2018 12:19 PM Apr 25 2018 12:19 PM
1.3.0 Apr 16 2018 10:58 AM Apr 16 2018 10:58 AM
1.2.0 Feb 16 2018 04:29 PM Feb 16 2018 04:29 PM
1.1.1 Feb 16 2018 01:02 PM Feb 16 2018 01:02 PM
1.1.0 Feb 15 2018 01:21 PM Feb 15 2018 01:21 PM
1.0.4 Nov 22 2017 11:12 AM Nov 22 2017 11:12 AM
1.0.3 Nov 13 2017 11:24 AM Nov 13 2017 11:24 AM
1.0.2 Oct 27 2017 02:35 AM Oct 27 2017 02:35 AM
1.0.1 Oct 27 2017 01:58 AM Oct 27 2017 01:58 AM
1.0.0 Oct 27 2017 01:51 AM Oct 27 2017 01:51 AM

 

     
  • Oct 27 2017 01:51 AM
  • Apr 16 2019 06:21 PM
  • 1430
  • 0
  • 2779