Skip to content

Commit f4ea707

Browse files
authored
Update migrations.md (#707)
1 parent 1f460bd commit f4ea707

1 file changed

Lines changed: 32 additions & 32 deletions

File tree

versioned_docs/version-6.x.x/other-topics/migrations.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
title: Migrations
33
---
44

5-
Just like you use [version control](https://en.wikipedia.org/wiki/Version_control) systems such as [Git](https://en.wikipedia.org/wiki/Git) to manage changes in your source code, you can use **migrations** to keep track of changes to the database. With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state.
5+
Just like you use [version control](https://en.wikipedia.org/wiki/Version_control) systems such as [Git](https://en.wikipedia.org/wiki/Git) to manage changes in your source code, you can use **migrations** to keep track of changes to the database. With migrations, you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state.
66

77
You will need the [Sequelize Command-Line Interface (CLI)](https://github.com/sequelize/cli). The CLI ships support for migrations and project bootstrapping.
88

9-
A Migration in Sequelize is a javascript file which exports two functions, `up` and `down`, that dictates how to perform the migration and undo it. You define those functions manually, but you don't call them manually; they will be called automatically by the CLI. In these functions, you should simply perform whatever queries you need, with the help of `sequelize.query` and whichever other methods Sequelize provides to you. There is no extra magic beyond that.
9+
A Migration in Sequelize is a javascript file that exports two functions, `up` and `down`, that dictate how to perform the migration and undo it. You define those functions manually, but you don't call them manually; they will be called automatically by the CLI. In these functions, you should simply perform whatever queries you need, with the help of `sequelize.query` and whichever other methods Sequelize provides to you. There is no extra magic beyond that.
1010

1111
## Installing the CLI
1212

@@ -28,14 +28,14 @@ npx sequelize-cli init
2828

2929
This will create following folders
3030

31-
- `config`, contains config file, which tells CLI how to connect with database
31+
- `config`, contains the config file, which tells CLI how to connect with the database
3232
- `models`, contains all models for your project
3333
- `migrations`, contains all migration files
3434
- `seeders`, contains all seed files
3535

3636
### Configuration
3737

38-
Before continuing further we will need to tell the CLI how to connect to the database. To do that let's open default config file `config/config.json`. It looks something like this:
38+
Before continuing further, we must tell the CLI how to connect to the database. To do that let's open the default config file `config/config.json`. It looks something like this:
3939

4040
```json
4141
{
@@ -63,17 +63,17 @@ Before continuing further we will need to tell the CLI how to connect to the dat
6363
}
6464
```
6565

66-
Note that the Sequelize CLI assumes mysql by default. If you're using another dialect, you need to change the content of the `"dialect"` option.
66+
Note that the Sequelize CLI assumes MySQL by default. If you're using another dialect, you need to change the content of the `"dialect"` option.
6767

6868
Now edit this file and set correct database credentials and dialect. The keys of the objects (e.g. "development") are used on `model/index.js` for matching `process.env.NODE_ENV` (When undefined, "development" is a default value).
6969

70-
Sequelize will use the default connection port for each dialect (for example, for postgres, it is port 5432). If you need to specify a different port, use the `"port"` field (it is not present by default in `config/config.js` but you can simply add it).
70+
Sequelize will use the default connection port for each dialect (for example, for Postgres, it is port 5432). If you need to specify a different port, use the `"port"` field (it is not present by default in `config/config.js` but you can simply add it).
7171

72-
**Note:** _If your database doesn't exist yet, you can just call `db:create` command. With proper access it will create that database for you._
72+
**Note:** _If your database doesn't exist yet, you can just call `db:create` command. With proper access, it will create that database for you._
7373

7474
## Creating the first Model (and Migration)
7575

76-
Once you have properly configured CLI config file you are ready to create your first migration. It's as simple as executing a simple command.
76+
Once you have properly configured the CLI config file you are ready to create your first migration. It's as simple as executing a simple command.
7777

7878
We will use `model:generate` command. This command requires two options:
7979

@@ -89,9 +89,9 @@ npx sequelize-cli model:generate --name User --attributes firstName:string,lastN
8989
This will:
9090

9191
- Create a model file `user` in `models` folder;
92-
- Create a migration file with name like `XXXXXXXXXXXXXX-create-user.js` in `migrations` folder.
92+
- Create a migration file with a name like `XXXXXXXXXXXXXX-create-user.js` in the `migrations` folder.
9393

94-
**Note:** _Sequelize will only use Model files, it's the table representation. On the other hand, the migration file is a change in that model or more specifically that table, used by CLI. Treat migrations like a commit or a log for some change in database._
94+
**Note:** _Sequelize will only use Model files, it's the table representation. On the other hand, the migration file is a change in that model or more specifically that table, used by CLI. Treat migrations like a commit or a log for some change in the database._
9595

9696
## Running Migrations
9797

@@ -103,21 +103,21 @@ npx sequelize-cli db:migrate
103103

104104
This command will execute these steps:
105105

106-
- Will ensure a table called `SequelizeMeta` in database. This table is used to record which migrations have run on the current database
107-
- Start looking for any migration files which haven't run yet. This is possible by checking `SequelizeMeta` table. In this case it will run `XXXXXXXXXXXXXX-create-user.js` migration, which we created in last step.
106+
- Will ensure a table called `SequelizeMeta` in the database. This table is used to record which migrations have run on the current database
107+
- Start looking for any migration files that haven't run yet. This is possible by checking the `SequelizeMeta` table. In this case, it will run the `XXXXXXXXXXXXXX-create-user.js` migration, which we created in the last step.
108108
- Creates a table called `Users` with all columns as specified in its migration file.
109109

110110
## Undoing Migrations
111111

112-
Now our table has been created and saved in the database. With migration you can revert to old state by just running a command.
112+
Now our table has been created and saved in the database. With migration, you can revert to the old state by just running a command.
113113

114114
You can use `db:migrate:undo`, this command will revert the most recent migration.
115115

116116
```text
117117
npx sequelize-cli db:migrate:undo
118118
```
119119

120-
You can revert back to the initial state by undoing all migrations with the `db:migrate:undo:all` command. You can also revert back to a specific migration by passing its name with the `--to` option.
120+
You can revert to the initial state by undoing all migrations with the `db:migrate:undo:all` command. You can also revert to a specific migration by passing its name with the `--to` option.
121121

122122
```text
123123
npx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js
@@ -127,17 +127,17 @@ npx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js
127127

128128
Suppose we want to insert some data into a few tables by default. If we follow up on the previous example we can consider creating a demo user for the `User` table.
129129

130-
To manage all data migrations you can use seeders. Seed files are some change in data that can be used to populate database tables with sample or test data.
130+
To manage all data migrations you can use seeders. Seed files are some changes in data that can be used to populate database tables with sample or test data.
131131

132-
Let's create a seed file which will add a demo user to our `User` table.
132+
Let's create a seed file that will add a demo user to our `User` table.
133133

134134
```text
135135
npx sequelize-cli seed:generate --name demo-user
136136
```
137137

138-
This command will create a seed file in `seeders` folder. File name will look something like `XXXXXXXXXXXXXX-demo-user.js`. It follows the same `up / down` semantics as the migration files.
138+
This command will create a seed file in the `seeders` folder. The file name will look something like `XXXXXXXXXXXXXX-demo-user.js`. It follows the same `up / down` semantics as the migration files.
139139

140-
Now we should edit this file to insert demo user to `User` table.
140+
Now we should edit this file to insert the demo user to the `User` table.
141141

142142
```js
143143
module.exports = {
@@ -160,7 +160,7 @@ module.exports = {
160160

161161
## Running Seeds
162162

163-
In last step you created a seed file; however, it has not been committed to the database. To do that we run a simple command.
163+
In the last step you created a seed file; however, it has not been committed to the database. To do that we run a simple command.
164164

165165
```text
166166
npx sequelize-cli db:seed:all
@@ -213,7 +213,7 @@ We can generate this file using `migration:generate`. This will create `xxx-migr
213213
npx sequelize-cli migration:generate --name migration-skeleton
214214
```
215215

216-
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`. Let's look at an example:
216+
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 function `up` or `down` should return a `Promise`. Let's look at an example:
217217

218218
```js
219219
module.exports = {
@@ -233,7 +233,7 @@ module.exports = {
233233
};
234234
```
235235

236-
The following is an example of a migration that performs two changes in the database, using an automatically-managed transaction to ensure that all instructions are successfully executed or rolled back in case of failure:
236+
The following is an example of a migration that performs two changes in the database, using an automatically managed transaction to ensure that all instructions are successfully executed or rolled back in case of failure:
237237

238238
```js
239239
module.exports = {
@@ -303,7 +303,7 @@ module.exports = {
303303
};
304304
```
305305

306-
The next example is of a migration that uses async/await where you create an unique index on a new column, with a manually-managed transaction:
306+
The next example is of a migration that uses async/awaits where you create a unique index on a new column, with a manually-managed transaction:
307307

308308
```js
309309
module.exports = {
@@ -342,7 +342,7 @@ module.exports = {
342342
};
343343
```
344344

345-
The next example is of a migration that creates an unique index composed of multiple fields with a condition, which allows a relation to exist multiple times but only one can satisfy the condition:
345+
The next example is of a migration that creates a unique index composed of multiple fields with a condition, which allows a relation to exist multiple times but only one can satisfy the condition:
346346

347347
```js
348348
module.exports = {
@@ -383,7 +383,7 @@ This is a special configuration file. It lets you specify the following options
383383

384384
Some scenarios where you can use it:
385385

386-
- You want to override default path to `migrations`, `models`, `seeders` or `config` folder.
386+
- You want to override the default path to the `migrations`, `models`, `seeders`, or `config` folder.
387387
- You want to rename `config.json` to something else like `database.json`
388388

389389
And a whole lot more. Let's see how you can use this file for custom configuration.
@@ -403,18 +403,18 @@ module.exports = {
403403
};
404404
```
405405

406-
With this config you are telling the CLI to:
406+
With this config, you are telling the CLI to:
407407

408-
- Use `config/database.json` file for config settings;
408+
- Use the `config/database.json` file for config settings;
409409
- Use `db/models` as models folder;
410410
- Use `db/seeders` as seeders folder;
411-
- Use `db/migrations` as migrations folder.
411+
- Use `db/migrations` as the migrations folder.
412412

413413
### Dynamic configuration
414414

415-
The configuration file is by default a JSON file called `config.json`. But sometimes you need a dynamic configuration, for example to access environment variables or execute some other code to determine the configuration.
415+
The configuration file is by default a JSON file called `config.json`. But sometimes you need a dynamic configuration, for example, to access environment variables or execute some other code to determine the configuration.
416416

417-
Thankfully, the Sequelize CLI can read from both `.json` and `.js` files. This can be setup with `.sequelizerc` file. You just have to provide the path to your `.js` file as the `config` option of your exported object:
417+
Thankfully, the Sequelize CLI can read from both `.json` and `.js` files. This can be setup with the `.sequelizerc` file. You just have to provide the path to your `.js` file as the `config` option of your exported object:
418418

419419
```js
420420
const path = require('path');
@@ -512,7 +512,7 @@ There are three types of storage that you can use: `sequelize`, `json`, and `non
512512

513513
#### Migration Storage
514514

515-
By default the CLI will create a table in your database called `SequelizeMeta` containing an entry for each executed migration. To change this behavior, there are three options you can add to the configuration file. Using `migrationStorage`, you can choose the type of storage to be used for migrations. If you choose `json`, you can specify the path of the file using `migrationStoragePath` or the CLI will write to the file `sequelize-meta.json`. If you want to keep the information in the database, using `sequelize`, but want to use a different table, you can change the table name using `migrationStorageTableName`. Also you can define a different schema for the `SequelizeMeta` table by providing the `migrationStorageTableSchema` property.
515+
By default, the CLI will create a table in your database called `SequelizeMeta` containing an entry for each executed migration. To change this behavior, there are three options you can add to the configuration file. Using `migrationStorage`, you can choose the type of storage to be used for migrations. If you choose `json`, you can specify the path of the file using `migrationStoragePath` or the CLI will write to the file `sequelize-meta.json`. If you want to keep the information in the database, using `sequelize`, but want to use a different table, you can change the table name using `migrationStorageTableName`. Also, you can define a different schema for the `SequelizeMeta` table by providing the `migrationStorageTableSchema` property.
516516

517517
```json
518518
{
@@ -542,7 +542,7 @@ By default the CLI will create a table in your database called `SequelizeMeta` c
542542

543543
#### Seed Storage
544544

545-
By default the CLI will not save any seed that is executed. If you choose to change this behavior (!), you can use `seederStorage` in the configuration file to change the storage type. If you choose `json`, you can specify the path of the file using `seederStoragePath` or the CLI will write to the file `sequelize-data.json`. If you want to keep the information in the database, using `sequelize`, you can specify the table name using `seederStorageTableName`, or it will default to `SequelizeData`.
545+
By default, the CLI will not save any seed that is executed. If you choose to change this behavior (!), you can use `seederStorage` in the configuration file to change the storage type. If you choose `json`, you can specify the path of the file using `seederStoragePath` or the CLI will write to the file `sequelize-data.json`. If you want to keep the information in the database, using `sequelize`, you can specify the table name using `seederStorageTableName`, or it will default to `SequelizeData`.
546546

547547
```json
548548
{
@@ -588,4 +588,4 @@ Use the command like so: `npm run migrate:up -- --url <url>`
588588

589589
### Programmatic usage
590590

591-
Sequelize has a sister library called [umzug](https://github.com/sequelize/umzug) for programmatically handling execution and logging of migration tasks.
591+
Sequelize has a sister library called [umzug](https://github.com/sequelize/umzug) for programmatically handling the execution and logging of migration tasks.

0 commit comments

Comments
 (0)