Sequelize `2.0.0` introduces a new CLI which is based on [gulp][0] and combines [sequelize-cli][1] and [gulp-sequelize][2]. 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
In order to use the CLI you need to install the respective package:
```bash
$ npm install --save sequelize-cli
```
As with any npm package, you can use the global flag (`-g`) to install the CLI globally. If you have installed the CLI without the global flag, use `node_modules/.bin/sequelize [command]` instead of `sequelize [command]`.
The CLI currently supports the following commands:
```bash
$ 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 detailed information about the available commands
can be obtained via the help command:
```bash
$ sequelize help:init
$ sequelize help:db:migrate
$ sequelize help:db:migrate:undo
# etc
```
The latter one for example will print out the following output:
```bash
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
--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 called `migrations` at the very top of the project. The sequelize binary can generate a migration skeleton. See the above section for more details.
```js
module.exports = {
up: function(queryInterface, Sequelize) {
// logic for transforming into the new state
},
down: function(queryInterface, Sequelize) {
// logic for reverting the changes
}
}
```
The passed `queryInterface` object can be used to modify the database. The `Sequelize` object stores the available data types such as `STRING` or `INTEGER`. Function `up` or `down` should return a `Promise`. Here is some code:
```js
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.dropAllTables();
}
}
```
The available methods of the queryInterface object are the following.
## Functions
Using the `queryInterface` object 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
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.
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).
```js
// This example will create the index person_firstname_lastname