-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Changelog.php
245 lines (222 loc) · 6.58 KB
/
Changelog.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Mview\View;
use Magento\Framework\DB\Adapter\ConnectionException;
use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\Phrase;
/**
* Class Changelog for manipulations with the mview_state table.
*/
class Changelog implements ChangelogInterface
{
/**
* Suffix for changelog table
*/
const NAME_SUFFIX = 'cl';
/**
* Column name of changelog entity
*/
const COLUMN_NAME = 'entity_id';
/**
* Database connection
*
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected $connection;
/**
* View Id identifier
*
* @var string
*/
protected $viewId;
/**
* @var \Magento\Framework\App\ResourceConnection
*/
protected $resource;
/**
* @param \Magento\Framework\App\ResourceConnection $resource
* @throws ConnectionException
*/
public function __construct(\Magento\Framework\App\ResourceConnection $resource)
{
$this->connection = $resource->getConnection();
$this->resource = $resource;
$this->checkConnection();
}
/**
* Check DB connection
*
* @return void
* @throws ConnectionException
*/
protected function checkConnection()
{
if (!$this->connection) {
throw new ConnectionException(
new Phrase("The write connection to the database isn't available. Please try again later.")
);
}
}
/**
* Create changelog table
*
* @return void
* @throws \Exception
*/
public function create()
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
$table = $this->connection->newTable(
$changelogTableName
)->addColumn(
'version_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Version ID'
)->addColumn(
$this->getColumnName(),
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Entity ID'
);
$this->connection->createTable($table);
}
}
/**
* Drop changelog table
*
* @return void
* @throws ChangelogTableNotExistsException
*/
public function drop()
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
}
$this->connection->dropTable($changelogTableName);
}
/**
* Clear changelog table by version_id
*
* @param int $versionId
* @return boolean
* @throws ChangelogTableNotExistsException
*/
public function clear($versionId)
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
}
$this->connection->delete($changelogTableName, ['version_id < ?' => (int)$versionId]);
return true;
}
/**
* Retrieve entity ids by range [$fromVersionId..$toVersionId]
*
* @param int $fromVersionId
* @param int $toVersionId
* @return int[]
* @throws ChangelogTableNotExistsException
*/
public function getList($fromVersionId, $toVersionId)
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
}
$select = $this->connection->select()->distinct(
true
)->from(
$changelogTableName,
[$this->getColumnName()]
)->where(
'version_id > ?',
(int)$fromVersionId
)->where(
'version_id <= ?',
(int)$toVersionId
);
return array_map('intval', $this->connection->fetchCol($select));
}
/**
* Get maximum version_id from changelog
*
* @return int
* @throws ChangelogTableNotExistsException
* @throws RuntimeException
*/
public function getVersion()
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
}
$select = $this->connection->select()->from($changelogTableName)->order('version_id DESC')->limit(1);
$row = $this->connection->fetchRow($select);
if ($row === false) {
return 0;
} else {
if (is_array($row) && array_key_exists('version_id', $row)) {
return (int)$row['version_id'];
} else {
throw new RuntimeException(
new Phrase("Table status for %1 is incorrect. Can`t fetch version id.", [$changelogTableName])
);
}
}
}
/**
* Get changlog name
*
* Build a changelog name by concatenating view identifier and changelog name suffix.
*
* @throws \DomainException
* @return string
*/
public function getName()
{
if (strlen($this->viewId) == 0) {
throw new \DomainException(
new Phrase("View's identifier is not set")
);
}
return $this->viewId . '_' . self::NAME_SUFFIX;
}
/**
* Get changlog entity column name
*
* @return string
*/
public function getColumnName()
{
return self::COLUMN_NAME;
}
/**
* Set view's identifier
*
* @param string $viewId
* @return Changelog
*/
public function setViewId($viewId)
{
$this->viewId = $viewId;
return $this;
}
/**
* Get view's identifier
*
* @return string
*/
public function getViewId()
{
return $this->viewId;
}
}