Skip to content
Mitch Conquer edited this page May 17, 2017 · 3 revisions

Nodal Migration Examples

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

Adding Columns

up() {

      return [
          this.addColumn("employees","address1","string",{"nullable":false})
        ];

    }

down() {

      return [
        this.dropColumn("employees","address1")
      ];

    }

Renaming Multiple Columns

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")
      ];

    }

Changing column properties

up() {

      return [
          this.alterColumn("employees","first_name","string",{"nullable":false,"unique":true})
      ];

    }

    down() {

      return [
          this.alterColumn("employees","first_name","string","")
      ];

    }

Adding Foreign Keys

up() {

      return [
          this.addColumn("employees","company_id","int"),
          this.addForeignKey("employees","companies")
      ];

    }

    down() {

      return [
          this.dropForeignKey("employees","companies"),
          this.dropColumn("employees","company_id")
      ];

    }

Adding Tables

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")
      ];

    }