Skip to content

Commit 5ec2b23

Browse files
Merge pull request #3831 from magento-engcom/prs-21151-21458
[EngCom] Public Pull Requests - 2.3.1-release - #21151 Database Rollback not working M2.3.0 by @Stepa4man - #21458 Elasticsearch6 implementation. by @romainruaud
2 parents f6298ac + 7e45f62 commit 5ec2b23

File tree

25 files changed

+2266
-17
lines changed

25 files changed

+2266
-17
lines changed

app/code/Magento/AdvancedSearch/etc/adminhtml/system.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@
4848
</depends>
4949
</field>
5050
<!--<group id="suggestions">-->
51-
<field id="search_suggestion_enabled" translate="label comment" type="select" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
51+
<field id="search_suggestion_enabled" translate="label comment" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
5252
<label>Enable Search Suggestions</label>
5353
<comment>When you enable this option your site may slow down.</comment>
5454
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
5555
</field>
56-
<field id="search_suggestion_count" translate="label" type="text" sortOrder="71" showInDefault="1" showInWebsite="1" showInStore="1">
56+
<field id="search_suggestion_count" translate="label" type="text" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="1">
5757
<label>Search Suggestions Count</label>
5858
<depends>
5959
<field id="search_suggestion_enabled">1</field>
6060
</depends>
6161
</field>
62-
<field id="search_suggestion_count_results_enabled" translate="label" type="select" sortOrder="72" showInDefault="1" showInWebsite="1" showInStore="1">
62+
<field id="search_suggestion_count_results_enabled" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="1">
6363
<label>Show Results Count for Each Suggestion</label>
6464
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
6565
<comment>When you enable this option your site may slow down.</comment>

app/code/Magento/Backup/Model/Db.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Backup\Model;
77

88
use Magento\Backup\Helper\Data as Helper;
9+
use Magento\Backup\Model\ResourceModel\Table\GetListTables;
10+
use Magento\Backup\Model\ResourceModel\View\CreateViewsBackup;
911
use Magento\Framework\App\ObjectManager;
1012
use Magento\Framework\Exception\RuntimeException;
1113

@@ -44,18 +46,35 @@ class Db implements \Magento\Framework\Backup\Db\BackupDbInterface
4446
private $helper;
4547

4648
/**
47-
* @param \Magento\Backup\Model\ResourceModel\Db $resourceDb
49+
* @var GetListTables
50+
*/
51+
private $getListTables;
52+
53+
/**
54+
* @var CreateViewsBackup
55+
*/
56+
private $getViewsBackup;
57+
58+
/**
59+
* Db constructor.
60+
* @param ResourceModel\Db $resourceDb
4861
* @param \Magento\Framework\App\ResourceConnection $resource
4962
* @param Helper|null $helper
63+
* @param GetListTables|null $getListTables
64+
* @param CreateViewsBackup|null $getViewsBackup
5065
*/
5166
public function __construct(
52-
\Magento\Backup\Model\ResourceModel\Db $resourceDb,
67+
ResourceModel\Db $resourceDb,
5368
\Magento\Framework\App\ResourceConnection $resource,
54-
?Helper $helper = null
69+
?Helper $helper = null,
70+
?GetListTables $getListTables = null,
71+
?CreateViewsBackup $getViewsBackup = null
5572
) {
5673
$this->_resourceDb = $resourceDb;
5774
$this->_resource = $resource;
5875
$this->helper = $helper ?? ObjectManager::getInstance()->get(Helper::class);
76+
$this->getListTables = $getListTables ?? ObjectManager::getInstance()->get(GetListTables::class);
77+
$this->getViewsBackup = $getViewsBackup ?? ObjectManager::getInstance()->get(CreateViewsBackup::class);
5978
}
6079

6180
/**
@@ -161,7 +180,7 @@ public function createBackup(\Magento\Framework\Backup\Db\BackupInterface $backu
161180

162181
$this->getResource()->beginTransaction();
163182

164-
$tables = $this->getResource()->getTables();
183+
$tables = $this->getListTables->execute();
165184

166185
$backup->write($this->getResource()->getHeader());
167186

@@ -198,6 +217,8 @@ public function createBackup(\Magento\Framework\Backup\Db\BackupInterface $backu
198217
$backup->write($this->getResource()->getTableDataAfterSql($table));
199218
}
200219
}
220+
$this->getViewsBackup->execute($backup);
221+
201222
$backup->write($this->getResource()->getTableForeignKeysSql());
202223
$backup->write($this->getResource()->getTableTriggersSql());
203224
$backup->write($this->getResource()->getFooter());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backup\Model\ResourceModel\Table;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
12+
/**
13+
* Provides full list of tables in the database. This list excludes views, to allow different backup process.
14+
*/
15+
class GetListTables
16+
{
17+
private const TABLE_TYPE = 'BASE TABLE';
18+
19+
/**
20+
* @var ResourceConnection
21+
*/
22+
private $resource;
23+
24+
/**
25+
* @param ResourceConnection $resource
26+
*/
27+
public function __construct(ResourceConnection $resource)
28+
{
29+
$this->resource = $resource;
30+
}
31+
32+
/**
33+
* Get list of database tables excluding views.
34+
*
35+
* @return array
36+
*/
37+
public function execute(): array
38+
{
39+
return $this->resource->getConnection('backup')->fetchCol(
40+
"SHOW FULL TABLES WHERE `Table_type` = ?",
41+
self::TABLE_TYPE
42+
);
43+
}
44+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backup\Model\ResourceModel\View;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\Backup\Db\BackupInterface;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
14+
/**
15+
* Creates backup of Views in the database.
16+
*/
17+
class CreateViewsBackup
18+
{
19+
/**
20+
* @var GetListViews
21+
*/
22+
private $getListViews;
23+
24+
/**
25+
* @var ResourceConnection
26+
*/
27+
private $resourceConnection;
28+
29+
/**
30+
* @var AdapterInterface
31+
*/
32+
private $connection;
33+
34+
/**
35+
* @param GetListViews $getListViews
36+
* @param ResourceConnection $resourceConnection
37+
*/
38+
public function __construct(
39+
GetListViews $getListViews,
40+
ResourceConnection $resourceConnection
41+
) {
42+
$this->getListViews = $getListViews;
43+
$this->resourceConnection = $resourceConnection;
44+
}
45+
46+
/**
47+
* Write backup data to backup file.
48+
*
49+
* @param BackupInterface $backup
50+
*/
51+
public function execute(BackupInterface $backup): void
52+
{
53+
$views = $this->getListViews->execute();
54+
55+
foreach ($views as $view) {
56+
$backup->write($this->getViewHeader($view));
57+
$backup->write($this->getDropViewSql($view));
58+
$backup->write($this->getCreateView($view));
59+
}
60+
}
61+
62+
/**
63+
* Retrieve Database connection for Backup.
64+
*
65+
* @return AdapterInterface
66+
*/
67+
private function getConnection(): AdapterInterface
68+
{
69+
if (!$this->connection) {
70+
$this->connection = $this->resourceConnection->getConnection('backup');
71+
}
72+
73+
return $this->connection;
74+
}
75+
76+
/**
77+
* Get CREATE VIEW query for the specific view.
78+
*
79+
* @param string $viewName
80+
* @return string
81+
*/
82+
private function getCreateView(string $viewName): string
83+
{
84+
$quotedViewName = $this->getConnection()->quoteIdentifier($viewName);
85+
$query = 'SHOW CREATE VIEW ' . $quotedViewName;
86+
$row = $this->getConnection()->fetchRow($query);
87+
$regExp = '/\sDEFINER\=\`([^`]*)\`\@\`([^`]*)\`/';
88+
$sql = preg_replace($regExp, '', $row['Create View']);
89+
90+
return $sql . ';' . "\n";
91+
}
92+
93+
/**
94+
* Prepare a header for View being dumped.
95+
*
96+
* @param string $viewName
97+
* @return string
98+
*/
99+
public function getViewHeader(string $viewName): string
100+
{
101+
$quotedViewName = $this->getConnection()->quoteIdentifier($viewName);
102+
return "\n--\n" . "-- Structure for view {$quotedViewName}\n" . "--\n\n";
103+
}
104+
105+
/**
106+
* Make sure that View being created is deleted if already exists.
107+
*
108+
* @param string $viewName
109+
* @return string
110+
*/
111+
public function getDropViewSql(string $viewName): string
112+
{
113+
$quotedViewName = $this->getConnection()->quoteIdentifier($viewName);
114+
return sprintf('DROP VIEW IF EXISTS %s;\n', $quotedViewName);
115+
}
116+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backup\Model\ResourceModel\View;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
12+
/**
13+
* Get list of database views.
14+
*/
15+
class GetListViews
16+
{
17+
private const TABLE_TYPE = 'VIEW';
18+
19+
/**
20+
* @var ResourceConnection
21+
*/
22+
private $resource;
23+
24+
/**
25+
* @param ResourceConnection $resource
26+
*/
27+
public function __construct(ResourceConnection $resource)
28+
{
29+
$this->resource = $resource;
30+
}
31+
32+
/**
33+
* Get list of database views.
34+
*
35+
* @return array
36+
*/
37+
public function execute(): array
38+
{
39+
return $this->resource->getConnection('backup')->fetchCol(
40+
"SHOW FULL TABLES WHERE `Table_type` = ?",
41+
self::TABLE_TYPE
42+
);
43+
}
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Elasticsearch6\Block\Adminhtml\System\Config;
7+
8+
/**
9+
* Elasticsearch 6.x test connection block
10+
*/
11+
class TestConnection extends \Magento\AdvancedSearch\Block\Adminhtml\System\Config\TestConnection
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
protected function _getFieldMapping()
17+
{
18+
$fields = [
19+
'engine' => 'catalog_search_engine',
20+
'hostname' => 'catalog_search_elasticsearch6_server_hostname',
21+
'port' => 'catalog_search_elasticsearch6_server_port',
22+
'index' => 'catalog_search_elasticsearch6_index_prefix',
23+
'enableAuth' => 'catalog_search_elasticsearch6_enable_auth',
24+
'username' => 'catalog_search_elasticsearch6_username',
25+
'password' => 'catalog_search_elasticsearch6_password',
26+
'timeout' => 'catalog_search_elasticsearch6_server_timeout',
27+
];
28+
29+
return array_merge(parent::_getFieldMapping(), $fields);
30+
}
31+
}

0 commit comments

Comments
 (0)