PatchHistory.php 3 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Framework\Setup\Patch;

use Magento\Framework\App\ResourceConnection;

/**
 * This is registry of all patches, that are already applied on database
 */
class PatchHistory
{
    /**
     * Table name where patche names will be persisted
     */
    const TABLE_NAME = 'patch_list';

    /**
     * Name of a patch
     */
    const CLASS_NAME = "patch_name";

    /**
     * Patch type for schema patches
     */
    const SCHEMA_PATCH_TYPE = 'schema';

    /**
     * Patch type for data patches
     */
    const DATA_PATCH_TYPE = 'data';

    /**
     * @var array
     */
    private $patchesRegistry = null;

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * PatchHistory constructor.
     * @param ResourceConnection $resourceConnection
     */
    public function __construct(ResourceConnection $resourceConnection)
    {
        $this->resourceConnection = $resourceConnection;
    }

    /**
     * Read and cache data patches from db
     *
     * All patches are store in patch_list table
     * @see self::TABLE_NAME
     *
     * @return array
     */
    private function getAppliedPatches()
    {
        if ($this->patchesRegistry === null) {
            $adapter = $this->resourceConnection->getConnection();
            $filterSelect = $adapter
                ->select()
                ->from($this->resourceConnection->getTableName(self::TABLE_NAME), self::CLASS_NAME);
            $this->patchesRegistry = $adapter->fetchCol($filterSelect);
        }

        return $this->patchesRegistry;
    }

    /**
     * Fix patch in patch table in order to avoid reapplying of patch
     *
     * @param string $patchName
     * @return void
     */
    public function fixPatch($patchName)
    {
        if ($this->isApplied($patchName)) {
            throw new \LogicException(sprintf("Patch %s cannot be applied twice", $patchName));
        }

        $adapter = $this->resourceConnection->getConnection();
        $adapter->insert($this->resourceConnection->getTableName(self::TABLE_NAME), [self::CLASS_NAME => $patchName]);
    }

    /**
     * Revert patch from history
     *
     * @param $patchName
     * @return void
     */
    public function revertPatchFromHistory($patchName)
    {
        if (!$this->isApplied($patchName)) {
            throw new \LogicException(
                sprintf("Patch %s should be applied, before you can revert it", $patchName)
            );
        }

        $adapter = $this->resourceConnection->getConnection();
        $adapter->delete(
            $this->resourceConnection->getTableName(self::TABLE_NAME),
            [self::CLASS_NAME . "= ?" => $patchName]
        );
    }

    /**
     * Check whether patch was applied on the system or not
     *
     * @param string $patchName
     * @return bool
     */
    public function isApplied($patchName)
    {
        return in_array($patchName, $this->getAppliedPatches());
    }
}