From 07e3cf5dca37824d5b7a0ef594a2a6cea5bc67b1 Mon Sep 17 00:00:00 2001 From: Stepan Furman Date: Wed, 27 Feb 2019 22:45:35 +0200 Subject: [PATCH 1/2] MSI-1988 issue fixed: Database Rollback not working M2.3 --- app/code/Magento/Backup/Model/Db.php | 24 +++- .../ResourceModel/Table/GetListTables.php | 42 +++++++ .../Model/ResourceModel/View/GetListViews.php | 41 +++++++ .../ResourceModel/View/GetViewsBackup.php | 106 ++++++++++++++++++ 4 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php create mode 100644 app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php create mode 100644 app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php diff --git a/app/code/Magento/Backup/Model/Db.php b/app/code/Magento/Backup/Model/Db.php index bc458a0a8e4bf..a385a476845db 100644 --- a/app/code/Magento/Backup/Model/Db.php +++ b/app/code/Magento/Backup/Model/Db.php @@ -6,6 +6,8 @@ namespace Magento\Backup\Model; use Magento\Backup\Helper\Data as Helper; +use Magento\Backup\Model\ResourceModel\Table\GetListTables; +use Magento\Backup\Model\ResourceModel\View\GetViewsBackup; use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\RuntimeException; @@ -43,19 +45,35 @@ class Db implements \Magento\Framework\Backup\Db\BackupDbInterface */ private $helper; + /** + * @var GetListTables + */ + private $getListTables; + + /** + * @var GetViewsBackup + */ + private $getViewsBackup; + /** * @param \Magento\Backup\Model\ResourceModel\Db $resourceDb * @param \Magento\Framework\App\ResourceConnection $resource + * @param GetListTables|null $getListTables + * @param GetViewsBackup|null $getViewsBackup * @param Helper|null $helper */ public function __construct( \Magento\Backup\Model\ResourceModel\Db $resourceDb, \Magento\Framework\App\ResourceConnection $resource, - ?Helper $helper = null + ?Helper $helper = null, + GetListTables $getListTables = null, + GetViewsBackup $getViewsBackup = null ) { $this->_resourceDb = $resourceDb; $this->_resource = $resource; $this->helper = $helper ?? ObjectManager::getInstance()->get(Helper::class); + $this->getListTables = $getListTables ?: ObjectManager::getInstance()->get(GetListTables::class); + $this->getViewsBackup = $getViewsBackup ?: ObjectManager::getInstance()->get(GetViewsBackup::class); } /** @@ -161,7 +179,7 @@ public function createBackup(\Magento\Framework\Backup\Db\BackupInterface $backu $this->getResource()->beginTransaction(); - $tables = $this->getResource()->getTables(); + $tables = $this->getListTables->execute(); $backup->write($this->getResource()->getHeader()); @@ -198,6 +216,8 @@ public function createBackup(\Magento\Framework\Backup\Db\BackupInterface $backu $backup->write($this->getResource()->getTableDataAfterSql($table)); } } + $this->getViewsBackup->execute($backup); + $backup->write($this->getResource()->getTableForeignKeysSql()); $backup->write($this->getResource()->getTableTriggersSql()); $backup->write($this->getResource()->getFooter()); diff --git a/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php b/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php new file mode 100644 index 0000000000000..5a9f96a50c35a --- /dev/null +++ b/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php @@ -0,0 +1,42 @@ +resource = $resource; + } + + /** + * @return array + */ + public function execute() + { + return $this->resource->getConnection('backup')->fetchCol( +"SHOW FULL TABLES WHERE `Table_type` = ?", + self::TABLE_TYPE + ); + } +} diff --git a/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php b/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php new file mode 100644 index 0000000000000..54f2a1a65d280 --- /dev/null +++ b/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php @@ -0,0 +1,41 @@ +resource = $resource; + } + + /** + * @return array + */ + public function execute() + { + return $this->resource->getConnection('backup')->fetchCol( + "SHOW FULL TABLES WHERE `Table_type` = ?", + self::TABLE_TYPE + ); + } +} diff --git a/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php b/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php new file mode 100644 index 0000000000000..bda5ff58c0594 --- /dev/null +++ b/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php @@ -0,0 +1,106 @@ +getListViews = $getListViews; + $this->resourceConnection = $resourceConnection; + } + + /** + * @param BackupInterface $backup + */ + public function execute(BackupInterface $backup) + { + $views = $this->getListViews->execute(); + + foreach ($views as $view) { + $backup->write($this->getViewHeader($view)); + $backup->write($this->getDropViewSql($view)); + $backup->write($this->getShowCreateView($view)); + } + } + + /** + * @return AdapterInterface + */ + private function getConnection() + { + if (!$this->connection) { + $this->connection = $this->resourceConnection->getConnection('backup'); + } + + return $this->connection; + } + + /** + * @param string $viewName + * @return string + */ + private function getShowCreateView($viewName) + { + $quotedViewName = $this->getConnection()->quoteIdentifier($viewName); + $query = 'SHOW CREATE VIEW ' . $quotedViewName; + $row = $this->getConnection()->fetchRow($query); + $regExp = '/\sDEFINER\=\`([^`]*)\`\@\`([^`]*)\`/'; + $sql = preg_replace($regExp, '', $row['Create View']); + + return $sql . ';' . "\n"; + } + + /** + * @param string $viewName + * @return string + */ + public function getViewHeader($viewName) + { + $quotedViewName = $this->getConnection()->quoteIdentifier($viewName); + return "\n--\n" . "-- Structure for view {$quotedViewName}\n" . "--\n\n"; + } + + /** + * @param string $viewName + * @return string + */ + public function getDropViewSql($viewName) + { + $quotedViewName = $this->getConnection()->quoteIdentifier($viewName); + return sprintf('DROP VIEW IF EXISTS %s;' . "\n", $quotedViewName); + } +} From b11652bb7c6377f3ba38407f6df25d8f35a47d28 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Thu, 28 Feb 2019 17:17:47 +0000 Subject: [PATCH 2/2] magento/magento2#21151: Fixed static tests --- app/code/Magento/Backup/Model/Db.php | 5 +++-- .../Backup/Model/ResourceModel/Table/GetListTables.php | 4 +++- .../Backup/Model/ResourceModel/View/GetListViews.php | 2 ++ .../Backup/Model/ResourceModel/View/GetViewsBackup.php | 10 ++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Backup/Model/Db.php b/app/code/Magento/Backup/Model/Db.php index a385a476845db..971223cdecc4a 100644 --- a/app/code/Magento/Backup/Model/Db.php +++ b/app/code/Magento/Backup/Model/Db.php @@ -56,11 +56,12 @@ class Db implements \Magento\Framework\Backup\Db\BackupDbInterface private $getViewsBackup; /** - * @param \Magento\Backup\Model\ResourceModel\Db $resourceDb + * Db constructor. + * @param ResourceModel\Db $resourceDb * @param \Magento\Framework\App\ResourceConnection $resource + * @param Helper|null $helper * @param GetListTables|null $getListTables * @param GetViewsBackup|null $getViewsBackup - * @param Helper|null $helper */ public function __construct( \Magento\Backup\Model\ResourceModel\Db $resourceDb, diff --git a/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php b/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php index 5a9f96a50c35a..013fc729069f0 100644 --- a/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php +++ b/app/code/Magento/Backup/Model/ResourceModel/Table/GetListTables.php @@ -30,12 +30,14 @@ public function __construct(ResourceConnection $resource) } /** + * Get base tables + * * @return array */ public function execute() { return $this->resource->getConnection('backup')->fetchCol( -"SHOW FULL TABLES WHERE `Table_type` = ?", + "SHOW FULL TABLES WHERE `Table_type` = ?", self::TABLE_TYPE ); } diff --git a/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php b/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php index 54f2a1a65d280..1c408dee8cac3 100644 --- a/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php +++ b/app/code/Magento/Backup/Model/ResourceModel/View/GetListViews.php @@ -29,6 +29,8 @@ public function __construct(ResourceConnection $resource) } /** + * Get view tables + * * @return array */ public function execute() diff --git a/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php b/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php index bda5ff58c0594..5a5f6a4d066e9 100644 --- a/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php +++ b/app/code/Magento/Backup/Model/ResourceModel/View/GetViewsBackup.php @@ -44,6 +44,8 @@ public function __construct( } /** + * Backup + * * @param BackupInterface $backup */ public function execute(BackupInterface $backup) @@ -58,6 +60,8 @@ public function execute(BackupInterface $backup) } /** + * Get connection + * * @return AdapterInterface */ private function getConnection() @@ -70,6 +74,8 @@ private function getConnection() } /** + * Get show create view + * * @param string $viewName * @return string */ @@ -85,6 +91,8 @@ private function getShowCreateView($viewName) } /** + * Get view header + * * @param string $viewName * @return string */ @@ -95,6 +103,8 @@ public function getViewHeader($viewName) } /** + * Get drop view SQL + * * @param string $viewName * @return string */