Migrator
up()
Signature
Section titled “Signature”up() — returns void
Available in: migration
Category: Migration Functions
Description
Section titled “Description”Migrates up: will be executed when migrating your schema forward Along with down(), these are the two main functions in any migration file Only available in a migration CFC
Examples
Section titled “Examples”// 1. Create a new table (typical migration forward)
function up() {
var state = {};
transaction {
try {
t = createTable(name="posts");
t.string(columnNames="title", limit=255);
t.text(columnNames="body");
t.boolean(columnNames="published", default=0);
t.timestamps();
t.create();
} 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. Add a column to an existing table
function up() {
var state = {};
transaction {
try {
addColumn(table="users", columnType="string", columnName="avatarUrl", limit=500, allowNull=true);
} 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. Run raw SQL and seed initial data in the same migration
function up() {
var state = {};
transaction {
try {
execute("ALTER TABLE products ADD COLUMN sku VARCHAR(50)");
addRecord(table="settings", key="maintenance_mode", value="false");
} 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";
}
}
}