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
+93-92Lines changed: 93 additions & 92 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,95 +4,36 @@ title: Migrations
4
4
5
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
-
You will need the [Sequelize Command-Line Interface (CLI)](https://github.com/sequelize/cli). The CLI ships support for migrations and project bootstrapping.
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.
10
-
11
-
## Installing the CLI
12
-
13
-
To install the Sequelize CLI:
7
+
You will need the [Sequelize Command-Line Interface (CLI)](https://github.com/sequelize/cli). The CLI ships support for migrations and project bootstrapping. To install the Sequelize CLI:
14
8
15
9
```text
16
10
npm install --save-dev sequelize-cli
17
11
```
18
12
19
-
For details see the [CLI GitHub repository](https://github.com/sequelize/cli).
20
13
21
-
## Project bootstrapping
14
+
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.
22
15
23
-
To create an empty project you will need to execute `init` command
16
+
## Creating the migration
17
+
18
+
We can generate this file using `migration:generate`. This will create `xxx-migration-skeleton.js` in your migration folder.
-`config`, contains config file, which tells CLI how to connect with database
32
-
-`models`, contains all models for your project
33
-
-`migrations`, contains all migration files
34
-
-`seeders`, contains all seed files
35
-
36
-
### Configuration
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:
24
+
The following skeleton shows a typical migration file.
39
25
40
-
```json
41
-
{
42
-
"development": {
43
-
"username": "root",
44
-
"password": null,
45
-
"database": "database_development",
46
-
"host": "127.0.0.1",
47
-
"dialect": "mysql"
48
-
},
49
-
"test": {
50
-
"username": "root",
51
-
"password": null,
52
-
"database": "database_test",
53
-
"host": "127.0.0.1",
54
-
"dialect": "mysql"
26
+
```js
27
+
module.exports= {
28
+
up: (queryInterface, Sequelize) => {
29
+
// logic for transforming into the new state
55
30
},
56
-
"production": {
57
-
"username": "root",
58
-
"password": null,
59
-
"database": "database_production",
60
-
"host": "127.0.0.1",
61
-
"dialect": "mysql"
31
+
down: (queryInterface, Sequelize) => {
32
+
// logic for reverting the changes
62
33
}
63
34
}
64
35
```
65
36
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
-
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
-
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
-
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
-
74
-
## Creating the first Model (and Migration)
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.
77
-
78
-
We will use `model:generate` command. This command requires two options:
79
-
80
-
-`name`: the name of the model;
81
-
-`attributes`: the list of model attributes.
82
-
83
-
Let's create a model named `User`.
84
-
85
-
```text
86
-
npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string
87
-
```
88
-
89
-
This will:
90
-
91
-
- Create a model file `user` in `models` folder;
92
-
- Create a migration file with name like `XXXXXXXXXXXXXX-create-user.js` in `migrations` folder.
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._
95
-
96
37
## Running Migrations
97
38
98
39
Until this step, we haven't inserted anything into the database. We have just created the required model and migration files for our first model, `User`. Now to actually create that table in the database you need to run `db:migrate` command.
@@ -123,7 +64,31 @@ You can revert back to the initial state by undoing all migrations with the `db:
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.
70
+
71
+
We will use `model:generate` command. This command requires two options:
72
+
73
+
-`name`: the name of the model;
74
+
-`attributes`: the list of model attributes.
75
+
76
+
Let's create a model named `User`.
77
+
78
+
```text
79
+
npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string
80
+
```
81
+
82
+
This will:
83
+
84
+
- Create a model file `user` in `models` folder;
85
+
- Create a migration file with name like `XXXXXXXXXXXXXX-create-user.js` in `migrations` folder.
86
+
87
+
**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._
88
+
89
+
90
+
91
+
## Creating the Seed
127
92
128
93
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.
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:
215
161
216
162
```js
@@ -360,6 +306,61 @@ module.exports = {
360
306
}
361
307
```
362
308
309
+
## Project bootstrapping
310
+
311
+
To create an empty project you will need to execute `init` command
312
+
313
+
```text
314
+
npx sequelize-cli init
315
+
```
316
+
317
+
This will create following folders
318
+
319
+
-`config`, contains config file, which tells CLI how to connect with database
320
+
-`models`, contains all models for your project
321
+
-`migrations`, contains all migration files
322
+
-`seeders`, contains all seed files
323
+
324
+
### Configuration
325
+
326
+
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:
327
+
328
+
```json
329
+
{
330
+
"development": {
331
+
"username": "root",
332
+
"password": null,
333
+
"database": "database_development",
334
+
"host": "127.0.0.1",
335
+
"dialect": "mysql"
336
+
},
337
+
"test": {
338
+
"username": "root",
339
+
"password": null,
340
+
"database": "database_test",
341
+
"host": "127.0.0.1",
342
+
"dialect": "mysql"
343
+
},
344
+
"production": {
345
+
"username": "root",
346
+
"password": null,
347
+
"database": "database_production",
348
+
"host": "127.0.0.1",
349
+
"dialect": "mysql"
350
+
}
351
+
}
352
+
```
353
+
354
+
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.
355
+
356
+
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).
357
+
358
+
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).
359
+
360
+
**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._
361
+
362
+
363
+
363
364
### The `.sequelizerc` file
364
365
365
366
This is a special configuration file. It lets you specify the following options that you would usually pass as arguments to CLI:
0 commit comments