123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?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());
- }
- }
|