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
+51-15Lines changed: 51 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -105,9 +105,9 @@ So, always rollback the migration before deleting it.
105
105
106
106
## Data Migration Scripts
107
107
108
-
Some features might require data migrations to ensure new functionality works as expected after an upgrade. Typically, these migrations can be dealt with by database migrations within a module. However, this is not the case when the changes affect the data models in several modules or if the data is stored in a third-party system. In those scenarios, the data needs to be brought to the desired state.
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.
109
109
110
-
To do so, you can use data migration scripts. They are very similar to regular scripts, in the sense that you have access to the dependency container, i.e. all modules. However, instead of running manually, data migration scripts are run automatically as part of running `medusa db:migrate`.
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`.
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.
134
131
135
-
exportdefaultasyncfunction migrationScript({
136
-
container,
137
-
}:ExecArgs) {
138
-
// Check if the required modules are present
139
-
if (!MedusaModule.isInstalled(Modules.PRODUCT))
132
+
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:
133
+
134
+
exportconst dataMigrationHighlights = [
135
+
["6", "migrateBlogData", "Function to execute when migrations are run"],
136
+
["8", "isInstalled", "Confirm that the Blog Module is installed before running the workflow."],
137
+
["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.
167
+
168
+
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.
169
+
170
+
#### Test Data Migration Script
171
+
172
+
To test out the data migration script, run the migration command:
173
+
174
+
```bash
175
+
npx medusa db:migrate
146
176
```
147
177
178
+
Medusa will run any pending migrations and migration scripts, including your script.
179
+
180
+
If the script runs successfully, Medusa won't run the script again.
181
+
182
+
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