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:

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:

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:

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.

<?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'
        ];
    }
}

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.

Last updated