1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\MigrateData\Traits; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\ORM\DataObject; |
7
|
|
|
use SilverStripe\ORM\DB; |
8
|
|
|
use SilverStripe\ORM\Queries\SQLSelect; |
9
|
|
|
use SilverStripe\Versioned\Versioned; |
|
|
|
|
10
|
|
|
use Sunnysideup\Flush\FlushNow; |
11
|
|
|
use Sunnysideup\Flush\FlushNowImplementor; |
12
|
|
|
|
13
|
|
|
trait HelperMethods |
14
|
|
|
{ |
15
|
|
|
use FlushNow; |
16
|
|
|
|
17
|
|
|
public function deleteObject($obj) |
18
|
|
|
{ |
19
|
|
|
if ($obj->exists()) { |
20
|
|
|
FlushNowImplementor::do_flush('DELETING ' . $obj->ClassName . '.' . $obj->ID, 'deleted'); |
21
|
|
|
if ($obj->hasExtension(Versioned::class)) { |
22
|
|
|
$obj->DeleteFromStage(Versioned::LIVE); |
23
|
|
|
$obj->DeleteFromStage(Versioned::DRAFT); |
24
|
|
|
} else { |
25
|
|
|
$obj->delete(); |
26
|
|
|
} |
27
|
|
|
@$obj->flushCache(); |
|
|
|
|
28
|
|
|
} else { |
29
|
|
|
FlushNowImplementor::do_flush('DOES NOT EXIST', 'added'); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $queries list of queries |
35
|
|
|
* @param string $name what is this list about? |
36
|
|
|
*/ |
37
|
|
|
protected function runSQLQueries($queries, $name = 'UPDATE QUERIES') |
38
|
|
|
{ |
39
|
|
|
if ([] !== $queries) { |
40
|
|
|
$this->flushNow('<h3>Performing ' . $name . ' Queries</h3>'); |
41
|
|
|
foreach ($queries as $sqlQuery) { |
42
|
|
|
$this->runUpdateQuery($sqlQuery); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $sqlQuery list of queries |
49
|
|
|
* @param int $indents what is this list about? |
50
|
|
|
*/ |
51
|
|
|
protected function runUpdateQuery(string $sqlQuery, ?int $indents = 1) |
52
|
|
|
{ |
53
|
|
|
$this->flushNow(str_replace('"', '`', $sqlQuery), 'created'); |
54
|
|
|
$prefix = str_repeat(' ... ', $indents); |
|
|
|
|
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
DB::query($sqlQuery); |
58
|
|
|
$this->flushNow($prefix . ' DONE ' . DB::affected_rows() . ' rows affected'); |
59
|
|
|
} catch (Exception $exception) { |
60
|
|
|
$this->flushNow($prefix . "ERROR: Unable to run '{$sqlQuery}'", 'deleted'); |
61
|
|
|
$this->flushNow('' . $exception->getMessage() . '', 'deleted'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $publishClasses list of class names to write / publish |
67
|
|
|
*/ |
68
|
|
|
protected function runPublishClasses(array $publishClasses) |
69
|
|
|
{ |
70
|
|
|
if ([] !== $publishClasses) { |
71
|
|
|
$this->flushNow('<h3>Publish classes</h3>'); |
72
|
|
|
foreach ($publishClasses as $publishClass) { |
73
|
|
|
$this->flushNow('<h6>Publishing ' . $publishClass . '</h6>'); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$count = 0; |
77
|
|
|
$publishItems = $publishClass::get(); |
78
|
|
|
foreach ($publishItems as $publishItem) { |
79
|
|
|
++$count; |
80
|
|
|
$this->flushNow( |
81
|
|
|
'Publishing ' . $count . ' of ' . $publishItems->count() . |
82
|
|
|
' ' . |
83
|
|
|
$publishClass . |
84
|
|
|
' item' . (1 === $publishItems->exists() ? '' : 's') . '.' |
85
|
|
|
); |
86
|
|
|
$publishItem->write(); |
87
|
|
|
if ($publishItem->hasMethod('publishRecursive')) { |
88
|
|
|
$publishItem->publishRecursive(); |
89
|
|
|
$this->flushNow('... DONE - PUBLISHED'); |
90
|
|
|
} else { |
91
|
|
|
$this->flushNow('... DONE - WRITE ONLY'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} catch (Exception $e) { |
95
|
|
|
$this->flushNow('Unable to publish ' . $publishClass . '', 'error'); |
96
|
|
|
$this->flushNow('' . $e->getMessage() . '', 'error'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function makeTableObsolete(string $tableName, ?bool $doEvenIfAlreadyObsolete = false): bool |
103
|
|
|
{ |
104
|
|
|
$schema = $this->getSchema(); |
105
|
|
|
if ($this->tableExists($tableName)) { |
106
|
|
|
if (!$this->tableExists('_obsolete_' . $tableName) || $doEvenIfAlreadyObsolete) { |
107
|
|
|
$schema->dontRequireTable($tableName); |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
$this->flushNow('Table ' . $tableName . ' is already obsolete'); |
112
|
|
|
} else { |
113
|
|
|
$this->flushNow('Table ' . $tableName . ' does not exist.'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function tableExists(string $tableName): bool |
120
|
|
|
{ |
121
|
|
|
$schema = $this->getSchema(); |
122
|
|
|
|
123
|
|
|
return (bool) $schema->hasTable($tableName); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function clearTable(string $tableName) |
127
|
|
|
{ |
128
|
|
|
DB::get_conn()->clearTable($tableName); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function replaceTable(string $a, string $b, ?bool $keepBackup = false) |
132
|
|
|
{ |
133
|
|
|
if ($this->tableExists($a)) { |
134
|
|
|
if ($this->tableExists($b)) { |
135
|
|
|
$itemsInDB = DB::query('SELECT DISTINCT ID FROM ' . stripslashes($b) . ';'); |
136
|
|
|
if ($itemsInDB->numRecords() > 0 && $keepBackup) { |
137
|
|
|
$this->makeTableObsolete($b, true); |
138
|
|
|
$this->flushNow('Backing up ' . $b, 'deleted'); |
139
|
|
|
} |
140
|
|
|
$this->dropTable($b); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!$this->tableExists($b)) { |
144
|
|
|
$this->renameTable($a, $b); |
145
|
|
|
} else { |
146
|
|
|
$this->flushNow('Could not delete ' . $b, 'deleted'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* delete the table both with and without slashes. |
153
|
|
|
*/ |
154
|
|
|
protected function dropTable(string $tableName) |
155
|
|
|
{ |
156
|
|
|
$this->flushNow('Deleting ' . $tableName, 'deleted'); |
157
|
|
|
DB::query('DROP TABLE IF EXISTS "' . $tableName . '";'); |
158
|
|
|
DB::query('DROP TABLE IF EXISTS "' . stripslashes($tableName) . '";'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function renameTable(string $a, string $b) |
162
|
|
|
{ |
163
|
|
|
$this->flushNow('Moving "' . $a . '" to "' . $b . '"', 'warning'); |
164
|
|
|
if (!$this->tableExists($a)) { |
165
|
|
|
$this->flushNow(' -- Could not find "' . $a . '", consider using replaceTable', 'deleted'); |
166
|
|
|
|
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
if ($this->tableExists($b)) { |
170
|
|
|
$this->flushNow(' -- Destination table already exists "' . $b . '", consider using replaceTable', 'deleted'); |
171
|
|
|
|
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
if (false !== strpos($a, '\\') || false !== strpos($b, '\\')) { |
175
|
|
|
$this->flushNow('Special slashes case "' . $a . '" to "' . $b . '"', 'warning'); |
176
|
|
|
DB::query('ALTER TABLE "' . stripslashes($a) . '" RENAME "' . stripslashes($b) . '"'); |
177
|
|
|
} else { |
178
|
|
|
$this->getSchema()->renameTable($a, $b); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function fieldExists(string $tableName, string $fieldName): bool |
183
|
|
|
{ |
184
|
|
|
$key = $tableName; |
185
|
|
|
if (!isset($this->_cacheFieldExists[$key])) { |
186
|
|
|
$schema = $this->getSchema(); |
187
|
|
|
$this->_cacheFieldExists[$key] = $schema->fieldList($tableName); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->_cacheFieldExists[$key][$fieldName] ?? false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function renameField(string $table, string $oldFieldName, string $newFieldName) |
194
|
|
|
{ |
195
|
|
|
$this->getSchema()->dontRequireField($table, $oldFieldName, $newFieldName); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
protected function getSchema() |
199
|
|
|
{ |
200
|
|
|
if (null === $this->_schema) { |
201
|
|
|
$this->_schema = DB::get_schema(); |
|
|
|
|
202
|
|
|
$this->_schema->schemaUpdate(function () { |
203
|
|
|
return true; |
204
|
|
|
}); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this->_schema; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
protected function getSchemaForDataObject() |
211
|
|
|
{ |
212
|
|
|
if (null === $this->_schemaForDataObject) { |
213
|
|
|
$this->_schemaForDataObject = DataObject::getSchema(); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->_schemaForDataObject; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function getListOfIDs(string $tableName, ?array $leftJoin = [], ?string $where = '') |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
return $this->getListAsIterableQuery($tableName, $leftJoin = [], $where = '') |
222
|
|
|
->keyedColumn('ID'); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
protected function getListAsIterableQuery(string $tableName, ?array $leftJoin = [], ?string $where = '') |
226
|
|
|
{ |
227
|
|
|
$sqlSelect = new SQLSelect(); |
228
|
|
|
$sqlSelect->setFrom($tableName); |
229
|
|
|
|
230
|
|
|
if ($leftJoin) { |
|
|
|
|
231
|
|
|
$sqlSelect->addLeftJoin($leftJoin['table'], $leftJoin['onPredicate']); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if ($where) { |
235
|
|
|
$sqlSelect->addWhere($where); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$sqlSelect->setOrderBy($tableName . '.ID'); |
239
|
|
|
|
240
|
|
|
return $sqlSelect->execute(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
protected function writeObject($obj, ?array $row = [], ?bool $isPage = false) |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
DataObject::Config()->set('validation_enabled', false); |
246
|
|
|
if ($obj->hasMethod('writeToStage')) { |
247
|
|
|
$obj->writeToStage(Versioned::DRAFT); |
248
|
|
|
$obj->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
249
|
|
|
$obj->publishRecursive(); |
250
|
|
|
} else { |
251
|
|
|
$obj->write(); |
252
|
|
|
} |
253
|
|
|
$obj->flushCache(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
protected function writePage($obj, $row) |
257
|
|
|
{ |
258
|
|
|
return $this->writeObject($obj, $row, $isPage = true); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths