CLI / Migrations

Sequelize 2.0.0 introduces a new CLI which is based on gulp and combines sequelize-cli and gulp-sequelize. The CLI ships support for migrations and project bootstrapping. With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe the way how to get to the new state and how to revert the changes in order to get back to the old state.

The CLI

npm install --save sequelize-cli

The CLI currently supports the following commands:

sequelize db:migrate        # Run pending migrations.
sequelize db:migrate:undo   # Revert the last migration run.
sequelize help              # Display this help text.
sequelize init              # Initializes the project.
sequelize migration:create  # Generates a new migration file.
sequelize version           # Prints the version number.

Further and more detailled information about the available commands can be obtained via the help command:

sequelize help:init
sequelize help:db:migrate
sequelize help:db:migrate:undo
# etc

The latter one for example will print out the following output:

Sequelize [CLI: v0.0.2, ORM: v1.7.5]

COMMANDS
    sequelize db:migrate:undo -- Revert the last migration run.

DESCRIPTION
    Revert the last migration run.

OPTIONS
    --env           The environment to run the command in. Default: development
    --options-path  The path to a JSON file with additional options. Default: none
    --coffee        Enables coffee script support. Default: false
    --config        The path to the config file. Default: config/config.json

Skeleton

The following skeleton shows a typical migration file. All migrations are expected to be located in a folder calledmigrationsat the very top of the project. Sequelize1.4.1added the possibility to let the sequelize binary generate a migration skeleton. See the aboves section for more details.

module.exports = {
  up: function(migration, DataTypes, done) {
    // logic for transforming into the new state
    done() // sets the migration as finished
  },
 
  down: function(migration, DataTypes, done) {
    // logic for reverting the changes
    done() // sets the migration as finished
  }
}

The passedmigrationobject can be used to modify the database. TheDataTypesobject stores the available data types such asSTRINGorINTEGER. The third parameter is a callback function which needs to be called once everything was executed. The first parameter of the callback function can be used to pass a possible error. In that case, the migration will be marked as failed. Here is some code:

module.exports = {
  up: function(migration, DataTypes, done) {
    migration.dropAllTables().complete(done)
 
    // equals:
    migration.dropAllTables().complete(function(err) {
      if (err) {
        done(err)
      } else {
        done(null)
      }
    })
  }
}

The available methods of the migration object are the following.

Functions

Using themigrationobject describe before, you will have access to most of already introduced functions. Furthermore there are some other methods, which are designed to actually change the database schema.

createTable(tableName, attributes, options)

This method allows creation of new tables. It is allowed to pass simple or complex attribute definitions. You can define the encoding of the table and the table's engine via options

migration.createTable(
  'nameOfTheNewTable',
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    createdAt: {
      type: DataTypes.DATE
    },
    updatedAt: {
      type: DataTypes.DATE
    },
    attr1: DataTypes.STRING,
    attr2: DataTypes.INTEGER,
    attr3: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      allowNull: false
    }
  },
  {
    engine: 'MYISAM', // default: 'InnoDB'
    charset: 'latin1' // default: null
  }
)

dropTable(tableName)

This method allows deletion of an existing table.

migration.dropTable('nameOfTheExistingTable')

dropAllTables()

This method allows deletion of all existing tables in the database.

migration.dropAllTables()

renameTable(before, after)

This method allows renaming of an existing table.

migration.renameTable('Person', 'User')

showAllTables()

This method returns the name of all existing tables in the database.

migration.showAllTables().success(function(tableNames) {})

describeTable(tableName)

This method returns an array of hashes containing information about all attributes in the table.

migration.describeTable('Person').success(function(attributes) {
  /*
    attributes will be something like:
 
    {
      name: {
        type:         'VARCHAR(255)', // this will be 'CHARACTER VARYING' for pg!
        allowNull:    true,
        defaultValue: null
      },
      isBetaMember: {
        type:         'TINYINT(1)', // this will be 'BOOLEAN' for pg!
        allowNull:    false,
        defaultValue: false
      }
    }
  */
})

addColumn(tableName, attributeName, dataTypeOrOptions)

This method allows adding columns to an existing table. The data type can be simple or complex.

migration.addColumn(
  'nameOfAnExistingTable',
  'nameOfTheNewAttribute',
  DataTypes.STRING
)
 
// or
 
migration.addColumn(
  'nameOfAnExistingTable',
  'nameOfTheNewAttribute',
  {
    type: DataTypes.STRING,
    allowNull: false
  }
)

removeColumn(tableName, attributeName)

This method allows deletion of a specific column of an existing table.

migration.removeColumn('Person', 'signature')

changeColumn(tableName, attributeName, dataTypeOrOptions)

This method changes the meta data of an attribute. It is possible to change the default value, allowance of null or the data type. Please make sure, that you are completely describing the new data type. Missing information are expected to be defaults.

migration.changeColumn(
  'nameOfAnExistingTable',
  'nameOfAnExistingAttribute',
  DataTypes.STRING
)
 
// or
 
migration.changeColumn(
  'nameOfAnExistingTable',
  'nameOfAnExistingAttribute',
  {
    type: DataTypes.FLOAT,
    allowNull: false,
    default: 0.0
  }
)

renameColumn(tableName, attrNameBefore, attrNameAfter)

This methods allows renaming attributes.

migration.renameColumn('Person', 'signature', 'sig')

addIndex(tableName, attributes, options)

This methods creates indexes for specific attributes of a table. The index name will be automatically generated if it is not passed via in the options (see below).

// This example will create the index person_firstname_lastname
migration.addIndex('Person', ['firstname', 'lastname'])
// This example will create a unique index with the name SuperDuperIndex using the optional 'options' field.
// Possible options:
// - indicesType: UNIQUE|FULLTEXT|SPATIAL
// - indexName: The name of the index. Default is __
// - parser: For FULLTEXT columns set your parser
// - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect
migration.addIndex(
  'Person',
  ['firstname', 'lastname'],
  {
    indexName: 'SuperDuperIndex',
    indicesType: 'UNIQUE'
  }
)

removeIndex(tableName, indexNameOrAttributes)

This method deletes an existing index of a table.

migration.removeIndex('Person', 'SuperDuperIndex')
 
// or
 
migration.removeIndex('Person', ['firstname', 'lastname'])

Programmatic use

If you need to interact with the migrator within your code, you can easily achieve that via sequelize.getMigrator. You can specify the path to your migrations as well as a pattern which represents the files that contain the migrations.

var migrator = sequelize.getMigrator({
  path:        process.cwd() + '/database/migrations',
  filesFilter: /\.coffee$/
})

Once you have a migrator object, you can run its migration with migrator.migrate. By default, this will execute all the up methods within your pending migrations. If you want to rollback a migration, just call it like this:

migrator
  .migrate({ method: 'down' })
  .success(function() {
    // The migrations have been executed!
  })
© Sascha Depold, et al. 2006 - 2022