Skip to content

Commit

Permalink
MAG-735: Migrate setup scripts to db_schema.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
ebanolopes committed Mar 9, 2022
1 parent c5d48bf commit 9fb0225
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 135 deletions.
23 changes: 18 additions & 5 deletions Helper/Retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Signifyd\Connect\Model\ResourceModel\Casedata\CollectionFactory as CasedataCollectionFactory;
use Signifyd\Connect\Model\ResourceModel\Casedata as CasedataResourceModel;
use Signifyd\Connect\Logger\Logger;
use Signifyd\Connect\Model\CasedataFactory;

class Retry extends AbstractHelper
{
Expand All @@ -25,24 +26,32 @@ class Retry extends AbstractHelper
*/
protected $logger;

/**
* @var CasedataFactory
*/
protected $casedataFactory;

/**
* Retry constructor.
* @param Context $context
* @param CasedataCollectionFactory $casedataCollectionFactory
* @param CasedataResourceModel $casedataResourceModel
* @param Logger $logger
* @param CasedataFactory $casedataFactory
*/
public function __construct(
Context $context,
CasedataCollectionFactory $casedataCollectionFactory,
CasedataResourceModel $casedataResourceModel,
Logger $logger
Logger $logger,
CasedataFactory $casedataFactory
) {
parent::__construct($context);

$this->casedataCollectionFactory = $casedataCollectionFactory;
$this->casedataResourceModel = $casedataResourceModel;
$this->logger = $logger;
$this->casedataFactory = $casedataFactory;
}

/**
Expand Down Expand Up @@ -72,13 +81,17 @@ public function getRetryCasesByStatus($status)

/** @var \Signifyd\Connect\Model\Casedata $case */
foreach ($casesCollection->getItems() as $case) {
$retries = $case->getData('retries');
$caseToUpdate = $this->casedataFactory->create();
$this->casedataResourceModel->loadForUpdate($caseToUpdate, $case->getId());

$retries = $caseToUpdate->getData('retries');
$secondsAfterUpdate = $case->getData('seconds_after_update');

if ($secondsAfterUpdate > $retryTimes[$retries]) {
$casesToRetry[$case->getId()] = $case;
$case->setData('retries', $retries + 1);
$this->casedataResourceModel->save($case);

$casesToRetry[$caseToUpdate->getId()] = $caseToUpdate;
$caseToUpdate->setData('retries', $retries + 1);
$this->casedataResourceModel->save($caseToUpdate);
}
}

Expand Down
49 changes: 30 additions & 19 deletions Setup/InstallSchema.php → Setup/Patch/Data/InstallConfig.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?php
/**
* Copyright 2015 SIGNIFYD Inc. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace Signifyd\Connect\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\App\Config\Storage\WriterInterface;
namespace Signifyd\Connect\Setup\Patch\Data;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;

/**
* @codeCoverageIgnore
*/
class InstallSchema implements InstallSchemaInterface
class InstallConfig implements DataPatchInterface
{
protected $logger;

Expand All @@ -35,6 +26,12 @@ class InstallSchema implements InstallSchemaInterface
*/
protected $dateTime;

/**
* @param \Signifyd\Connect\Logger\Install $logger
* @param WriterInterface $writerInterface
* @param ScopeConfigInterface $scopeConfigInterface
* @param DateTime $dateTime
*/
public function __construct(
\Signifyd\Connect\Logger\Install $logger,
WriterInterface $writerInterface,
Expand All @@ -47,11 +44,7 @@ public function __construct(
$this->dateTime = $dateTime;
}

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
public function apply()
{
try {
if ($this->scopeConfigInterface->isSetFlag('signifyd_connect/general/installation_date') === false) {
Expand All @@ -63,4 +56,22 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
$this->logger->error($e->getMessage());
}
}

/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}

/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [

];
}
}
85 changes: 85 additions & 0 deletions Setup/Patch/Data/UpdateOrderId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Signifyd\Connect\Setup\Patch\Data;

use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Signifyd\Connect\Logger\Install;

class UpdateOrderId implements DataPatchInterface
{
/**
* @var SchemaSetupInterface
*/
protected $schemaSetupInterface;

/**
* @var Install
*/
protected $logger;

/**
* @var WriterInterface
*/
protected $configWriter;

/**
* @param SchemaSetupInterface $schemaSetupInterface
* @param Install $logger
* @param WriterInterface $configWriter
*/
public function __construct(
SchemaSetupInterface $schemaSetupInterface,
Install $logger,
WriterInterface $configWriter
) {
$this->schemaSetupInterface = $schemaSetupInterface;
$this->logger = $logger;
$this->configWriter = $configWriter;
}

public function apply()
{
$signifydConnectCase = $this->schemaSetupInterface->getTable('signifyd_connect_case');
$salesOrder = $this->schemaSetupInterface->getTable('sales_order');

try {
$this->schemaSetupInterface->getConnection()->query("UPDATE ". $signifydConnectCase ." JOIN " . $salesOrder . " ON " .
$signifydConnectCase .".order_increment = " . $salesOrder . ".increment_id SET " .
$signifydConnectCase .".order_id = " . $salesOrder . ".entity_id WHERE " .
$signifydConnectCase . ".magento_status='complete'");
} catch (\Exception $e) {
$this->logger->debug('Update order_id on magento status complete failed');
$this->configWriter->save("signifyd/general/upgrade4.3_inconsistency", "setup");
}

try {
$this->schemaSetupInterface->getConnection()->query("UPDATE ". $signifydConnectCase ." JOIN " . $salesOrder . " ON " .
$signifydConnectCase .".order_increment = " . $salesOrder . ".increment_id SET ".
$signifydConnectCase .".order_id = " . $salesOrder . ".entity_id WHERE ".
$signifydConnectCase . ".magento_status<>'complete'");
} catch (\Exception $e) {
$this->logger->debug('Update order_id on magento status different from complete failed');
$this->configWriter->save("signifyd/general/upgrade4.3_inconsistency", "setup");
}
}

/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}

/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [

];
}
}
111 changes: 0 additions & 111 deletions Setup/UpgradeSchema.php

This file was deleted.

1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<column identity="false" name="quote_id" padding="10" nullable="true" unsigned="true" xsi:type="int"/>
<column length="255" name="checkout_token" nullable="false" xsi:type="varchar"/>
<column length="255" name="policy_name" nullable="false" xsi:type="varchar"/>
<column identity="false" name="lock_start" padding="10" nullable="true" default="null" xsi:type="int"/>
<index referenceId="INDEX_MAGENTO_STATUS" indexType="btree">
<column name="magento_status"/>
</index>
Expand Down

0 comments on commit 9fb0225

Please sign in to comment.