Skip to content

Commit b053d18

Browse files
committed
reordering
1 parent 3d1444f commit b053d18

File tree

1 file changed

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

1 file changed

+6
-22
lines changed

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,17 @@ So, always rollback the migration before deleting it.
103103

104104
---
105105

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
109107

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).
111109

112-
### How they work
110+
---
113111

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
115113

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

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

128118
### How to Create a Data Migration Script
129119

@@ -180,9 +170,3 @@ Medusa will run any pending migrations and migration scripts, including your scr
180170
If the script runs successfully, Medusa won't run the script again.
181171

182172
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

Comments
 (0)