# Migrations

Database migrations can be brought by plugins and AdditionalBundles.

If you want to know more about migrations you can follow the steps from the Shopware documentation:

{% embed url="<https://developer.shopware.com/docs/concepts/framework/migrations.html#migrations>" %}

All migrations are located in the *Migration/*-directory of your plugin or AdditionalBundle per default.

## Adding a migration to an AdditionalBundle

Shopware provides the `database:create-migration` console command to easily create a new migration.

To create a new migration in an AdditionalBundle you must call the command with some options:

```bash
bin/console database:create-migration --name=<name> path/to/your/bundle/migration/dir Your\\Bundle\\Migration\\Namespace
```

The following example command adds a migration *MyMigration* to the *Migration/*-directory of the *FooBundle:*

```bash
bin/console database:create-migration --name=MyMigration custom/plugins/FooPlugin/FooBundle/Migration Foo\\Bundle\\Migration
```

## Delete data on plugin uninstallation

If you have some migrations which add new database tables you also want to delete these tables on plugin uninstallation. dustin/shopware-utils automatically takes care about that! The only thing you need to do is to tell which tables need to be deleted on plugin uninstallation.

Overwrite the `getTablesToRemove` -method of your plugin or AdditionalBundle.

{% tabs %}
{% tab title="Plugin" %}

```php
<?php

namespace Foo\Bar;

use Dustin\ShopwareUtils\Core\Framework\Plugin;

class FooBar extends Plugin
{
    public function getTablesToRemove(): array {
        return [
            'my_first_table',
            'my_second_table'
        ];
    }
}
```

{% endtab %}

{% tab title="AdditionalBundle" %}

```php
<?php

namespace Foo\Bundle;

use Dustin\ShopwareUtils\Core\Framework\AdditionalBundle;

class FooBundle extends AdditionalBundle
{
    public function getTablesToRemove(): array {
        return [
            'my_first_table',
            'my_second_table'
        ];
    }
}
```

{% endtab %}
{% endtabs %}

dustin/shopware-utils will  execute the following steps to remove your tables from database:

1. Execute a DELETE-statement for each table in the given order. All statements will be executed in a single transaction. If there is an error and the transaction fails a rollback will be executed and the exception will be thrown. If the transaction is successful all tables should be present but with no data.
2. Execute a DROP TABLE-statement for each table in the given order.

{% hint style="warning" %}
DELETE-statements will be executed in the given order. If you want two tables to be deleted which have foreign key constraints you have to list the associated entity first.<br>

Example:

You have two entities:

* my\_entity
* my\_entity\_association (with a foreign key and ON DELETE RESTRICT constraint )

You must provide the entities in the following order to prevent an exception:

1. my\_entity\_association
2. my\_entity
   {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dustinsimon.gitbook.io/shopware-utils/migrations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
