Playlistr: Migrate Create

Let's define us a database table!

Migrate Create

With the commandbox-migrations module in place and configured, we can now start to create a migration script. These scripts are used to modify your database tables and columns.

A simple migrate create artists will create the directory ./resources/database/migrations/ and place a migration template file name yyyy_mm_dd_hhmmss_artists.cfc.

With this boilerplate file we can modify it to build our artists table.

The up() method is called by the migrate up and down() is called by migrate down.

In this first example we have hand written the SQL to create and drop our table. Later you we will use qb to perform our database interactions.

Now we have a way to build our artists table and tear it down in a predicable and consistent manner using migrate up and migrate down.

migrate create artists

component {

    function up( schema ) {

    }

    function down( schema ) {

    }

}

yyyy_mm_dd_hhmmss_artists.cfc

component {

    function up( schema ) {
		var sql = "CREATE TABLE `artists` (
			`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
			`name` varchar(255) NOT NULL,
			PRIMARY KEY (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;"

        queryExecute( sql );
    }

    function down( schema ) {
        var sql = "DROP TABLE `artists`;"

        queryExecute( sql );
    }

}