protected function setUp() { /** @var \Magento\Framework\Setup\ModuleDataSetupInterface $installer */ $installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('\\Magento\\Framework\\Setup\\ModuleDataSetupInterface'); $this->_connection = $installer->getConnection(); $this->_tableName = $this->_connection->getTableName('table_two_column_idx'); $this->_oneColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1']); $this->_twoColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1', 'column2']); $table = $this->_connection->newTable($this->_tableName)->addColumn('id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Id')->addColumn('column1', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER)->addColumn('column2', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER)->addIndex($this->_oneColumnIdxName, ['column1'])->addIndex($this->_twoColumnIdxName, ['column1', 'column2']); $this->_connection->createTable($table); }
/** * Return structure for flat catalog table * * @param string $tableName * @return \Magento\Framework\DB\Ddl\Table */ protected function getFlatTableStructure($tableName) { $table = $this->connection->newTable($tableName)->setComment(sprintf("Catalog Category Flat", $tableName)); //Adding columns foreach ($this->getColumns() as $fieldName => $fieldProp) { $default = $fieldProp['default']; if ($fieldProp['type'][0] == \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP && $default == 'CURRENT_TIMESTAMP') { $default = \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT; } $table->addColumn($fieldName, $fieldProp['type'][0], $fieldProp['type'][1], ['nullable' => $fieldProp['nullable'], 'unsigned' => $fieldProp['unsigned'], 'default' => $default, 'primary' => isset($fieldProp['primary']) ? $fieldProp['primary'] : false], $fieldProp['comment'] != '' ? $fieldProp['comment'] : ucwords(str_replace('_', ' ', $fieldName))); } // Adding indexes $table->addIndex($this->connection->getIndexName($tableName, ['entity_id']), ['entity_id'], ['type' => 'primary']); $table->addIndex($this->connection->getIndexName($tableName, ['store_id']), ['store_id'], ['type' => 'index']); $table->addIndex($this->connection->getIndexName($tableName, ['path']), ['path'], ['type' => 'index']); $table->addIndex($this->connection->getIndexName($tableName, ['level']), ['level'], ['type' => 'index']); return $table; }
/** * Prepare flat table for store * * @param int|string $storeId * @return void * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _createTemporaryFlatTable($storeId) { $columns = $this->_productIndexerHelper->getFlatColumns(); $indexesNeed = $this->_productIndexerHelper->getFlatIndexes(); $maxIndex = $this->_config->getValue(self::XML_NODE_MAX_INDEX_COUNT); if ($maxIndex && count($indexesNeed) > $maxIndex) { throw new \Magento\Framework\Exception\LocalizedException(__('The Flat Catalog module has a limit of %2$d filterable and/or sortable attributes.' . 'Currently there are %1$d of them.' . 'Please reduce the number of filterable/sortable attributes in order to use this module', count($indexesNeed), $maxIndex)); } $indexKeys = []; $indexProps = array_values($indexesNeed); $upperPrimaryKey = strtoupper(\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_PRIMARY); foreach ($indexProps as $i => $indexProp) { $indexName = $this->_connection->getIndexName($this->_getTemporaryTableName($this->_productIndexerHelper->getFlatTableName($storeId)), $indexProp['fields'], $indexProp['type']); $indexProp['type'] = strtoupper($indexProp['type']); if ($indexProp['type'] == $upperPrimaryKey) { $indexKey = $upperPrimaryKey; } else { $indexKey = $indexName; } $indexProps[$i] = ['KEY_NAME' => $indexName, 'COLUMNS_LIST' => $indexProp['fields'], 'INDEX_TYPE' => strtolower($indexProp['type'])]; $indexKeys[$i] = $indexKey; } $indexesNeed = array_combine($indexKeys, $indexProps); /** @var $table \Magento\Framework\DB\Ddl\Table */ $table = $this->_connection->newTable($this->_getTemporaryTableName($this->_productIndexerHelper->getFlatTableName($storeId))); foreach ($columns as $fieldName => $fieldProp) { $columnLength = isset($fieldProp['length']) ? $fieldProp['length'] : null; $columnDefinition = ['nullable' => isset($fieldProp['nullable']) ? (bool) $fieldProp['nullable'] : false, 'unsigned' => isset($fieldProp['unsigned']) ? (bool) $fieldProp['unsigned'] : false, 'default' => isset($fieldProp['default']) ? $fieldProp['default'] : false, 'primary' => false]; $columnComment = isset($fieldProp['comment']) ? $fieldProp['comment'] : $fieldName; $table->addColumn($fieldName, $fieldProp['type'], $columnLength, $columnDefinition, $columnComment); } foreach ($indexesNeed as $indexProp) { $table->addIndex($indexProp['KEY_NAME'], $indexProp['COLUMNS_LIST'], ['type' => $indexProp['INDEX_TYPE']]); } $table->setComment("Catalog Product Flat (Store {$storeId})"); $this->_connection->dropTable($this->_getTemporaryTableName($this->_productIndexerHelper->getFlatTableName($storeId))); $this->_connection->createTable($table); }
/** * Retrieve 32bit UNIQUE HASH for a Table index * * @param string $tableName * @param array|string $fields * @param string $indexType * @return string */ public function getIdxName($tableName, $fields, $indexType = '') { return $this->connection->getIndexName($tableName, $fields, $indexType); }