petrknap/doctrine-unalterablemigrations

Unalterable migrations for Doctrine Migrations

v1.1.0 2020-08-13 09:01 UTC

This package is auto-updated.

Last update: 2024-12-13 18:37:38 UTC


README

This package provides simple way how to migrate unalterable objects like views and triggers.

Example

Create view

<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\Migrations\AbstractMigration;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationInterface;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationTrait;

class Version1 extends AbstractMigration implements UnalterableMigrationInterface
{
    use UnalterableMigrationTrait;

    public function getUpSql(): string
    {
        return 'CREATE VIEW view_b AS (
            SELECT
                a.id
            FROM table_a a
        )';
    }

    public function getDownSql(): ?string
    {
        return 'DROP VIEW view_b';
    }
}

Alter view

<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\Migrations\AbstractMigration;
use PetrKnap\Doctrine\UnalterableMigrations\Patches;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationInterface;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationTrait;

class Version2 extends AbstractMigration implements UnalterableMigrationInterface
{
    use UnalterableMigrationTrait;

    public function getParentClassName(): ?string
    {
        return Version1::class;
    }

    public function getUpSql(): string
    {
        return Patches::on($this->getParent()->getUpSql())
            ->removeLine(2, 'a.id')
            ->insertLine(2, 'a.id,')
            ->insertLine(3, 'a.name')
            ->apply();
    }
}

Rename view

<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\Migrations\AbstractMigration;
use PetrKnap\Doctrine\UnalterableMigrations\Patches;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationInterface;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationTrait;

class Version3 extends AbstractMigration implements UnalterableMigrationInterface
{
    use UnalterableMigrationTrait;

    public function getParentClassName(): ?string
    {
        return Version2::class;
    }

    public function getUpSql(): string
    {
        return Patches::on($this->getParent()->getUpSql())
            ->removeLine(1, 'CREATE VIEW view_b AS (')
            ->insertLine(1, 'CREATE VIEW view_c AS (')
            ->apply();
    }

    public function getDownSql(): ?string
    {
        return 'DROP VIEW view_c';
    }
}

Drop view

<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\Migrations\AbstractMigration;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationInterface;
use PetrKnap\Doctrine\UnalterableMigrations\UnalterableMigrationTrait;

class Version4 extends AbstractMigration implements UnalterableMigrationInterface
{
    use UnalterableMigrationTrait;

    public function getParentClassName(): ?string
    {
        return Version3::class;
    }

    public function getUpSql(): string
    {
        return self::DROP_PARENT;
    }
}

How to install

Run composer require petrknap/doctrine-unalterablemigrations or merge this JSON code with your project composer.json file manually and run composer install. Instead of dev-master you can use one of released versions.

{
    "require": {
        "petrknap/doctrine-unalterablemigrations": "dev-master"
    }
}

Or manually clone this repository via git clone https://github.com/petrknap/doctrine-unalterablemigrations.git or download this repository as ZIP and extract files into your project.