We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How To Create a Migration in Shopware 6?
Migrations are PHP classes that allow changes in Database Schema. Using Migration files we can update database tables, create new tables, remove columns, add new columns, change the database contents and many more database related changes. You can create your own migration file using the following command.
php bin/console database:create-migration -p --name
For Example:
php bin/console database:create-migration -p MageSparkHelloWorld --name createBlogTable
The migration file has been created in the custom/plugins/MageSparkHelloWorld/src/Migration directory, as you can see. Migration (prefix) + 1649315274 (timestamp) + createBlogTable is the file name (only a brief description of a migration's objective).
Please edit the created file in the Migration directory. Make sure to include your logic in the update method, such as this:
<?php declare(strict_types=1);
namespace MageSpark\HelloWorld\Migration;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;
class Migration1649315274createBlogTable extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1649315274;
}
public function update(Connection $connection): void
{
$query = <<<SQL
CREATE TABLE IF NOT EXISTS `magespark_blogs` (
`id` BINARY(16) NOT NULL,
`name` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
$connection->executeStatement($query);
}
public function updateDestructive(Connection $connection): void
{
}
}
To begin the migration, use the following command:
php bin/console database:migrate MageSparkHelloWorld --all
Result
Finished! When you complete this step, you will find a new table magespark_blogs created in your Database.
NOTE: if the database:migrate command throws the following error, then please clear the cache first. You can clear the cache using the following command. After you clear the cache you have to execute the database:migrate again.
Error:
Get collection for identifier: "PluginName"
! [NOTE] No collection found for identifier: "PluginName", continuing
Clear Cache:
php bin/console cache:clear
Keep The Following Information In Mind:-
While Updating Your Method:
This approach lets you create tables, change columns, and update data. Let's imagine you spend most of your time adding or changing database processes.
This method will be called (if it hasn't already been called) whenever a plugin is installed, enabled, or updated, or when the database:migrate command is used.
If you're deleting or deactivating the plugin, this technique won't work.
The update method must only include non-destructive modifications that may be turned back at any moment, according to the specification.
Update Destructive Method:
This technique should only be used to drop tables, delete data, and ensure that anything connected to your plugin that was previously placed in the database is destroyed. If you're deleting or deactivating the plugin, this technique won't work. A special command must be used to carry out such a damaging operation: database:migrate-destructive php bin/console PluginName Here —everything
According to the paperwork: The most recent update Destructive methods might include alterations that are irreversible, such as dropping columns or tables.
- This approach should only be used to eliminate tables, wipe data, and verify that anything linked to your plugin which was previously entered into the database is fully erased.
- If you are deleting or deactivating the plugin, this technique will not work. Such a damaging action must be initiated by a precise command: database:migrate-destructive php bin/console PluginName Here—All
- According to the paperwork: The revision Damaging methods can include harmful modifications that cannot be reversed, such as deleting columns or tables.
I hope this information was beneficial to you! See you next time on the blog!
Starting Your Own Shopware Store?
Get a free consultation from Shopware experts
on do's and don't