Skip to content

Commit 6826fc0

Browse files
author
Sergii Kovalenko
committed
MAGETWO-89402: Make 'db:status' command works properly
--fix sales table sync
1 parent cc8dea7 commit 6826fc0

File tree

10 files changed

+116
-37
lines changed

10 files changed

+116
-37
lines changed

app/code/Magento/SalesSequence/etc/db_schema.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
-->
88
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
10-
<table name="sales_sequence_profile" resource="sales" engine="innodb" comment="sales_sequence_profile">
10+
<table name="sales_sequence_profile" resource="sales" engine="innodb" comment="sales_sequence_profile" onCreate="skip-migration">
1111
<column xsi:type="int" name="profile_id" padding="10" unsigned="true" nullable="false" identity="true"
1212
comment="Id"/>
1313
<column xsi:type="int" name="meta_id" padding="10" unsigned="true" nullable="false" identity="false"
@@ -35,7 +35,7 @@
3535
<column name="suffix"/>
3636
</constraint>
3737
</table>
38-
<table name="sales_sequence_meta" resource="sales" engine="innodb" comment="sales_sequence_meta">
38+
<table name="sales_sequence_meta" resource="sales" engine="innodb" comment="sales_sequence_meta" onCreate="skip-migration">
3939
<column xsi:type="int" name="meta_id" padding="10" unsigned="true" nullable="false" identity="true"
4040
comment="Id"/>
4141
<column xsi:type="varchar" name="entity_type" nullable="false" length="32" comment="Prefix"/>

app/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,8 @@
14721472
<arguments>
14731473
<argument name="operations" xsi:type="array">
14741474
<item name="recreate_table" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\ReCreateTable</item>
1475-
<item name="drop_table" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\DropTable</item>
14761475
<item name="create_table" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\CreateTable</item>
1476+
<item name="drop_table" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\DropTable</item>
14771477
<item name="drop_reference" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\DropReference</item>
14781478
<item name="modify_column" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\ModifyColumn</item>
14791479
<item name="add_column" xsi:type="object">Magento\Framework\Setup\Declaration\Schema\Operations\AddColumn</item>

dev/tests/setup-integration/_files/Magento/TestSetupDeclarationModule1/etc/db_schema_whitelist.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,10 @@
7070
"constraint": {
7171
"": true
7272
}
73+
},
74+
"some_table": {
75+
"column": {
76+
"some_column": true
77+
}
7378
}
7479
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
return [
7+
'before' => 'CREATE TABLE `some_table` (
8+
`some_column` varchar(255) DEFAULT NULL COMMENT \'Some Column Name\'
9+
) ENGINE=InnoDB DEFAULT CHARSET=utf8',
10+
'after' => 'CREATE TABLE `some_table_renamed` (
11+
`some_column` varchar(255) DEFAULT NULL COMMENT \'Some Column Name\'
12+
) ENGINE=InnoDB DEFAULT CHARSET=utf8',
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
10+
<table name="some_table">
11+
<column name="some_column" xsi:type="varchar" length="255" comment="Some Column Name" />
12+
</table>
13+
</schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
10+
<table name="some_table_renamed" onCreate="migrateDataFromAnotherTable(some_table)">
11+
<column name="some_column" xsi:type="varchar" length="255" comment="Some Column Name" />
12+
</table>
13+
</schema>

dev/tests/setup-integration/testsuite/Magento/Setup/DeclarativeInstallerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,44 @@ public function testInstallWithCodeBaseRollback()
256256
$afterRollback = $this->describeTable->describeShard('default');
257257
self::assertEquals($this->getData()['after'], $afterRollback);
258258
}
259+
260+
/**
261+
* @moduleName Magento_TestSetupDeclarationModule1
262+
* @dataProviderFromFile Magento/TestSetupDeclarationModule1/fixture/declarative_installer/table_rename.php
263+
*/
264+
public function testTableRename()
265+
{
266+
$dataToMigrate = ['some_column' => 'Some Value'];
267+
//Move db_schema.xml file and tried to install
268+
$this->moduleManager->updateRevision(
269+
'Magento_TestSetupDeclarationModule1',
270+
'table_rename',
271+
'db_schema.xml',
272+
'etc'
273+
);
274+
$this->cliCommad->install(
275+
['Magento_TestSetupDeclarationModule1']
276+
);
277+
$before = $this->describeTable->describeShard('default');
278+
$adapter = $this->resourceConnection->getConnection('default');
279+
$adapter->insert(
280+
$this->resourceConnection->getTableName('some_table'),
281+
$dataToMigrate
282+
);
283+
self::assertEquals($this->getData()['before'], $before['some_table']);
284+
//Move db_schema.xml file and tried to install
285+
$this->moduleManager->updateRevision(
286+
'Magento_TestSetupDeclarationModule1',
287+
'table_rename_after',
288+
'db_schema.xml',
289+
'etc'
290+
);
291+
292+
$this->cliCommad->upgrade();
293+
$after = $this->describeTable->describeShard('default');
294+
self::assertEquals($this->getData()['after'], $after['some_table_renamed']);
295+
$select = $adapter->select()
296+
->from($this->resourceConnection->getTableName('some_table_renamed'));
297+
self::assertEquals([$dataToMigrate], $adapter->fetchAll($select));
298+
}
259299
}

lib/internal/Magento/Framework/Setup/Declaration/Schema/Diff/Diff.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
namespace Magento\Framework\Setup\Declaration\Schema\Diff;
88

99
use Magento\Framework\Component\ComponentRegistrar;
10+
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraints\Reference;
1011
use Magento\Framework\Setup\Declaration\Schema\Dto\ElementInterface;
1112
use Magento\Framework\Setup\Declaration\Schema\Dto\Table;
1213
use Magento\Framework\Setup\Declaration\Schema\Dto\TableElementInterface;
1314
use Magento\Framework\Setup\Declaration\Schema\ElementHistory;
1415
use Magento\Framework\Setup\Declaration\Schema\ElementHistoryFactory;
16+
use Magento\Framework\Setup\Declaration\Schema\Operations\DropReference;
1517

1618
/**
1719
* Holds information about all changes between 2 schemas: db and declaration XML.
@@ -181,22 +183,36 @@ public function canBeRegistered(ElementInterface $object, $operation)
181183
public function register(
182184
ElementInterface $dtoObject,
183185
$operation,
184-
ElementInterface $oldDtoObject = null,
185-
$tableKey = null
186+
ElementInterface $oldDtoObject = null
186187
) {
187188
if (!$this->canBeRegistered($dtoObject, $operation)) {
188189
return $this;
189190
}
190191

191192
$historyData = ['new' => $dtoObject, 'old' => $oldDtoObject];
192193
$history = $this->elementHistoryFactory->create($historyData);
193-
$dtoObjectName = $dtoObject instanceof TableElementInterface ?
194-
$dtoObject->getTable()->getName() : $dtoObject->getName();
195-
//We use not real tables but table indexes in order to be sure that order of table is correct
196-
$tableKey = $tableKey === null ? $this->tableIndexes[$dtoObjectName] : $this->tableIndexes[$tableKey];
197194
//dtoObjects can have 4 types: column, constraint, index, table
198-
$this->changes[$tableKey][$operation][] = $history;
195+
$this->changes[$this->findTableIndex($dtoObject, $operation)][$operation][] = $history;
199196
$this->debugChanges[$operation][] = $history;
200197
return $this;
201198
}
199+
200+
/**
201+
* As tables can references to each other, we need to take into account
202+
* that they should goes in specific structure: parent table -> child table
203+
* Also we should take into account, that first of all in any case we need to remove all foreign keys
204+
* from tables and only then modify that tables
205+
*
206+
* @param ElementInterface $element
207+
* @param string $operation
208+
* @return int
209+
*/
210+
private function findTableIndex(ElementInterface $element, string $operation) : int
211+
{
212+
$elementName = $element instanceof TableElementInterface ?
213+
$element->getTable()->getName() : $element->getName();
214+
//We use not real tables but table indexes in order to be sure that order of table is correct
215+
$tableIndex = $this->tableIndexes[$elementName] ?? INF;
216+
return $operation === DropReference::OPERATION_NAME ? 0 : (int) $tableIndex;
217+
}
202218
}

lib/internal/Magento/Framework/Setup/Declaration/Schema/Diff/DiffManager.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public function registerRemoval(
104104
) {
105105
foreach ($generatedElements as $generatedElement) {
106106
if ($generatedElement instanceof Reference) {
107-
$this->registerReferenceDrop($generatedElement, $diff);
108-
continue;
107+
$diff->register($generatedElement, DropReference::OPERATION_NAME, $generatedElement);
108+
} elseif ($generatedElement instanceof Table) {
109+
$diff->register($generatedElement, DropTable::OPERATION_NAME, $generatedElement);
110+
} else {
111+
$diff->register($generatedElement, DropElement::OPERATION_NAME, $generatedElement);
109112
}
110-
111-
$operation = $generatedElement instanceof Table ? DropTable::OPERATION_NAME : DropElement::OPERATION_NAME;
112-
$diff->register($generatedElement, $operation, $generatedElement);
113113
}
114114

115115
return $diff;
@@ -137,27 +137,6 @@ public function registerCreation(DiffInterface $diff, ElementInterface $element)
137137
return $diff;
138138
}
139139

140-
/**
141-
* We need to register drop of foreign key in scope of reference table.
142-
*
143-
* This done because reference table is goes first and starting from this table
144-
* there should be no foreign key on modified column.
145-
*
146-
* @param Reference $reference
147-
* @param Diff $diff
148-
* @return Diff
149-
*/
150-
public function registerReferenceDrop(Reference $reference, Diff $diff)
151-
{
152-
$diff->register(
153-
$reference,
154-
DropReference::OPERATION_NAME,
155-
$reference,
156-
$reference->getReferenceTable()->getName()
157-
);
158-
return $diff;
159-
}
160-
161140
/**
162141
* Depends on what should be changed we can re-create table or modify it.
163142
*

lib/internal/Magento/Framework/Setup/Declaration/Schema/Diff/SchemaDiff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private function createTableIndex(array $tableNames, array $generatedTableNames)
7373
foreach ($generatedTableNames as $tableName) {
7474
//If table exists only in db
7575
if (!in_array($tableName, $tableNames)) {
76-
array_unshift($tableNames, $tableName);
76+
$tableNames[] = $tableName;
7777
}
7878
}
7979

0 commit comments

Comments
 (0)