Skip to content

Commit

Permalink
Document using strapi instance for migrations (#2085)
Browse files Browse the repository at this point in the history
* 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 <pwizla@users.noreply.github.com>

* docs: title clean up for database-migrations.md

Co-authored-by: Pierre Wizla <pwizla@users.noreply.github.com>

---------

Co-authored-by: Pierre Wizla <pwizla@users.noreply.github.com>
  • Loading branch information
nc1z and pwizla committed Apr 23, 2024
1 parent 05167cb commit 7da45e0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docusaurus/docs/dev-docs/database-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,35 @@ module.exports = {
```

</details>

### Using Strapi Instance for migrations

:::danger
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.
:::

<details>
<summary>Example of migration file with Strapi instance</summary>

```jsx title="./database/migrations/2022.05.10T00.00.00.name-of-my-migration.js"
module.exports = {
async up() {
await strapi.db.transaction(async () => {
// Your migration code here

// Example: creating new entries
await strapi.entityService.create('api::article.article', {
data: {
title: 'My Article',
},
});

// Example: custom service method
await strapi.service('api::article.article').updateRelatedArticles();
});
},
};
```

</details>

0 comments on commit 7da45e0

Please sign in to comment.