You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-6.x.x/other-topics/migrations.md
+32-32Lines changed: 32 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
title: Migrations
3
3
---
4
4
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.
6
6
7
7
You will need the [Sequelize Command-Line Interface (CLI)](https://github.com/sequelize/cli). The CLI ships support for migrations and project bootstrapping.
8
8
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.
10
10
11
11
## Installing the CLI
12
12
@@ -28,14 +28,14 @@ npx sequelize-cli init
28
28
29
29
This will create following folders
30
30
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
32
32
-`models`, contains all models for your project
33
33
-`migrations`, contains all migration files
34
34
-`seeders`, contains all seed files
35
35
36
36
### Configuration
37
37
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:
39
39
40
40
```json
41
41
{
@@ -63,17 +63,17 @@ Before continuing further we will need to tell the CLI how to connect to the dat
63
63
}
64
64
```
65
65
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.
67
67
68
68
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).
69
69
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).
71
71
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._
73
73
74
74
## Creating the first Model (and Migration)
75
75
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.
77
77
78
78
We will use `model:generate` command. This command requires two options:
- 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.
93
93
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._
- 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.
108
108
- Creates a table called `Users` with all columns as specified in its migration file.
109
109
110
110
## Undoing Migrations
111
111
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.
113
113
114
114
You can use `db:migrate:undo`, this command will revert the most recent migration.
115
115
116
116
```text
117
117
npx sequelize-cli db:migrate:undo
118
118
```
119
119
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.
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.
129
129
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.
131
131
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.
133
133
134
134
```text
135
135
npx sequelize-cli seed:generate --name demo-user
136
136
```
137
137
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.
139
139
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.
141
141
142
142
```js
143
143
module.exports= {
@@ -160,7 +160,7 @@ module.exports = {
160
160
161
161
## Running Seeds
162
162
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.
164
164
165
165
```text
166
166
npx sequelize-cli db:seed:all
@@ -213,7 +213,7 @@ We can generate this file using `migration:generate`. This will create `xxx-migr
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:
217
217
218
218
```js
219
219
module.exports= {
@@ -233,7 +233,7 @@ module.exports = {
233
233
};
234
234
```
235
235
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 automaticallymanaged transaction to ensure that all instructions are successfully executed or rolled back in case of failure:
237
237
238
238
```js
239
239
module.exports= {
@@ -303,7 +303,7 @@ module.exports = {
303
303
};
304
304
```
305
305
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:
307
307
308
308
```js
309
309
module.exports= {
@@ -342,7 +342,7 @@ module.exports = {
342
342
};
343
343
```
344
344
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:
346
346
347
347
```js
348
348
module.exports= {
@@ -383,7 +383,7 @@ This is a special configuration file. It lets you specify the following options
383
383
384
384
Some scenarios where you can use it:
385
385
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.
387
387
- You want to rename `config.json` to something else like `database.json`
388
388
389
389
And a whole lot more. Let's see how you can use this file for custom configuration.
@@ -403,18 +403,18 @@ module.exports = {
403
403
};
404
404
```
405
405
406
-
With this config you are telling the CLI to:
406
+
With this config, you are telling the CLI to:
407
407
408
-
- Use `config/database.json` file for config settings;
408
+
- Use the `config/database.json` file for config settings;
409
409
- Use `db/models` as models folder;
410
410
- Use `db/seeders` as seeders folder;
411
-
- Use `db/migrations` as migrations folder.
411
+
- Use `db/migrations` as the migrations folder.
412
412
413
413
### Dynamic configuration
414
414
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.
416
416
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:
418
418
419
419
```js
420
420
constpath=require('path');
@@ -512,7 +512,7 @@ There are three types of storage that you can use: `sequelize`, `json`, and `non
512
512
513
513
#### Migration Storage
514
514
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.
516
516
517
517
```json
518
518
{
@@ -542,7 +542,7 @@ By default the CLI will create a table in your database called `SequelizeMeta` c
542
542
543
543
#### Seed Storage
544
544
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`.
546
546
547
547
```json
548
548
{
@@ -588,4 +588,4 @@ Use the command like so: `npm run migrate:up -- --url <url>`
588
588
589
589
### Programmatic usage
590
590
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