Skip to content

Commit

Permalink
Added compatibility functions for DDL table and createTable method not
Browse files Browse the repository at this point in the history
supporting auto_increment fields on 1.4-1.5 versions
Reworked getMagentoEdition to make it possible to use after
Mage::app()->baseInit() not only after Mage::app()->_initModules()
  • Loading branch information
anjey-lobas committed Jan 21, 2014
1 parent c107569 commit 92488f0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 21 deletions.
44 changes: 35 additions & 9 deletions Goodahead_Core/app/code/community/Goodahead/Core/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This extension is supplied with every Goodahead extension and provide common
* features, used by Goodahead extensions.
*
* Copyright (C) 2013 Goodahead Ltd. (http://www.goodahead.com)
* Copyright (C) 2014 Goodahead Ltd. (http://www.goodahead.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -23,7 +23,7 @@
*
* @category Goodahead
* @package Goodahead_Core
* @copyright Copyright (c) 2013 Goodahead Ltd. (http://www.goodahead.com)
* @copyright Copyright (c) 2014 Goodahead Ltd. (http://www.goodahead.com)
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html
*/

Expand All @@ -50,15 +50,15 @@ class Goodahead_Core_Helper_Data extends Mage_Core_Helper_Abstract
* @var string
*/
static protected $_magentoEdition;
static protected $_magentoEditionPredispatch;

/**
* Current Magento edition
* Current Magento version
*
* @var string
*/
static protected $_magentoCoreVersion;


/**
* Return current Magento edition
*
Expand All @@ -70,12 +70,38 @@ public static function getMagentoEdition()
if (method_exists('Mage', 'getEdition')) {
self::$_magentoEdition = Mage::getEdition();
} else {
if (@Mage::getConfig()->getModuleConfig('Enterprise_Checkout')) {
self::$_magentoEdition = self::MAGENTO_EDITION_EE;
} elseif (@Mage::getConfig()->getModuleConfig('Enterprise_Enterprise')) {
self::$_magentoEdition = self::MAGENTO_EDITION_PE;
if ($modules = Mage::getConfig()->getModuleConfig() !== false) {
if (Mage::getConfig()->getModuleConfig('Enterprise_Checkout')) {
self::$_magentoEdition = self::MAGENTO_EDITION_EE;
} elseif (Mage::getConfig()->getModuleConfig('Enterprise_Enterprise')) {
self::$_magentoEdition = self::MAGENTO_EDITION_PE;
} else {
self::$_magentoEdition = self::MAGENTO_EDITION_CE;
}
} else {
self::$_magentoEdition = self::MAGENTO_EDITION_CE;
/**
* Possibly on predispatch stage. Separate cache for edition
* result check. Verify actual class existence by checking
* class constants (not reliable if someone has copied-over
* Enterprise modules and installed them on Community
* Magento, but who cares for people violating licenses?)
*/
if (!isset(self::$_magentoEditionPredispatch)) {
try {
if (@class_exists('Enterprise_Checkout_Helper_Data')) {
self::$_magentoEditionPredispatch = self::MAGENTO_EDITION_EE;
return self::$_magentoEditionPredispatch;
}
} catch (Exception $e) {}
try {
if (@class_exists('Enterprise_Enterprise_Model_Observer')) {
self::$_magentoEditionPredispatch = self::MAGENTO_EDITION_PE;
return self::$_magentoEditionPredispatch;
}
} catch (Exception $e) {}
self::$_magentoEditionPredispatch = self::MAGENTO_EDITION_CE;
}
return self::$_magentoEditionPredispatch;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,11 @@ public function getFkName($priTableName, $priColumnName, $refTableName, $refColu
}
}

public function createTable(Varien_Db_Ddl_Table $table)
{
Goodahead_Core_Model_Resource_Setup_Compatibility::createTable(
$this, $table
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,53 @@ public static function getTimestampColumnDefaultValue($value)
}
}

public static function createTable(Mage_Core_Model_Resource_Setup $installer, Varien_Db_Ddl_Table $table)
{
$connection = $installer->getConnection();
if (method_exists($table, 'setComment')) {
if (($comment = $table->getOption('comment')) !== null) {
$table->setComment($comment);
}
}
$connection->createTable($table);

/**
* Old PDO Adapter Mysql with no autoincrement support on createTable
* we need to add autoincrement for this column
*/
if (
$connection instanceof Varien_Db_Adapter_Pdo_Mysql
&& !defined('Varien_Db_Adapter_Pdo_Mysql::LENGTH_TABLE_NAME')
) {
foreach ($table->getColumns() as $column) {
if (!empty($column['IDENTITY']) || !empty($column['AUTO_INCREMENT'])) {
$createTable = $connection->getCreateTable($table->getName());
$columnNamePattern = preg_quote($connection->quoteIdentifier($column['COLUMN_NAME']),'/');
/**
* This is not advanced pattern for mysql column definition
* syntax.
*
* But we don't need to worry about anything else except
* default definition to determine AUTO_INCREMENT insertion
* position, because in old version of PDO Adapter Mysql any
* other column definition parts are not supported
*
* So this pattern should be just fine to correct issues
* with old version of PDO Adapter Mysql
*/
$pattern = "/.*?^\\s+{$columnNamePattern}(.+?)( default.+?)?,?$.*/msi";

$columnDefinition = preg_replace($pattern,'$1 auto_increment$2', $createTable, 1);
$connection->modifyColumn($table->getName(), $column['COLUMN_NAME'], $columnDefinition);

/**
* Mysql support only one AUTO_INCREMENT column
*/
break;
}
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
$installer->getIdxName(
'goodahead_core/cms_update',
array('cms_item_id')),
array('cms_item_id'))
->setComment('CMS Block/Page update resource table');
array('cms_item_id'));

$installer->getConnection()->createTable($table);
$table->setOption('comment', 'CMS Block/Page update resource table');
$installer->createTable($table);
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,5 @@
'goodahead_core/cms_update',
array('cms_item_id')),
array('cms_item_id'));

$comment = 'CMS Block/Page update resource table';
if (method_exists($table, 'setComment')) {
$table->setComment($comment);
} else {
$table->setOption('comment', $comment);
}

$installer->getConnection()->createTable($table);
$table->setOption('comment', 'CMS Block/Page update resource table');
$installer->createTable($table);

0 comments on commit 92488f0

Please sign in to comment.