Migrator
down()
Signature
Section titled “Signature”down() — returns void
Available in: migration
Category: Migration Functions
Description
Section titled “Description”Migrates down: will be executed when migrating your schema backward Along with up(), these are the two main functions in any migration file Only available in a migration CFC
Examples
Section titled “Examples”// 1. Reverse a table creation by dropping the table
// Called automatically when rolling back this migration
function down() {
var state = {};
transaction {
try {
dropTable("employees");
} catch (any e) {
state.exception = e;
}
if (structKeyExists(state, "exception")) {
transaction action="rollback";
throw(
errorCode = "1",
detail = state.exception.detail,
message = state.exception.message,
type = "any"
);
} else {
transaction action="commit";
}
}
}
// 2. Reverse an addColumn() call by removing the column
function down() {
var state = {};
transaction {
try {
removeColumn(table="users", columnName="biography");
} catch (any e) {
state.exception = e;
}
if (structKeyExists(state, "exception")) {
transaction action="rollback";
throw(
errorCode = "1",
detail = state.exception.detail,
message = state.exception.message,
type = "any"
);
} else {
transaction action="commit";
}
}
}
// 3. Paired up() and down() inside a full migration component
component extends="[extends]" hint="Add status column to orders" {
function up() {
var state = {};
transaction {
try {
addColumn(table="orders", columnType="string", columnName="status", limit=50, default="pending");
} catch (any e) {
state.exception = e;
}
if (structKeyExists(state, "exception")) {
transaction action="rollback";
throw(
errorCode = "1",
detail = state.exception.detail,
message = state.exception.message,
type = "any"
);
} else {
transaction action="commit";
}
}
}
function down() {
var state = {};
transaction {
try {
removeColumn(table="orders", columnName="status");
} catch (any e) {
state.exception = e;
}
if (structKeyExists(state, "exception")) {
transaction action="rollback";
throw(
errorCode = "1",
detail = state.exception.detail,
message = state.exception.message,
type = "any"
);
} else {
transaction action="commit";
}
}
}
}