Database Migrations

Database migrations are mini programs that modify the database schema or content. You can create database migrations via the esp generate migration command. This command generates a migration for a specific database table by creating or removing columns. To generate a migration:

$ esp generate migration description table [field:type ...]

This will create a migration script under the migrations directory. Migration script filenames are time stamped and use the supplied description argument as part of the filename for the migration script (so keep it short and sweet and don't use spaces!).

For each specified field:type pair, esp will generate code to add a column in the migration script. If the --reverse switch is specified, then code to remove a column will be generated. To change the type or name of a column, remove then re-add the column.

Running Migrations

Migration scripts can be run via the esp migrate command. Without other parameters, the command will run all migrations that have not yet been applied to the database. You can also use esp migrate forward to apply the next unapplied migration. Similarly, esp migrate backward will reverse the last applied migration. You can also use esp migrate NNN to migrate forward or backward to a specific migration. NNN is the migration sequence number which is the number at the start of the migration script file name.

Migrations are often under-used. They are much more important than most people realize. Being able to effectively create and manage database test data greatly simplifies this aspect of development and testing.

© Embedthis Software, 2003-2015. All rights reserved.