diff --git a/app/code/Magento/OfflineShipping/Setup/InstallSchema.php b/app/code/Magento/OfflineShipping/Setup/InstallSchema.php index 8ce77e6098b43..0c2b62f908894 100644 --- a/app/code/Magento/OfflineShipping/Setup/InstallSchema.php +++ b/app/code/Magento/OfflineShipping/Setup/InstallSchema.php @@ -15,6 +15,16 @@ */ class InstallSchema implements InstallSchemaInterface { + /** + * @var string + */ + private static $quoteConnectionName = 'checkout'; + + /** + * @var string + */ + private static $salesConnectionName = 'sales'; + /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) @@ -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, diff --git a/app/code/Magento/OfflineShipping/composer.json b/app/code/Magento/OfflineShipping/composer.json index e9a759057d322..3c959b4dec766 100644 --- a/app/code/Magento/OfflineShipping/composer.json +++ b/app/code/Magento/OfflineShipping/composer.json @@ -8,7 +8,6 @@ "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.*", @@ -16,6 +15,7 @@ }, "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", diff --git a/app/code/Magento/Persistent/Setup/InstallSchema.php b/app/code/Magento/Persistent/Setup/InstallSchema.php index 940133fcf5dd7..89e7d88fbd066 100644 --- a/app/code/Magento/Persistent/Setup/InstallSchema.php +++ b/app/code/Magento/Persistent/Setup/InstallSchema.php @@ -15,6 +15,11 @@ */ class InstallSchema implements InstallSchemaInterface { + /** + * @var string + */ + private static $connectionName = 'checkout'; + /** * {@inheritdoc} */ @@ -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, diff --git a/app/code/Magento/Quote/Setup/QuoteSetup.php b/app/code/Magento/Quote/Setup/QuoteSetup.php index 2608580cf8fa3..1ef5f4b0dae2e 100644 --- a/app/code/Magento/Quote/Setup/QuoteSetup.php +++ b/app/code/Magento/Quote/Setup/QuoteSetup.php @@ -29,6 +29,11 @@ class QuoteSetup extends EavSetup */ protected $_encryptor; + /** + * @var string + */ + private static $connectionName = 'checkout'; + /** * @param ModuleDataSetupInterface $setup * @param Context $context @@ -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) + ); } /** @@ -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 ); diff --git a/app/code/Magento/Quote/Setup/UpgradeSchema.php b/app/code/Magento/Quote/Setup/UpgradeSchema.php index d2163035bdcbf..0ea04377eaa39 100644 --- a/app/code/Magento/Quote/Setup/UpgradeSchema.php +++ b/app/code/Magento/Quote/Setup/UpgradeSchema.php @@ -14,6 +14,11 @@ */ class UpgradeSchema implements UpgradeSchemaInterface { + /** + * @var string + */ + private static $connectionName = 'checkout'; + /** * {@inheritdoc} */ @@ -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', [ diff --git a/app/code/Magento/Sales/Setup/SalesSetup.php b/app/code/Magento/Sales/Setup/SalesSetup.php index 7a349bac35c76..3a2c999678a03 100644 --- a/app/code/Magento/Sales/Setup/SalesSetup.php +++ b/app/code/Magento/Sales/Setup/SalesSetup.php @@ -15,6 +15,7 @@ /** * Setup Model of Sales Module * @codeCoverageIgnore + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class SalesSetup extends \Magento\Eav\Setup\EavSetup { @@ -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 @@ -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) + ); } /** @@ -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 ); @@ -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 ); diff --git a/app/code/Magento/Sales/Setup/UpgradeSchema.php b/app/code/Magento/Sales/Setup/UpgradeSchema.php index 34e7c29b6c6a0..b977cb28596ea 100644 --- a/app/code/Magento/Sales/Setup/UpgradeSchema.php +++ b/app/code/Magento/Sales/Setup/UpgradeSchema.php @@ -5,11 +5,9 @@ */ 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 @@ -17,9 +15,9 @@ class UpgradeSchema implements UpgradeSchemaInterface { /** - * @var AdapterInterface + * @var string */ - private $salesConnection; + private static $connectionName = 'sales'; /** * {@inheritdoc} @@ -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 ) ); @@ -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, @@ -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; - } } diff --git a/app/code/Magento/SalesSequence/Setup/InstallSchema.php b/app/code/Magento/SalesSequence/Setup/InstallSchema.php index 023094527c59d..5da1dc1e6bf4b 100644 --- a/app/code/Magento/SalesSequence/Setup/InstallSchema.php +++ b/app/code/Magento/SalesSequence/Setup/InstallSchema.php @@ -14,6 +14,11 @@ */ class InstallSchema implements InstallSchemaInterface { + /** + * @var string + */ + private static $connectionName = 'sales'; + /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) @@ -25,8 +30,8 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con /** * Create table 'sales_sequence_profile' */ - $table = $installer->getConnection()->newTable( - $installer->getTable('sales_sequence_profile') + $table = $installer->getConnection(self::$connectionName)->newTable( + $installer->getTable('sales_sequence_profile', self::$connectionName) )->addColumn( 'profile_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, @@ -84,24 +89,32 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con $installer->getIdxName( 'sales_sequence_profile', ['meta_id', 'prefix', 'suffix'], - \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE + \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE, + '', + self::$connectionName ), ['meta_id', 'prefix', 'suffix'], ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE] )->addForeignKey( - $installer->getFkName('sales_sequence_profile', 'meta_id', 'sales_sequence_meta', 'meta_id'), + $installer->getFkName( + 'sales_sequence_profile', + 'meta_id', + 'sales_sequence_meta', + 'meta_id', + self::$connectionName + ), 'meta_id', - $installer->getTable('sales_sequence_meta'), + $installer->getTable('sales_sequence_meta', self::$connectionName), 'meta_id', \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE ); - $installer->getConnection()->createTable($table); + $installer->getConnection(self::$connectionName)->createTable($table); /** * Create table 'sales_sequence_meta' */ - $table = $installer->getConnection()->newTable( - $installer->getTable('sales_sequence_meta') + $table = $installer->getConnection(self::$connectionName)->newTable( + $installer->getTable('sales_sequence_meta', self::$connectionName) )->addColumn( 'meta_id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, @@ -130,12 +143,14 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con $installer->getIdxName( 'sales_sequence_meta', ['entity_type', 'store_id'], - \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE + \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE, + '', + self::$connectionName ), ['entity_type', 'store_id'], ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE] ); - $installer->getConnection()->createTable($table); + $installer->getConnection(self::$connectionName)->createTable($table); $installer->endSetup(); } } diff --git a/lib/internal/Magento/Framework/Module/Setup.php b/lib/internal/Magento/Framework/Module/Setup.php index 0b0bbb20a33d5..7bd12bc89f1e9 100644 --- a/lib/internal/Magento/Framework/Module/Setup.php +++ b/lib/internal/Magento/Framework/Module/Setup.php @@ -9,6 +9,7 @@ use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\SetupInterface; +use Magento\Framework\App\ResourceConnection; class Setup implements SetupInterface { @@ -57,9 +58,27 @@ public function __construct( /** * Get connection object * + * @param string|null $connectionName * @return \Magento\Framework\DB\Adapter\AdapterInterface */ - public function getConnection() + public function getConnection($connectionName = null) + { + if ($connectionName !== null) { + try { + return $this->resourceModel->getConnectionByName($connectionName); + } catch (\DomainException $exception) { + //Fallback to default connection + } + } + return $this->getDefaultConnection(); + } + + /** + * Returns default setup connection instance + * + * @return \Magento\Framework\DB\Adapter\AdapterInterface + */ + private function getDefaultConnection() { if (null === $this->connection) { $this->connection = $this->resourceModel->getConnection($this->connectionName); @@ -95,13 +114,14 @@ public function getTablePlaceholder($tableName) * Get table name (validated by db adapter) by table placeholder * * @param string|array $tableName + * @param string $connectionName * @return string */ - public function getTable($tableName) + public function getTable($tableName, $connectionName = ResourceConnection::DEFAULT_CONNECTION) { $cacheKey = $this->_getTableCacheName($tableName); if (!isset($this->tables[$cacheKey])) { - $this->tables[$cacheKey] = $this->resourceModel->getTableName($tableName); + $this->tables[$cacheKey] = $this->resourceModel->getTableName($tableName, $connectionName); } return $this->tables[$cacheKey]; } @@ -124,12 +144,13 @@ private function _getTableCacheName($tableName) * Check is table exists * * @param string $table + * @param string $connectionName * @return bool */ - public function tableExists($table) + public function tableExists($table, $connectionName = ResourceConnection::DEFAULT_CONNECTION) { - $table = $this->getTable($table); - return $this->getConnection()->isTableExists($table); + $table = $this->getTable($table, $connectionName); + return $this->getConnection($connectionName)->isTableExists($table); } /** diff --git a/lib/internal/Magento/Framework/Module/Test/Unit/SetupTest.php b/lib/internal/Magento/Framework/Module/Test/Unit/SetupTest.php index 63b5d54e2e7d4..984c9a073fb39 100644 --- a/lib/internal/Magento/Framework/Module/Test/Unit/SetupTest.php +++ b/lib/internal/Magento/Framework/Module/Test/Unit/SetupTest.php @@ -34,7 +34,11 @@ protected function setUp() $this->resourceModel->expects($this->any()) ->method('getConnection') ->with(self::CONNECTION_NAME) - ->will($this->returnValue($this->connection)); + ->willReturn($this->connection); + $this->resourceModel->expects($this->any()) + ->method('getConnectionByName') + ->with(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION) + ->willReturn($this->connection); $this->object = new Setup($this->resourceModel, self::CONNECTION_NAME); } diff --git a/setup/src/Magento/Setup/Module/Setup.php b/setup/src/Magento/Setup/Module/Setup.php index 4451267a0e9aa..39fa5cbda13ad 100644 --- a/setup/src/Magento/Setup/Module/Setup.php +++ b/setup/src/Magento/Setup/Module/Setup.php @@ -6,6 +6,7 @@ namespace Magento\Setup\Module; use Magento\Framework\Setup\SchemaSetupInterface; +use Magento\Framework\App\ResourceConnection; class Setup extends \Magento\Framework\Module\Setup implements SchemaSetupInterface { @@ -15,11 +16,16 @@ class Setup extends \Magento\Framework\Module\Setup implements SchemaSetupInterf * @param string $tableName * @param array|string $fields * @param string $indexType + * @param string $connectionName * @return string */ - public function getIdxName($tableName, $fields, $indexType = '') - { - return $this->getConnection()->getIndexName($tableName, $fields, $indexType); + public function getIdxName( + $tableName, + $fields, + $indexType = '', + $connectionName = ResourceConnection::DEFAULT_CONNECTION + ) { + return $this->getConnection($connectionName)->getIndexName($tableName, $fields, $indexType); } /** @@ -29,10 +35,21 @@ public function getIdxName($tableName, $fields, $indexType = '') * @param string $priColumnName the target table column name * @param string $refTableName the reference table name * @param string $refColumnName the reference table column name + * @param string $connectionName * @return string */ - public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName) - { - return $this->getConnection()->getForeignKeyName($priTableName, $priColumnName, $refTableName, $refColumnName); + public function getFkName( + $priTableName, + $priColumnName, + $refTableName, + $refColumnName, + $connectionName = ResourceConnection::DEFAULT_CONNECTION + ) { + return $this->getConnection($connectionName)->getForeignKeyName( + $priTableName, + $priColumnName, + $refTableName, + $refColumnName + ); } } diff --git a/setup/src/Magento/Setup/Test/Unit/Module/SetupTest.php b/setup/src/Magento/Setup/Test/Unit/Module/SetupTest.php index 231adb9ab695b..9a75c0a3d4bf3 100644 --- a/setup/src/Magento/Setup/Test/Unit/Module/SetupTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Module/SetupTest.php @@ -30,6 +30,10 @@ protected function setUp() ->method('getConnection') ->with(self::CONNECTION_NAME) ->will($this->returnValue($this->connection)); + $resourceModel->expects($this->any()) + ->method('getConnectionByName') + ->with(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION) + ->willReturn($this->connection); $this->setup = new Setup($resourceModel, self::CONNECTION_NAME); }