Skip to content

Commit 3d1444f

Browse files
rewording
Co-authored-by: Shahed Nasser <[email protected]>
1 parent 295465e commit 3d1444f

File tree

1 file changed

+51
-15
lines changed
  • www/apps/book/app/learn/fundamentals/data-models/write-migration

1 file changed

+51
-15
lines changed

www/apps/book/app/learn/fundamentals/data-models/write-migration/page.mdx

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ So, always rollback the migration before deleting it.
105105

106106
## Data Migration Scripts
107107

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

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`.
111111

112112
### How they work
113113

@@ -125,26 +125,62 @@ The flow is as follows:
125125
- Mark script as finished
126126
- Release lock
127127

128-
### Example
128+
### How to Create a Data Migration Script
129129

130-
```ts title="src/migration-scripts/example.tsx"
131-
import { MedusaModule } from "@medusajs/framework/modules-sdk"
132-
import { ExecArgs } from "@medusajs/framework/types"
133-
import { Modules } from "@medusajs/framework/utils"
130+
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.
134131

135-
export default async function 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+
export const 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."],
138+
]
139+
140+
```ts title="src/migration-scripts/migrate-blog-data.ts" highlights={dataMigrationHighlights}
141+
import { MedusaModule } from "@medusajs/framework/modules-sdk";
142+
import { ExecArgs } from "@medusajs/framework/types";
143+
import { BLOG_MODULE } from "../modules/blog";
144+
import { createWorkflow } from "@medusajs/framework/workflows-sdk";
145+
146+
export default async function migrateBlogData({ container }: ExecArgs) {
147+
// Check that the blog module exists
148+
if (!MedusaModule.isInstalled(BLOG_MODULE)) {
140149
return
141-
142-
// Migrate data
143-
await migrateDataWorkflow(container).run(...)
150+
}
144151

152+
await migrateBlogDataWorkflow(container).run({})
145153
}
154+
155+
const migrateBlogDataWorkflow = createWorkflow(
156+
"migrate-blog-data",
157+
() => {
158+
// Assuming you have these steps
159+
createDefaultSiteStep()
160+
161+
assignBlogDataToSiteStep()
162+
}
163+
)
164+
```
165+
166+
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
146176
```
147177

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.
183+
148184
---
149185

150186
## More Database Commands

0 commit comments

Comments
 (0)