Skip to content

Commit

Permalink
MAGETWO-51078: Changes are not applied after switching from Update on…
Browse files Browse the repository at this point in the history
… Schedule to Update on Save mode
  • Loading branch information
rganin committed Apr 15, 2016
1 parent f93b8da commit 6ae3c48
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
Expand Up @@ -185,13 +185,13 @@ public function testCreate()
$this->model->create();
}

public function testCreateWithException()
public function testCreateWithExistingTable()
{
$changelogTableName = 'viewIdtest_cl';
$this->mockIsTableExists($changelogTableName, true);
$this->mockGetTableName();

$this->setExpectedException('Exception', "Table {$changelogTableName} already exist");
$this->connectionMock->expects($this->never())->method('createTable');
$this->model->setViewId('viewIdtest');
$this->model->create();
}
Expand Down
12 changes: 1 addition & 11 deletions lib/internal/Magento/Framework/Mview/Test/Unit/ViewTest.php
Expand Up @@ -221,15 +221,11 @@ public function testUnsubscribe()
$this->stateMock->expects($this->once())
->method('getMode')
->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
$this->stateMock->expects($this->once())
->method('setVersionId')
->with(null)
->will($this->returnSelf());
$this->stateMock->expects($this->once())
->method('setMode')
->with(\Magento\Framework\Mview\View\StateInterface::MODE_DISABLED)
->will($this->returnSelf());
$this->changelogMock->expects($this->once())
$this->changelogMock->expects($this->never())
->method('drop');
$subscriptionMock = $this->getMock(
\Magento\Framework\Mview\View\Subscription::class,
Expand Down Expand Up @@ -312,9 +308,6 @@ public function testUpdate()
$this->stateMock->expects($this->once())
->method('setVersionId')
->will($this->returnSelf());
$this->stateMock->expects($this->once())
->method('getMode')
->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
$this->stateMock->expects($this->exactly(2))
->method('getStatus')
->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
Expand Down Expand Up @@ -376,9 +369,6 @@ public function testUpdateWithException()
->will($this->returnValue($lastVersionId));
$this->stateMock->expects($this->never())
->method('setVersionId');
$this->stateMock->expects($this->once())
->method('getMode')
->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED));
$this->stateMock->expects($this->exactly(2))
->method('getStatus')
->will($this->returnValue(\Magento\Framework\Mview\View\StateInterface::STATUS_IDLE));
Expand Down
26 changes: 14 additions & 12 deletions lib/internal/Magento/Framework/Mview/View.php
Expand Up @@ -8,6 +8,7 @@

namespace Magento\Framework\Mview;

use Magento\Framework\Mview\View\ChangelogTableNotExistsException;
use Magento\Framework\Mview\View\SubscriptionFactory;

/**
Expand Down Expand Up @@ -228,11 +229,8 @@ public function unsubscribe()
$subscriptionInstance->remove();
}

// Drop changelog table
$this->getChangelog()->drop();

// Update view state
$this->getState()->setVersionId(null)->setMode(View\StateInterface::MODE_DISABLED)->save();
$this->getState()->setMode(View\StateInterface::MODE_DISABLED)->save();
} catch (\Exception $e) {
throw $e;
}
Expand All @@ -249,10 +247,12 @@ public function unsubscribe()
*/
public function update()
{
if ($this->getState()->getMode() == View\StateInterface::MODE_ENABLED &&
$this->getState()->getStatus() == View\StateInterface::STATUS_IDLE
) {
$currentVersionId = $this->getChangelog()->getVersion();
if ($this->getState()->getStatus() == View\StateInterface::STATUS_IDLE) {
try {
$currentVersionId = $this->getChangelog()->getVersion();
} catch (ChangelogTableNotExistsException $e) {
return;
}
$lastVersionId = $this->getState()->getVersionId();
$ids = $this->getChangelog()->getList($lastVersionId, $currentVersionId);
if ($ids) {
Expand All @@ -261,13 +261,15 @@ public function update()
try {
$action->execute($ids);
$this->getState()->loadByView($this->getId());
$statusToRestore = $this->getState()->getStatus() ==
View\StateInterface::STATUS_SUSPENDED ? View\StateInterface::STATUS_SUSPENDED : View\StateInterface::STATUS_IDLE;
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
? View\StateInterface::STATUS_SUSPENDED
: View\StateInterface::STATUS_IDLE;
$this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
} catch (\Exception $exception) {
$this->getState()->loadByView($this->getId());
$statusToRestore = $this->getState()->getStatus() ==
View\StateInterface::STATUS_SUSPENDED ? View\StateInterface::STATUS_SUSPENDED : View\StateInterface::STATUS_IDLE;
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
? View\StateInterface::STATUS_SUSPENDED
: View\StateInterface::STATUS_IDLE;
$this->getState()->setStatus($statusToRestore)->save();
throw $exception;
}
Expand Down
53 changes: 25 additions & 28 deletions lib/internal/Magento/Framework/Mview/View/Changelog.php
Expand Up @@ -70,40 +70,37 @@ protected function checkConnection()
public function create()
{
$changelogTableName = $this->resource->getTableName($this->getName());
if ($this->connection->isTableExists($changelogTableName)) {
throw new \Exception("Table {$changelogTableName} already exist");
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);
}

$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 \Exception
* @throws ChangelogTableNotExistsException
*/
public function drop()
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new \Exception("Table {$changelogTableName} does not exist");
throw new ChangelogTableNotExistsException("Table {$changelogTableName} does not exist");
}

$this->connection->dropTable($changelogTableName);
Expand All @@ -114,13 +111,13 @@ public function drop()
*
* @param int $versionId
* @return boolean
* @throws \Exception
* @throws ChangelogTableNotExistsException
*/
public function clear($versionId)
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new \Exception("Table {$changelogTableName} does not exist");
throw new ChangelogTableNotExistsException("Table {$changelogTableName} does not exist");
}

$this->connection->delete($changelogTableName, ['version_id <= ?' => (int)$versionId]);
Expand All @@ -134,13 +131,13 @@ public function clear($versionId)
* @param int $fromVersionId
* @param int $toVersionId
* @return int[]
* @throws \Exception
* @throws ChangelogTableNotExistsException
*/
public function getList($fromVersionId, $toVersionId)
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new \Exception("Table {$changelogTableName} does not exist");
throw new ChangelogTableNotExistsException("Table {$changelogTableName} does not exist");
}

$select = $this->connection->select()->distinct(
Expand All @@ -161,15 +158,15 @@ public function getList($fromVersionId, $toVersionId)

/**
* Get maximum version_id from changelog
*
* @return int
* @throws ChangelogTableNotExistsException
* @throws \Exception
*/
public function getVersion()
{
$changelogTableName = $this->resource->getTableName($this->getName());
if (!$this->connection->isTableExists($changelogTableName)) {
throw new \Exception("Table {$changelogTableName} does not exist");
throw new ChangelogTableNotExistsException("Table {$changelogTableName} does not exist");
}
$row = $this->connection->fetchRow('SHOW TABLE STATUS LIKE ?', [$changelogTableName]);
if (isset($row['Auto_increment'])) {
Expand Down
@@ -0,0 +1,16 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Mview\View;

/**
* Class ChangelogTableNotExistsException
* @package Magento\Framework\Mview\View
*/
class ChangelogTableNotExistsException extends \Exception
{

}

0 comments on commit 6ae3c48

Please sign in to comment.