Skip to content

Commit

Permalink
Merge pull request #266 from magento-folks/split-db
Browse files Browse the repository at this point in the history
Fixed Issues:
- MAGETWO-56743: Unable to upgrade with split databases
  • Loading branch information
dvoskoboinikov authored Aug 19, 2016
2 parents 29d72c0 + f784247 commit 522fad0
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 85 deletions.
26 changes: 18 additions & 8 deletions app/code/Magento/OfflineShipping/Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* @var string
*/
private static $quoteConnectionName = 'checkout';

/**
* @var string
*/
private static $salesConnectionName = 'sales';

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
Expand Down Expand Up @@ -105,32 +115,32 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Simple Free Shipping'
);
$installer->getConnection()->addColumn(
$installer->getTable('sales_order_item'),
$installer->getConnection(self::$salesConnectionName)->addColumn(
$installer->getTable('sales_order_item', self::$salesConnectionName),
'free_shipping',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Free Shipping'
);
$installer->getConnection()->addColumn(
$installer->getTable('quote_address'),
$installer->getConnection(self::$quoteConnectionName)->addColumn(
$installer->getTable('quote_address', self::$quoteConnectionName),
'free_shipping',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Free Shipping'
);
$installer->getConnection()->addColumn(
$installer->getTable('quote_item'),
$installer->getConnection(self::$quoteConnectionName)->addColumn(
$installer->getTable('quote_item', self::$quoteConnectionName),
'free_shipping',
\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Free Shipping'
);
$installer->getConnection()->addColumn(
$installer->getTable('quote_address_item'),
$installer->getConnection(self::$quoteConnectionName)->addColumn(
$installer->getTable('quote_address_item', self::$quoteConnectionName),
'free_shipping',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/OfflineShipping/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"magento/module-backend": "100.2.*",
"magento/module-shipping": "100.2.*",
"magento/module-catalog": "101.1.*",
"magento/module-sales": "100.2.*",
"magento/module-sales-rule": "100.2.*",
"magento/module-directory": "100.2.*",
"magento/module-quote": "100.2.*",
"magento/framework": "100.2.*"
},
"suggest": {
"magento/module-checkout": "100.2.*",
"magento/module-sales": "100.2.*",
"magento/module-offline-shipping-sample-data": "Sample Data version:100.2.*"
},
"type": "magento2-module",
Expand Down
9 changes: 7 additions & 2 deletions app/code/Magento/Persistent/Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* @var string
*/
private static $connectionName = 'checkout';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -97,8 +102,8 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
* Alter quote table with is_persistent flag
*
*/
$installer->getConnection()->addColumn(
$installer->getTable('quote'),
$installer->getConnection(self::$connectionName)->addColumn(
$installer->getTable('quote', self::$connectionName),
'is_persistent',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
Expand Down
20 changes: 15 additions & 5 deletions app/code/Magento/Quote/Setup/QuoteSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class QuoteSetup extends EavSetup
*/
protected $_encryptor;

/**
* @var string
*/
private static $connectionName = 'checkout';

/**
* @param ModuleDataSetupInterface $setup
* @param Context $context
Expand Down Expand Up @@ -70,8 +75,11 @@ public function __construct(
*/
protected function _flatTableExist($table)
{
$tablesList = $this->getSetup()->getConnection()->listTables();
return in_array(strtoupper($this->getSetup()->getTable($table)), array_map('strtoupper', $tablesList));
$tablesList = $this->getSetup()->getConnection(self::$connectionName)->listTables();
return in_array(
strtoupper($this->getSetup()->getTable($table, self::$connectionName)),
array_map('strtoupper', $tablesList)
);
}

/**
Expand Down Expand Up @@ -107,13 +115,15 @@ public function addAttribute($entityTypeId, $code, array $attr)
*/
protected function _addFlatAttribute($table, $attribute, $attr)
{
$tableInfo = $this->getSetup()->getConnection()->describeTable($this->getSetup()->getTable($table));
$tableInfo = $this->getSetup()
->getConnection(self::$connectionName)
->describeTable($this->getSetup()->getTable($table, self::$connectionName));
if (isset($tableInfo[$attribute])) {
return $this;
}
$columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
$this->getSetup()->getConnection()->addColumn(
$this->getSetup()->getTable($table),
$this->getSetup()->getConnection(self::$connectionName)->addColumn(
$this->getSetup()->getTable($table, self::$connectionName),
$attribute,
$columnDefinition
);
Expand Down
15 changes: 10 additions & 5 deletions app/code/Magento/Quote/Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* @var string
*/
private static $connectionName = 'checkout';

/**
* {@inheritdoc}
*/
Expand All @@ -22,16 +27,16 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
$setup->startSetup();

if (version_compare($context->getVersion(), '2.0.1', '<')) {
$setup->getConnection()->addIndex(
$setup->getTable('quote_id_mask'),
$setup->getIdxName('quote_id_mask', ['masked_id']),
$setup->getConnection(self::$connectionName)->addIndex(
$setup->getTable('quote_id_mask', self::$connectionName),
$setup->getIdxName('quote_id_mask', ['masked_id'], '', self::$connectionName),
['masked_id']
);
}

if (version_compare($context->getVersion(), '2.0.2', '<')) {
$setup->getConnection()->changeColumn(
$setup->getTable('quote_address'),
$setup->getConnection(self::$connectionName)->changeColumn(
$setup->getTable('quote_address', self::$connectionName),
'street',
'street',
[
Expand Down
25 changes: 18 additions & 7 deletions app/code/Magento/Sales/Setup/SalesSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/**
* Setup Model of Sales Module
* @codeCoverageIgnore
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SalesSetup extends \Magento\Eav\Setup\EavSetup
{
Expand Down Expand Up @@ -48,6 +49,11 @@ class SalesSetup extends \Magento\Eav\Setup\EavSetup
*/
protected $encryptor;

/**
* @var string
*/
private static $connectionName = 'sales';

/**
* @param ModuleDataSetupInterface $setup
* @param Context $context
Expand Down Expand Up @@ -105,8 +111,11 @@ public function __construct(
*/
protected function _flatTableExist($table)
{
$tablesList = $this->getSetup()->getConnection()->listTables();
return in_array(strtoupper($this->getSetup()->getTable($table)), array_map('strtoupper', $tablesList));
$tablesList = $this->getSetup()->getConnection(self::$connectionName)->listTables();
return in_array(
strtoupper($this->getSetup()->getTable($table, self::$connectionName)),
array_map('strtoupper', $tablesList)
);
}

/**
Expand Down Expand Up @@ -143,13 +152,15 @@ public function addAttribute($entityTypeId, $code, array $attr)
*/
protected function _addFlatAttribute($table, $attribute, $attr)
{
$tableInfo = $this->getSetup()->getConnection()->describeTable($this->getSetup()->getTable($table));
$tableInfo = $this->getSetup()
->getConnection(self::$connectionName)
->describeTable($this->getSetup()->getTable($table, self::$connectionName));
if (isset($tableInfo[$attribute])) {
return $this;
}
$columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
$this->getSetup()->getConnection()->addColumn(
$this->getSetup()->getTable($table),
$this->getSetup()->getConnection(self::$connectionName)->addColumn(
$this->getSetup()->getTable($table, self::$connectionName),
$attribute,
$columnDefinition
);
Expand All @@ -169,8 +180,8 @@ protected function _addGridAttribute($table, $attribute, $attr, $entityTypeId)
{
if (in_array($entityTypeId, $this->_flatEntitiesGrid) && !empty($attr['grid'])) {
$columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
$this->getSetup()->getConnection()->addColumn(
$this->getSetup()->getTable($table . '_grid'),
$this->getSetup()->getConnection(self::$connectionName)->addColumn(
$this->getSetup()->getTable($table . '_grid', self::$connectionName),
$attribute,
$columnDefinition
);
Expand Down
51 changes: 17 additions & 34 deletions app/code/Magento/Sales/Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
*/
namespace Magento\Sales\Setup;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;

/**
* @codeCoverageIgnore
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* @var AdapterInterface
* @var string
*/
private $salesConnection;
private static $connectionName = 'sales';

/**
* {@inheritdoc}
Expand All @@ -29,36 +27,39 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '2.0.2', '<')) {
$connection = $installer->getConnection();
$connection = $installer->getConnection(self::$connectionName);
//sales_bestsellers_aggregated_daily
$connection->dropForeignKey(
$installer->getTable('sales_bestsellers_aggregated_daily'),
$installer->getTable('sales_bestsellers_aggregated_daily', self::$connectionName),
$installer->getFkName(
'sales_bestsellers_aggregated_daily',
'product_id',
'catalog_product_entity',
'entity_id'
'entity_id',
self::$connectionName
)
);
//sales_bestsellers_aggregated_monthly
$connection->dropForeignKey(
$installer->getTable('sales_bestsellers_aggregated_monthly'),
$installer->getTable('sales_bestsellers_aggregated_monthly', self::$connectionName),
$installer->getFkName(
'sales_bestsellers_aggregated_monthly',
'product_id',
'catalog_product_entity',
'entity_id'
'entity_id',
self::$connectionName
)
);

//sales_bestsellers_aggregated_yearly
$connection->dropForeignKey(
$installer->getTable('sales_bestsellers_aggregated_yearly'),
$installer->getTable('sales_bestsellers_aggregated_yearly', self::$connectionName),
$installer->getFkName(
'sales_bestsellers_aggregated_yearly',
'product_id',
'catalog_product_entity',
'entity_id'
'entity_id',
self::$connectionName
)
);

Expand All @@ -76,9 +77,9 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
*/
private function addColumnBaseGrandTotal(SchemaSetupInterface $installer)
{
$connection = $this->getSalesConnection();
$connection = $installer->getConnection(self::$connectionName);
$connection->addColumn(
$installer->getTable('sales_invoice_grid'),
$installer->getTable('sales_invoice_grid', self::$connectionName),
'base_grand_total',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
Expand All @@ -96,29 +97,11 @@ private function addColumnBaseGrandTotal(SchemaSetupInterface $installer)
*/
private function addIndexBaseGrandTotal(SchemaSetupInterface $installer)
{
$connection = $this->getSalesConnection();
$connection = $installer->getConnection(self::$connectionName);
$connection->addIndex(
$installer->getTable('sales_invoice_grid'),
$installer->getIdxName('sales_invoice_grid', ['base_grand_total']),
$installer->getTable('sales_invoice_grid', self::$connectionName),
$installer->getIdxName('sales_invoice_grid', ['base_grand_total'], '', self::$connectionName),
['base_grand_total']
);
}

/**
* @return AdapterInterface
*/
private function getSalesConnection()
{
if ($this->salesConnection === null) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var ResourceConnection $resourceConnection */
$resourceConnection = $objectManager->get(ResourceConnection::class);
try {
$this->salesConnection = $resourceConnection->getConnectionByName('sales');
} catch (\DomainException $e) {
$this->salesConnection = $resourceConnection->getConnection();
}
}
return $this->salesConnection;
}
}
Loading

0 comments on commit 522fad0

Please sign in to comment.