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: www/apps/book/app/learn/fundamentals/data-models/write-migration/page.mdx
+6-22Lines changed: 6 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -103,27 +103,17 @@ So, always rollback the migration before deleting it.
103
103
104
104
---
105
105
106
-
## Data Migration Scripts
107
-
108
-
In some use cases, you may need to perform data migration after updates to the database. For example, after you added a `Site` data model to the Blog Module, you want to assign all existing posts and authors to a default site. Another example is updating data stored in a third-party system.
106
+
## More Database Commands
109
107
110
-
In those scenarios, you can instead create a data migration script. They are asynchronous function that the Medusa application executes once when you run the `npx medusa db:migrate command`.
108
+
To learn more about the Medusa CLI's database commands, refer to [this CLI reference](!resources!/medusa-cli/commands/db).
111
109
112
-
### How they work
110
+
---
113
111
114
-
The Medusa project, its plugins, and the core are scoured for files in the `src/migration-scripts` folder, which are then executed. The files should export a default function. These scripts are only run once, provided they succeed.
112
+
## Data Migration Scripts
115
113
116
-
The flow is as follows:
114
+
In some use cases, you may need to perform data migration after updates to the database. For example, after you added a `Site` data model to the Blog Module, you want to assign all existing posts and authors to a default site. Another example is updating data stored in a third-party system.
117
115
118
-
- Find files
119
-
- Ensure the `script_migrations` table exists. If not, create it
120
-
- Identify pending migration scripts (scripts that have not been ran previously)
121
-
- Acquire a lock on the table
122
-
- Begin loop, for each script
123
-
- Insert migration script in the database
124
-
- Execute script
125
-
- Mark script as finished
126
-
- Release lock
116
+
In those scenarios, you can instead create a data migration script. They are asynchronous function that the Medusa application executes once when you run the `npx medusa db:migrate command`.
127
117
128
118
### How to Create a Data Migration Script
129
119
@@ -180,9 +170,3 @@ Medusa will run any pending migrations and migration scripts, including your scr
180
170
If the script runs successfully, Medusa won't run the script again.
181
171
182
172
If there are errors in the script, you'll receive an error in the migration script logs. Medusa will keep running the script every time you run the migration command until it runs successfully.
183
-
184
-
---
185
-
186
-
## More Database Commands
187
-
188
-
To learn more about the Medusa CLI's database commands, refer to [this CLI reference](!resources!/medusa-cli/commands/db).
0 commit comments