-
Notifications
You must be signed in to change notification settings - Fork 209
Migration Examples
Mitch Conquer edited this page May 17, 2017
·
3 revisions
In order to update the database schema, create a new migration by using the generate migration command and passing in a migration name relevant to your model
nodal g:migration employees
This will generate a new migration file in the format timestamp_employees.js that you can edit.
After editing the migration file use the db:migrate command to commit the change to the database
nodal db:migrate
If you need to roll a migration back.
nodal db:rollback
The available methods that can be used in the migrations, can be found in the schema_generator.js file:
nodal/core/required/db/schema_generator.js
up() {
return [
this.addColumn("employees","address1","string",{"nullable":false})
];
}
down() {
return [
this.dropColumn("employees","address1")
];
}
up() {
return [
this.renameColumn("employees","first_name","firstName"),
this.renameColumn("employees","last_name","lastName")
];
}
down() {
return [
this.renameColumn("employees","firstName","first_name"),
this.renameColumn("employees","lastName","last_name")
];
}
up() {
return [
this.alterColumn("employees","first_name","string",{"nullable":false,"unique":true})
];
}
down() {
return [
this.alterColumn("employees","first_name","string","")
];
}
up() {
return [
this.addColumn("employees","company_id","int"),
this.addForeignKey("employees","companies")
];
}
down() {
return [
this.dropForeignKey("employees","companies"),
this.dropColumn("employees","company_id")
];
}
Follows the signature this.createTable(tableName, arrayFieldData, modelName)
.
up() {
return [
this.createTable(
"employees",
[{
"name": "name",
"type": "string",
"properties": {"nullable":false, "primary_key": false, "auto_increment": false}
},{
"name": "start_date",
"type": "datetime",
"properties": {}
}],
"Employee"
)
];
}
down() {
return [
this.dropTable("employees")
];
}