不要怂,就是干,撸起袖子干!

Commit 13708e7e by Mick Hansen

Merge pull request #4589 from williamgueiros/doc-migrations

fix: Created documentation the same way we have for sequelize command.
2 parents 2b8ce0e3 aa7ba618
Showing with 33 additions and 33 deletions
...@@ -55,55 +55,55 @@ The following skeleton shows a typical migration file. All migrations are ...@@ -55,55 +55,55 @@ The following skeleton shows a typical migration file. All migrations are
```js ```js
module.exports = { module.exports = {
up: function(migration, DataTypes) { up: function(queryInterface, Sequelize) {
// logic for transforming into the new state // logic for transforming into the new state
}, },
   
down: function(migration, DataTypes) { down: function(queryInterface, Sequelize) {
// logic for reverting the changes // logic for reverting the changes
} }
} }
``` ```
The passed `migration` object can be used to modify the database. The `DataTypes` object stores the available data types such as `STRING` or `INTEGER`. 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: The passed `queryInterface` object can be used to modify the database. The `Sequelize` object stores the available data types such as `STRING` or `INTEGER`. 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:
```js ```js
module.exports = { module.exports = {
up: function(migration, DataTypes) { up: function(queryInterface, Sequelize) {
return migration.dropAllTables(); return queryInterface.dropAllTables();
} }
} }
``` ```
The available methods of the migration object are the following. The available methods of the queryInterface object are the following.
## Functions ## Functions
Using the `migration` 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. 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) ### 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 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
```js ```js
migration.createTable( queryInterface.createTable(
'nameOfTheNewTable', 'nameOfTheNewTable',
{ {
id: { id: {
type: DataTypes.INTEGER, type: Sequelize.INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
}, },
createdAt: { createdAt: {
type: DataTypes.DATE type: Sequelize.DATE
}, },
updatedAt: { updatedAt: {
type: DataTypes.DATE type: Sequelize.DATE
}, },
attr1: DataTypes.STRING, attr1: Sequelize.STRING,
attr2: DataTypes.INTEGER, attr2: Sequelize.INTEGER,
attr3: { attr3: {
type: DataTypes.BOOLEAN, type: Sequelize.BOOLEAN,
defaultValue: false, defaultValue: false,
allowNull: false allowNull: false
} }
...@@ -120,7 +120,7 @@ migration.createTable( ...@@ -120,7 +120,7 @@ migration.createTable(
This method allows deletion of an existing table. This method allows deletion of an existing table.
```js ```js
migration.dropTable('nameOfTheExistingTable') queryInterface.dropTable('nameOfTheExistingTable')
``` ```
### dropAllTables(options) ### dropAllTables(options)
...@@ -128,7 +128,7 @@ migration.dropTable('nameOfTheExistingTable') ...@@ -128,7 +128,7 @@ migration.dropTable('nameOfTheExistingTable')
This method allows deletion of all existing tables in the database. This method allows deletion of all existing tables in the database.
```js ```js
migration.dropAllTables() queryInterface.dropAllTables()
``` ```
### renameTable(before, after, options) ### renameTable(before, after, options)
...@@ -136,7 +136,7 @@ migration.dropAllTables() ...@@ -136,7 +136,7 @@ migration.dropAllTables()
This method allows renaming of an existing table. This method allows renaming of an existing table.
```js ```js
migration.renameTable('Person', 'User') queryInterface.renameTable('Person', 'User')
``` ```
### showAllTables(options) ### showAllTables(options)
...@@ -144,7 +144,7 @@ migration.renameTable('Person', 'User') ...@@ -144,7 +144,7 @@ migration.renameTable('Person', 'User')
This method returns the name of all existing tables in the database. This method returns the name of all existing tables in the database.
```js ```js
migration.showAllTables().then(function(tableNames) {}) queryInterface.showAllTables().then(function(tableNames) {})
``` ```
### describeTable(tableName, options) ### describeTable(tableName, options)
...@@ -152,7 +152,7 @@ migration.showAllTables().then(function(tableNames) {}) ...@@ -152,7 +152,7 @@ migration.showAllTables().then(function(tableNames) {})
This method returns an array of hashes containing information about all attributes in the table. This method returns an array of hashes containing information about all attributes in the table.
```js ```js
migration.describeTable('Person').then(function(attributes) { queryInterface.describeTable('Person').then(function(attributes) {
/* /*
attributes will be something like: attributes will be something like:
   
...@@ -177,19 +177,19 @@ migration.describeTable('Person').then(function(attributes) { ...@@ -177,19 +177,19 @@ migration.describeTable('Person').then(function(attributes) {
This method allows adding columns to an existing table. The data type can be simple or complex. This method allows adding columns to an existing table. The data type can be simple or complex.
```js ```js
migration.addColumn( queryInterface.addColumn(
'nameOfAnExistingTable', 'nameOfAnExistingTable',
'nameOfTheNewAttribute', 'nameOfTheNewAttribute',
DataTypes.STRING Sequelize.STRING
) )
   
// or // or
   
migration.addColumn( queryInterface.addColumn(
'nameOfAnExistingTable', 'nameOfAnExistingTable',
'nameOfTheNewAttribute', 'nameOfTheNewAttribute',
{ {
type: DataTypes.STRING, type: Sequelize.STRING,
allowNull: false allowNull: false
} }
) )
...@@ -200,7 +200,7 @@ migration.addColumn( ...@@ -200,7 +200,7 @@ migration.addColumn(
This method allows deletion of a specific column of an existing table. This method allows deletion of a specific column of an existing table.
```js ```js
migration.removeColumn('Person', 'signature') queryInterface.removeColumn('Person', 'signature')
``` ```
### changeColumn(tableName, attributeName, dataTypeOrOptions, options) ### changeColumn(tableName, attributeName, dataTypeOrOptions, options)
...@@ -208,19 +208,19 @@ migration.removeColumn('Person', 'signature') ...@@ -208,19 +208,19 @@ migration.removeColumn('Person', 'signature')
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. 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.
```js ```js
migration.changeColumn( queryInterface.changeColumn(
'nameOfAnExistingTable', 'nameOfAnExistingTable',
'nameOfAnExistingAttribute', 'nameOfAnExistingAttribute',
DataTypes.STRING Sequelize.STRING
) )
   
// or // or
   
migration.changeColumn( queryInterface.changeColumn(
'nameOfAnExistingTable', 'nameOfAnExistingTable',
'nameOfAnExistingAttribute', 'nameOfAnExistingAttribute',
{ {
type: DataTypes.FLOAT, type: Sequelize.FLOAT,
allowNull: false, allowNull: false,
default: 0.0 default: 0.0
} }
...@@ -232,7 +232,7 @@ migration.changeColumn( ...@@ -232,7 +232,7 @@ migration.changeColumn(
This methods allows renaming attributes. This methods allows renaming attributes.
```js ```js
migration.renameColumn('Person', 'signature', 'sig') queryInterface.renameColumn('Person', 'signature', 'sig')
``` ```
### addIndex(tableName, attributes, options) ### addIndex(tableName, attributes, options)
...@@ -241,7 +241,7 @@ This methods creates indexes for specific attributes of a table. The inde ...@@ -241,7 +241,7 @@ This methods creates indexes for specific attributes of a table. The inde
```js ```js
// This example will create the index person_firstname_lastname // This example will create the index person_firstname_lastname
migration.addIndex('Person', ['firstname', 'lastname']) queryInterface.addIndex('Person', ['firstname', 'lastname'])
// This example will create a unique index with the name SuperDuperIndex using the optional 'options' field. // This example will create a unique index with the name SuperDuperIndex using the optional 'options' field.
// Possible options: // Possible options:
...@@ -250,7 +250,7 @@ migration.addIndex('Person', ['firstname', 'lastname']) ...@@ -250,7 +250,7 @@ migration.addIndex('Person', ['firstname', 'lastname'])
// - parser: For FULLTEXT columns set your parser // - parser: For FULLTEXT columns set your parser
// - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect // - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect
// - logging: A function that receives the sql query, e.g. console.log // - logging: A function that receives the sql query, e.g. console.log
migration.addIndex( queryInterface.addIndex(
'Person', 'Person',
['firstname', 'lastname'], ['firstname', 'lastname'],
{ {
...@@ -265,11 +265,11 @@ migration.addIndex( ...@@ -265,11 +265,11 @@ migration.addIndex(
This method deletes an existing index of a table. This method deletes an existing index of a table.
```js ```js
migration.removeIndex('Person', 'SuperDuperIndex') queryInterface.removeIndex('Person', 'SuperDuperIndex')
   
// or // or
   
migration.removeIndex('Person', ['firstname', 'lastname']) queryInterface.removeIndex('Person', ['firstname', 'lastname'])
``` ```
## Programmatic use ## Programmatic use
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!