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
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,3 +106,67 @@ So, always rollback the migration before deleting it.
106
106
## More Database Commands
107
107
108
108
To learn more about the Medusa CLI's database commands, refer to [this CLI reference](!resources!/medusa-cli/commands/db).
109
+
110
+
---
111
+
112
+
## Data Migration Scripts
113
+
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.
115
+
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`.
117
+
118
+
### How to Create a Data Migration Script
119
+
120
+
You can create data migration scripts in a TypeScript or JavaScript file under the `src/migration-scripts` directory. The file must export an asynchronous function that will be executed when the `db:migrate` command is executed.
121
+
122
+
For example, to create a data migration script for the Blog Module example, create the file `src/migration-scripts/migrate-blog-data.ts` with the following content:
123
+
124
+
exportconst dataMigrationHighlights = [
125
+
["6", "migrateBlogData", "Function to execute when migrations are run"],
126
+
["8", "isInstalled", "Confirm that the Blog Module is installed before running the workflow."],
127
+
["12", "migrateBlogDataWorkflow", "Perform the data migration action."],
In the above example, you default export an asynchronous function that receives an object parameter with the [Medusa Container](../../medusa-container/page.mdx) property.
157
+
158
+
In the function, you first ensure that the Blog Module is installed to avoid errors otherwise. Then, you run a workflow that you've created in the same file that performs the necessary data migration.
159
+
160
+
#### Test Data Migration Script
161
+
162
+
To test out the data migration script, run the migration command:
163
+
164
+
```bash
165
+
npx medusa db:migrate
166
+
```
167
+
168
+
Medusa will run any pending migrations and migration scripts, including your script.
169
+
170
+
If the script runs successfully, Medusa won't run the script again.
171
+
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.
0 commit comments