Skip to content

Commit 7da45e0

Browse files
nc1zpwizla
andauthored
Document using strapi instance for migrations (#2085)
* docs: using strapi instance for migrations * docs: update database-migrations.md might result in data loss so the highest-level of callout is required Co-authored-by: Pierre Wizla <[email protected]> * docs: title clean up for database-migrations.md Co-authored-by: Pierre Wizla <[email protected]> --------- Co-authored-by: Pierre Wizla <[email protected]>
1 parent 05167cb commit 7da45e0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docusaurus/docs/dev-docs/database-migrations.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,35 @@ module.exports = {
7474
```
7575

7676
</details>
77+
78+
### Using Strapi Instance for migrations
79+
80+
:::danger
81+
If a user opts not to use Knex directly for migrations and instead utilizes the Strapi instance, it is important to wrap the migration code with `strapi.db.transaction()`. Failure to do so may result in migrations not rolling back if an error occurs.
82+
:::
83+
84+
<details>
85+
<summary>Example of migration file with Strapi instance</summary>
86+
87+
```jsx title="./database/migrations/2022.05.10T00.00.00.name-of-my-migration.js"
88+
module.exports = {
89+
async up() {
90+
await strapi.db.transaction(async () => {
91+
// Your migration code here
92+
93+
// Example: creating new entries
94+
await strapi.entityService.create('api::article.article', {
95+
data: {
96+
title: 'My Article',
97+
},
98+
});
99+
100+
// Example: custom service method
101+
await strapi.service('api::article.article').updateRelatedArticles();
102+
});
103+
},
104+
};
105+
```
106+
107+
</details>
108+

0 commit comments

Comments
 (0)