Skip to content

Commit 39d50f3

Browse files
umer-ccUmerMIB
authored andcommitted
update migration.md to keep migration info at first place
1 parent c236a03 commit 39d50f3

File tree

1 file changed

+93
-92
lines changed

1 file changed

+93
-92
lines changed

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

Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,36 @@ title: Migrations
44

55
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

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:
148

159
```text
1610
npm install --save-dev sequelize-cli
1711
```
1812

19-
For details see the [CLI GitHub repository](https://github.com/sequelize/cli).
2013

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.
2215

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.
2419

2520
```text
26-
npx sequelize-cli init
21+
npx sequelize-cli migration:generate --name migration-skeleton
2722
```
2823

29-
This will create following folders
30-
31-
- `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.
3925

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
5530
},
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
6233
}
6334
}
6435
```
6536

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-
9637
## Running Migrations
9738

9839
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:
12364
npx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js
12465
```
12566

126-
## Creating the first Seed
67+
## Creating the Model (and Migration)
68+
69+
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
12792

12893
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.
12994

@@ -192,25 +157,6 @@ npx sequelize-cli db:seed:undo:all
192157

193158
## Migration Skeleton
194159

195-
The following skeleton shows a typical migration file.
196-
197-
```js
198-
module.exports = {
199-
up: (queryInterface, Sequelize) => {
200-
// logic for transforming into the new state
201-
},
202-
down: (queryInterface, Sequelize) => {
203-
// logic for reverting the changes
204-
}
205-
}
206-
```
207-
208-
We can generate this file using `migration:generate`. This will create `xxx-migration-skeleton.js` in your migration folder.
209-
210-
```text
211-
npx sequelize-cli migration:generate --name migration-skeleton
212-
```
213-
214160
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:
215161

216162
```js
@@ -360,6 +306,61 @@ module.exports = {
360306
}
361307
```
362308

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+
363364
### The `.sequelizerc` file
364365

365366
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

Comments
 (0)