Skip to content

Commit 9d16fe4

Browse files
committed
Merge branch 'MAGETWO-87191' of github.com:magento-trigger/magento2ce into MAGETWO-88054
2 parents 105d482 + 55ee814 commit 9d16fe4

File tree

5 files changed

+85
-119
lines changed

5 files changed

+85
-119
lines changed

app/etc/di.xml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,14 +1419,6 @@
14191419
<arguments>
14201420
<argument name="triggers" xsi:type="array">
14211421
<item name="migrateDataFromSameTable" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Db\MySQL\DDL\Triggers\MigrateDataFrom</item>
1422-
<item name="migrateDataFromAnotherTable" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Db\MySQL\DDL\Triggers\MigrateDataFromAnotherTable</item>
1423-
</argument>
1424-
</arguments>
1425-
</type>
1426-
<type name="Magento\Framework\Setup\Declaration\Schema\Operations\CreateTable">
1427-
<arguments>
1428-
<argument name="triggers" xsi:type="array">
1429-
<item name="migrateDataFromAnotherTable" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Db\MySQL\DDL\Triggers\MigrateDataFromAnotherTable</item>
14301422
</argument>
14311423
</arguments>
14321424
</type>
@@ -1516,4 +1508,9 @@
15161508
<argument name="schemaPatchReader" xsi:type="object">\Magento\Framework\Setup\Patch\SchemaPatchReader</argument>
15171509
</arguments>
15181510
</type>
1511+
<type name="Magento\Framework\Setup\Declaration\Schema\Db\SchemaBuilder">
1512+
<arguments>
1513+
<argument name="logger" xsi:type="object">\Magento\Framework\Setup\Patch\DataPatchReader</argument>
1514+
</arguments>
1515+
</type>
15191516
</config>

lib/internal/Magento/Framework/Setup/Declaration/Schema/Db/MySQL/DDL/Triggers/MigrateDataFromAnotherTable.php

Lines changed: 0 additions & 88 deletions
This file was deleted.

lib/internal/Magento/Framework/Setup/Declaration/Schema/Db/SchemaBuilder.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Setup\Declaration\Schema\Dto\Schema;
1212
use Magento\Framework\Setup\Declaration\Schema\Dto\Table;
1313
use Magento\Framework\Setup\Declaration\Schema\Sharding;
14+
use Magento\Framework\Setup\Exception;
1415

1516
/**
1617
* This type of builder is responsible for converting ENTIRE data, that comes from db
@@ -98,15 +99,15 @@ public function build(Schema $schema)
9899
$table->addColumns($columns);
99100
//Process indexes
100101
foreach ($indexesData as $indexData) {
101-
$indexData['columns'] = $this->resolveInternalRelations($columns, $indexData);
102102
$indexData['table'] = $table;
103+
$indexData['columns'] = $this->resolveInternalRelations($columns, $indexData);
103104
$index = $this->elementFactory->create('index', $indexData);
104105
$indexes[$index->getName()] = $index;
105106
}
106107
//Process internal constraints
107108
foreach ($constrainsData as $constraintData) {
108-
$constraintData['columns'] = $this->resolveInternalRelations($columns, $constraintData);
109109
$constraintData['table'] = $table;
110+
$constraintData['columns'] = $this->resolveInternalRelations($columns, $constraintData);
110111
$constraint = $this->elementFactory->create($constraintData['type'], $constraintData);
111112
$constraints[$constraint->getName()] = $constraint;
112113
}
@@ -159,19 +160,29 @@ private function processReferenceKeys(array $tables)
159160
* @param Column[] $columns
160161
* @param array $data
161162
* @return Column[]
163+
* @throws Exception
162164
*/
163165
private function resolveInternalRelations(array $columns, array $data)
164166
{
165167
if (!is_array($data['column'])) {
166-
throw new \InvalidArgumentException("Cannot find columns for internal index");
168+
throw new Exception(
169+
__("Cannot find columns for internal index")
170+
);
167171
}
168172

169173
$referenceColumns = [];
170174
foreach ($data['column'] as $columnName) {
171175
if (!isset($columns[$columnName])) {
172-
//Depends on business logic, can either ignore non-existing column
173-
//or throw exception if db is not consistent and there is no column
174-
//that was specified for key
176+
$tableName = isset($data['table']) ? $data['table']->getName() : '';
177+
trigger_error(
178+
__(
179+
'Column %1 does not exist for index/constraint %2 in table %3.',
180+
$columnName,
181+
$data['name'],
182+
$tableName
183+
),
184+
E_USER_WARNING
185+
);
175186
} else {
176187
$referenceColumns[] = $columns[$columnName];
177188
}

lib/internal/Magento/Framework/Setup/Patch/PatchApplier.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,14 @@ public function applySchemaPatch($moduleName = null)
228228
$schemaPatch->apply();
229229
$this->patchHistory->fixPatch(get_class($schemaPatch));
230230
} catch (\Exception $e) {
231-
throw new Exception($e->getMessage());
231+
throw new Exception(
232+
__(
233+
'Unable to apply patch %1 for module %2. Original exception message: %3',
234+
get_class($schemaPatch),
235+
$moduleName,
236+
$e->getMessage()
237+
)
238+
);
232239
} finally {
233240
unset($schemaPatch);
234241
}

lib/internal/Magento/Framework/Setup/Test/Unit/Declaration/Schema/Db/SchemaBuilderTest.php

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,56 @@ private function createTimestampColumn($name, Table $table)
266266
* @param array $references
267267
* @param array $constraints
268268
* @param array $indexes
269-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
270269
*/
271270
public function testBuild(array $columns, array $references, array $constraints, array $indexes)
271+
{
272+
$this->prepareSchemaMocks($columns, $references, $constraints, $indexes);
273+
$resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
274+
->disableOriginalConstructor()
275+
->getMock();
276+
/** @var Schema $schema */
277+
$schema = $this->objectManagerHelper->getObject(
278+
Schema::class,
279+
['resourceConnection' => $resourceConnectionMock]
280+
);
281+
$this->model->build($schema);
282+
}
283+
284+
/**
285+
* @dataProvider dataProvider
286+
* @param array $columns
287+
* @param array $references
288+
* @param array $constraints
289+
* @param array $indexes
290+
* @expectedException \PHPUnit\Framework\Exception
291+
* @expectedExceptionMessage
292+
* User Warning: Column unknown_column does not exist for index/constraint FIRST_INDEX in table second_table
293+
*/
294+
public function testBuildUnknownIndexColumn(array $columns, array $references, array $constraints, array $indexes)
295+
{
296+
$indexes['second_table']['FIRST_INDEX']['column'][] = 'unknown_column';
297+
$this->prepareSchemaMocks($columns, $references, $constraints, $indexes);
298+
$resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
299+
->disableOriginalConstructor()
300+
->getMock();
301+
/** @var Schema $schema */
302+
$schema = $this->objectManagerHelper->getObject(
303+
Schema::class,
304+
['resourceConnection' => $resourceConnectionMock]
305+
);
306+
$this->model->build($schema);
307+
}
308+
309+
/**
310+
* Prepare mocks for test.
311+
*
312+
* @param array $columns
313+
* @param array $references
314+
* @param array $constraints
315+
* @param array $indexes
316+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
317+
*/
318+
private function prepareSchemaMocks(array $columns, array $references, array $constraints, array $indexes)
272319
{
273320
$withContext = [['first_table', 'default'], ['second_table', 'default']];
274321
$this->shardingMock->expects(self::once())
@@ -278,26 +325,26 @@ public function testBuild(array $columns, array $references, array $constraints,
278325
->method('readTables')
279326
->with('default')
280327
->willReturn(['first_table', 'second_table']);
281-
$this->dbSchemaReaderMock->expects(self::exactly(2))
328+
$this->dbSchemaReaderMock->expects($this->any())
282329
->method('getTableOptions')
283330
->withConsecutive(...array_values($withContext))
284331
->willReturnOnConsecutiveCalls(
285332
['Engine' => 'innodb', 'Comment' => ''],
286333
['Engine' => 'innodb', 'Comment' => 'Not null comment']
287334
);
288-
$this->dbSchemaReaderMock->expects(self::exactly(2))
335+
$this->dbSchemaReaderMock->expects($this->any())
289336
->method('readColumns')
290337
->withConsecutive(...array_values($withContext))
291338
->willReturnOnConsecutiveCalls($columns['first_table'], $columns['second_table']);
292-
$this->dbSchemaReaderMock->expects(self::exactly(2))
339+
$this->dbSchemaReaderMock->expects($this->any())
293340
->method('readIndexes')
294341
->withConsecutive(...array_values($withContext))
295342
->willReturnOnConsecutiveCalls([], $indexes['second_table']);
296-
$this->dbSchemaReaderMock->expects(self::exactly(2))
343+
$this->dbSchemaReaderMock->expects($this->any())
297344
->method('readConstraints')
298345
->withConsecutive(...array_values($withContext))
299346
->willReturnOnConsecutiveCalls($constraints['first_table'], []);
300-
$this->dbSchemaReaderMock->expects(self::exactly(2))
347+
$this->dbSchemaReaderMock->expects($this->any())
301348
->method('readReferences')
302349
->withConsecutive(...array_values($withContext))
303350
->willReturnOnConsecutiveCalls($references['first_table'], []);
@@ -322,7 +369,7 @@ public function testBuild(array $columns, array $references, array $constraints,
322369
);
323370
$table->addColumns([$firstColumn, $foreignColumn, $timestampColumn]);
324371
$table->addConstraints([$foreignKey, $primaryKey]);
325-
$this->elementFactoryMock->expects(self::exactly(9))
372+
$this->elementFactoryMock->expects($this->any())
326373
->method('create')
327374
->withConsecutive(
328375
[
@@ -426,14 +473,6 @@ public function testBuild(array $columns, array $references, array $constraints,
426473
$index,
427474
$foreignKey
428475
);
429-
$resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
430-
->disableOriginalConstructor()
431-
->getMock();
432-
/** @var Schema $schema */
433-
$schema = $this->objectManagerHelper->getObject(
434-
Schema::class,
435-
['resourceConnection' => $resourceConnectionMock]
436-
);
437-
$this->model->build($schema);
476+
438477
}
439478
}

0 commit comments

Comments
 (0)