Skip to content

Migrator

announce()

announce() — returns any

Available in: migration, tabledefinition Category: Migration Functions

Used internally by Migrator to provide feedback to the GUI and CLI about completed DB operations Only available in a migration CFC

NameTypeRequiredDefaultDescription
messagestringyes—
// 1. Log a custom message during a migration's up() step
function up() {
	var state = {};
	transaction {
		try {
			t = createTable(name="articles", force=true);
			t.string(columnNames="title", allowNull=false);
			t.text(columnNames="body");
			t.timestamps();
			t.create();
			announce("Created articles table");
		} 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. Announce multiple steps to provide granular feedback
function up() {
	var state = {};
	transaction {
		try {
			addColumn(table="users", columnType="string", columnName="apiKey", limit=64, allowNull=true);
			announce("Added apiKey column to users");

			addIndex(table="users", columnNames="apiKey", unique=true);
			announce("Added unique index on users.apiKey");
		} 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";
		}
	}
}