2. WHAT THIS TALK is
▸ An introduction to qb's SchemaBuilder
▸ Co-locating your app's database structure in your app
▸ How to make changes to your database schema from CommandBox
5. WHAT ARE database migrations?
▸ Changes to your application's database schema
▸ Describes how to apply the change and how to rollback the change
▸ Ran up and down in order
9. WHY Database Migrations?
▸ Co-located inside your app's codebase
▸ Apply schema changes in order
▸ Able to bring up new instances / databases on demand
11. WHY CommandBox?
▸ No worries about exposing access to your database from the web
▸ Scriptable — can run as part of your deploy pipline
▸ Scaffold new migrations with a single command
13. CFMIGRATIONS
CAN BE PULLED IN YOUR APP TO RUN
MIGRATIONS WITHOUT
COMMANDBOX
(USEFUL FOR INTEGRATION TESTING)
14. CONVENTIONS
▸ Migrations are located inside
resources/database/migrations
▸ Migration file names start with the timestamp they were created
(2017_09_03_043150_create_users_table.cfc)
29. SCHEMABUILDER
▸ Is to database structure what QueryBuilder is to queries
▸ Fluent, expressive syntax for describing tables, columns, and
constraints
▸ Bridges the many database idiosyncrasies in this area.
30. SCHEMABUILDER
▸ Not required to use for commandbox-migrations
▸ Also can be used outside of commandbox-migrations
▸ Comes bundled and configured for use because it makes life easier
31. component {
function up( schema, query ) {
// An pre-configured instance
// of `SchemaBuilder` and `QueryBuilder`
// are passed to each migration function
}
function down( schema, query ) {
// feel free to ignore them
// if they aren't your thing
}
}
35. COLUMNS
COLUMNS
bigIncrements bigInteger bit boolean
char date datetime decimal
enum float increments integer
json longText mediumIncrements mediumInteger
mediumText morphs nullableMorphs raw
smallIncrements smallInteger string text
time timestamp tinyIncrements tinyInteger
unsignedBigInteger unsignedInteger unsignedMediumInteger unsignedSmallInteger
unsignedTinyInteger uuid
36. COLUMN MODIFIERS
table.integer( "age" ).nullable();
table.boolean( "is_active" ).default( false );
▸ Can be called on any column
▸ default, nullable, unsigned, comment