dustin/shopware-utils
  • dustin/shopware-utils
  • Installation
    • Installation
  • Bundles
    • Plugins and AdditionalBundles
      • Create plugin
      • Add additional bundles
    • Themes
  • Migrations
  • Features
    • Configuration
  • Adding resources
    • Custom fields
    • Document types
    • Payment methods
    • Mail template types
    • Mail templates
Powered by GitBook
On this page
  • Adding a migration to an AdditionalBundle
  • Delete data on plugin uninstallation

Migrations

PreviousThemesNextConfiguration

Last updated 1 month ago

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

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

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.

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.

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

Migrations | Shopware Documentation
Logo