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
desc: Payload features first-party database migrations all done in TypeScript.
7
7
---
8
8
9
-
Payload exposes a full suite of migration controls available for your use. Migration commands are accessible via the `npm run payload` command in your project directory.
9
+
Payload exposes a full suite of migration controls available for your use. Migration commands are accessible via
10
+
the `npm run payload` command in your project directory.
10
11
11
12
Ensure you have an npm script called "payload" in your `package.json` file.
12
13
@@ -24,33 +25,42 @@ Ensure you have an npm script called "payload" in your `package.json` file.
24
25
25
26
### Migration file contents
26
27
27
-
Payload stores all created migrations in a folder that you can specify. By default, migrations are stored in `./src/migrations`.
28
+
Payload stores all created migrations in a folder that you can specify. By default, migrations are stored
29
+
in `./src/migrations`.
28
30
29
-
A migration file has two exports - an `up` function, which is called when a migration is executed, and a `down` function that will be called if for some reason the migration fails to complete successfully. The `up` function should contain all changes that you attempt to make within the migration, and the `down` should ideally revert any changes you make.
31
+
A migration file has two exports - an `up` function, which is called when a migration is executed, and a `down` function
32
+
that will be called if for some reason the migration fails to complete successfully. The `up` function should contain
33
+
all changes that you attempt to make within the migration, and the `down` should ideally revert any changes you make.
30
34
31
-
For an added level of safety, migrations should leverage Payload [transactions](/docs/database/transactions).
35
+
For an added level of safety, migrations should leverage Payload [transactions](/docs/database/transactions). Migration
36
+
functions should make use of the `req` by adding it to the arguments of your payload local API calls such
37
+
as `payload.create` and database adapter methods like `payload.db.create`.
// Do whatever you need to revert changes if the `up` function fails
46
52
};
47
53
```
48
54
49
55
### Migrations Directory
50
56
51
-
Each DB adapter has an optional property `migrationDir` where you can override where you want your migrations to be stored/read. If this is not specified, Payload will check the default and possibly make a best effort to find your migrations directory by searching in common locations ie. `./src/migrations`, `./dist/migrations`, `./migrations`, etc.
57
+
Each DB adapter has an optional property `migrationDir` where you can override where you want your migrations to be
58
+
stored/read. If this is not specified, Payload will check the default and possibly make a best effort to find your
59
+
migrations directory by searching in common locations ie. `./src/migrations`, `./dist/migrations`, `./migrations`, etc.
52
60
53
-
All database adapters should implement similar migration patterns, but there will be small differences based on the adapter and its specific needs. Below is a list of all migration commands that should be supported by your database adapter.
61
+
All database adapters should implement similar migration patterns, but there will be small differences based on the
62
+
adapter and its specific needs. Below is a list of all migration commands that should be supported by your database
63
+
adapter.
54
64
55
65
## Commands
56
66
@@ -64,15 +74,17 @@ npm run payload migrate
64
74
65
75
### Create
66
76
67
-
Create a new migration file in the migrations directory. You can optionally name the migration that will be created. By default, migrations will be named using a timestamp.
77
+
Create a new migration file in the migrations directory. You can optionally name the migration that will be created. By
78
+
default, migrations will be named using a timestamp.
68
79
69
80
```text
70
81
npm run payload migrate:create optional-name-here
71
82
```
72
83
73
84
### Status
74
85
75
-
The `migrate:status` command will check the status of migrations and output a table of which migrations have been run, and which migrations have not yet run.
86
+
The `migrate:status` command will check the status of migrations and output a table of which migrations have been run,
0 commit comments