# Custom plugin: adding and applying migrations

**URL:** <https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303>\
**Category:** Development\
**Created:** [September 5, 2022, 3:28pm UTC](https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303 "2022-09-05T15:28:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![0xsheff](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/0xsheff/32/8183_2.png) [@0xsheff](https://forum.mautic.org/u/0xsheff)\
**Post date:** [September 5, 2022, 3:28pm UTC](https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303/1 "2022-09-05T15:28:26Z")

</div>

Hi, I’m trying to develop some custom plugin, and trying to add some columns to the database. I need to create additional column in the existing ‘emails’ table. My code with namespaces below

It seems what I need to change some settings or do something else, because Mautic console doesn’t see my migration at all.

So question is: how to add and apply the migration in the plugin properly? I need to do this without any changes in the core Mautic code.

```auto
<?php

declare(strict_types=1);

namespace MauticPlugin\MyPluginBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;

class Version_0_0_1 extends AbstractMigration
{
    private $table = 'emails';

    protected function isApplicable(Schema $schema): bool
    {
        try {
            return !$schema->getTable($this->concatPrefix($this->table))->hasColumn('replace_api_key');
        } catch (SchemaException $e) {
            return false;
        }
    }

    protected function up(): void
    {
        $this->addSql("
            ALTER TABLE `{$this->concatPrefix($this->table)}`
            ADD `replace_api_key` varchar(255) NULL
        ");
    }
}

```

---

<div class="post-metadata">

**Author:** ![mzagmajster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mzagmajster/32/687_2.png) [@mzagmajster](https://forum.mautic.org/u/mzagmajster)\
**Post date:** [September 5, 2022, 3:54pm UTC](https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303/2 "2022-09-05T15:54:12Z")

</div>

Make sure that plugin version in config/Config.php matches the version in migration file.

Also make sure that plugin with that version is not already in database when you run reload or install (update or delete the entry if itis)

---

<div class="post-metadata">

**Author:** ![0xsheff](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/0xsheff/32/8183_2.png) [@0xsheff](https://forum.mautic.org/u/0xsheff)\
**Post date:** [September 6, 2022, 11:06am UTC](https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303/3 "2022-09-06T11:06:13Z")

</div>

Thank you for reply, I tried to remove it from ‘plugins’ table, then cleared the cache and install it again. Version of plugin was ‘0.0.1’, i changed to ‘0.0.2’ and changed the name of the migration to ‘Version\_0\_0\_2’. Then i’ve installed it, it is appeared in the ‘plugins’ table, but migration wasn’t applied and table ‘emails’ wasn’t changed.

---

<div class="post-metadata">

**Author:** ![mzagmajster](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/mzagmajster/32/687_2.png) [@mzagmajster](https://forum.mautic.org/u/mzagmajster)\
**Post date:** [September 6, 2022, 11:31am UTC](https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303/4 "2022-09-06T11:31:23Z")

</div>

Hmm strange, it works in my bundle and I extend the same base class. For the record file is named ‘Version\_0\_0\_2.php’ right and its located in Migrations directory of your bundle right?

As you can see here, mautic core is using dedicated base class to run migrations: [mautic/Version20180921144421.php at 8a57278758e2c3e1c1ca987aaf9ebd5f05b3c877 · mautic/mautic · GitHub](https://github.com/mautic/mautic/blob/8a57278758e2c3e1c1ca987aaf9ebd5f05b3c877/app/migrations/Version20180921144421.php)

You might give it a go, since you are still struggling.

---

<div class="post-metadata">

**Author:** ![0xsheff](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/0xsheff/32/8183_2.png) [@0xsheff](https://forum.mautic.org/u/0xsheff)\
**Post date:** [September 7, 2022, 12:55pm UTC](https://forum.mautic.org/t/custom-plugin-adding-and-applying-migrations/25303/5 "2022-09-07T12:55:48Z")

</div>

Well, that was my fault, i’m not experienced in Symfony, and didn’t created any Entity class, but it seems required to apply migrations and database changes. I didn’t change the base Email entity class and created new table and simple Entity with two fields (email\_id and replace\_api\_key) instead, changed the migration accordingly, after that all db changed was applied.
