PatchHistory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Patch;
  7. use Magento\Framework\App\ResourceConnection;
  8. /**
  9. * This is registry of all patches, that are already applied on database
  10. */
  11. class PatchHistory
  12. {
  13. /**
  14. * Table name where patche names will be persisted
  15. */
  16. const TABLE_NAME = 'patch_list';
  17. /**
  18. * Name of a patch
  19. */
  20. const CLASS_NAME = "patch_name";
  21. /**
  22. * Patch type for schema patches
  23. */
  24. const SCHEMA_PATCH_TYPE = 'schema';
  25. /**
  26. * Patch type for data patches
  27. */
  28. const DATA_PATCH_TYPE = 'data';
  29. /**
  30. * @var array
  31. */
  32. private $patchesRegistry = null;
  33. /**
  34. * @var ResourceConnection
  35. */
  36. private $resourceConnection;
  37. /**
  38. * PatchHistory constructor.
  39. * @param ResourceConnection $resourceConnection
  40. */
  41. public function __construct(ResourceConnection $resourceConnection)
  42. {
  43. $this->resourceConnection = $resourceConnection;
  44. }
  45. /**
  46. * Read and cache data patches from db
  47. *
  48. * All patches are store in patch_list table
  49. * @see self::TABLE_NAME
  50. *
  51. * @return array
  52. */
  53. private function getAppliedPatches()
  54. {
  55. if ($this->patchesRegistry === null) {
  56. $adapter = $this->resourceConnection->getConnection();
  57. $filterSelect = $adapter
  58. ->select()
  59. ->from($this->resourceConnection->getTableName(self::TABLE_NAME), self::CLASS_NAME);
  60. $this->patchesRegistry = $adapter->fetchCol($filterSelect);
  61. }
  62. return $this->patchesRegistry;
  63. }
  64. /**
  65. * Fix patch in patch table in order to avoid reapplying of patch
  66. *
  67. * @param string $patchName
  68. * @return void
  69. */
  70. public function fixPatch($patchName)
  71. {
  72. if ($this->isApplied($patchName)) {
  73. throw new \LogicException(sprintf("Patch %s cannot be applied twice", $patchName));
  74. }
  75. $adapter = $this->resourceConnection->getConnection();
  76. $adapter->insert($this->resourceConnection->getTableName(self::TABLE_NAME), [self::CLASS_NAME => $patchName]);
  77. }
  78. /**
  79. * Revert patch from history
  80. *
  81. * @param $patchName
  82. * @return void
  83. */
  84. public function revertPatchFromHistory($patchName)
  85. {
  86. if (!$this->isApplied($patchName)) {
  87. throw new \LogicException(
  88. sprintf("Patch %s should be applied, before you can revert it", $patchName)
  89. );
  90. }
  91. $adapter = $this->resourceConnection->getConnection();
  92. $adapter->delete(
  93. $this->resourceConnection->getTableName(self::TABLE_NAME),
  94. [self::CLASS_NAME . "= ?" => $patchName]
  95. );
  96. }
  97. /**
  98. * Check whether patch was applied on the system or not
  99. *
  100. * @param string $patchName
  101. * @return bool
  102. */
  103. public function isApplied($patchName)
  104. {
  105. return in_array($patchName, $this->getAppliedPatches());
  106. }
  107. }