diff --git a/application/helpers/update/updatedb_helper.php b/application/helpers/update/updatedb_helper.php index 758a08f07a1..23f54d743c6 100644 --- a/application/helpers/update/updatedb_helper.php +++ b/application/helpers/update/updatedb_helper.php @@ -30,6 +30,7 @@ function db_upgrade_all($iOldDBVersion) { echo str_pad(gT('The LimeSurvey database is being upgraded').' ('.date('Y-m-d H:i:s').')',14096).".

". gT('Please be patient...')."

\n"; $oDB = Yii::app()->getDb(); + Yii::app()->setConfig('Updating',true); $oDB->schemaCachingDuration=0; // Deactivate schema caching $oTransaction = $oDB->beginTransaction(); try @@ -1147,7 +1148,7 @@ function db_upgrade_all($iOldDBVersion) { dropColumn('{{sessions}}','data'); } catch (Exception $e) { - + } switch (Yii::app()->db->driverName){ case 'mysql': @@ -1299,24 +1300,24 @@ function db_upgrade_all($iOldDBVersion) { } } $oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>180),"stg_name='DBVersion'"); - + } if ($iOldDBVersion < 181) { upgradeTokenTables181(); upgradeSurveyTables181(); $oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>181),"stg_name='DBVersion'"); - } + } if ($iOldDBVersion < 183) { upgradeSurveyTables183(); $oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>183),"stg_name='DBVersion'"); - } + } if ($iOldDBVersion < 184) { fixKCFinder184(); $oDB->createCommand()->update('{{settings_global}}',array('stg_value'=>184),"stg_name='DBVersion'"); - } + } $oTransaction->commit(); // Activate schema caching $oDB->schemaCachingDuration=3600; @@ -1327,6 +1328,7 @@ function db_upgrade_all($iOldDBVersion) { } catch(Exception $e) { + Yii::app()->setConfig('Updating',false); $oTransaction->rollback(); // Activate schema caching $oDB->schemaCachingDuration=3600; @@ -1338,6 +1340,7 @@ function db_upgrade_all($iOldDBVersion) { return false; } fixLanguageConsistencyAllSurveys(); + Yii::app()->setConfig('Updating',false); echo '

'.sprintf(gT('Database update finished (%s)'),date('Y-m-d H:i:s')).'

'; return true; } @@ -1346,7 +1349,7 @@ function db_upgrade_all($iOldDBVersion) { function upgradeSurveyTables183() { $oSchema = Yii::app()->db->schema; - $aTables = dbGetTablesLike("survey\_%"); + $aTables = dbGetTablesLike("survey\_%"); if (!empty($aTables)) { foreach ( $aTables as $sTableName ) @@ -1354,8 +1357,8 @@ function upgradeSurveyTables183() $oTableSchema=$oSchema->getTable($sTableName); if (empty($oTableSchema->primaryKey)) { - addPrimaryKey(substr($sTableName,strlen(Yii::app()->getDb()->tablePrefix)), 'id'); - } + addPrimaryKey(substr($sTableName,strlen(Yii::app()->getDb()->tablePrefix)), 'id'); + } } } } @@ -1366,10 +1369,10 @@ function fixKCFinder184() $sThirdPartyDir=Yii::app()->getConfig('homedir').DIRECTORY_SEPARATOR.'third_party'.DIRECTORY_SEPARATOR; rmdirr($sThirdPartyDir.'ckeditor/plugins/toolbar'); rmdirr($sThirdPartyDir.'ckeditor/plugins/toolbar/ls-office2003'); - array_map('unlink', glob($sThirdPartyDir.'kcfinder/cache/*.js')); - array_map('unlink', glob($sThirdPartyDir.'kcfinder/cache/*.css')); - rmdirr($sThirdPartyDir.'kcfinder/upload/files'); - rmdirr($sThirdPartyDir.'kcfinder/upload/.thumbs'); + array_map('unlink', glob($sThirdPartyDir.'kcfinder/cache/*.js')); + array_map('unlink', glob($sThirdPartyDir.'kcfinder/cache/*.css')); + rmdirr($sThirdPartyDir.'kcfinder/upload/files'); + rmdirr($sThirdPartyDir.'kcfinder/upload/.thumbs'); } @@ -1379,7 +1382,7 @@ function upgradeSurveyTables181() $oSchema = Yii::app()->db->schema; if(Yii::app()->db->driverName!='pgsql') { - $aTables = dbGetTablesLike("survey\_%"); + $aTables = dbGetTablesLike("survey\_%"); if ($aTables) { foreach ( $aTables as $sTableName ) @@ -1397,8 +1400,8 @@ function upgradeSurveyTables181() case 'mysqli': alterColumn($sTableName, 'token', "string(35) COLLATE 'utf8_bin'"); break; - default: die('Unknown database driver'); - } + default: die('Unknown database driver'); + } } } } @@ -1426,8 +1429,8 @@ function upgradeTokenTables181() case 'mysqli': alterColumn($sTableName, 'token', "string(35) COLLATE 'utf8_bin'"); break; - default: die('Unknown database driver'); - } + default: die('Unknown database driver'); + } } } } @@ -1446,7 +1449,7 @@ function upgradeTokenTables179() break; default: $sSubstringCommand='substr'; - } + } $surveyidresult = dbGetTablesLike("tokens%"); if ($surveyidresult) { @@ -2409,16 +2412,16 @@ function dropUniqueKeyMSSQL($sFieldName, $sTableName) function dropSecondaryKeyMSSQL($sFieldName, $sTableName) { $oDB = Yii::app()->getDb(); - $sQuery="select + $sQuery="select i.name as IndexName - from sys.indexes i + from sys.indexes i join sys.objects o on i.object_id = o.object_id - join sys.index_columns ic on ic.object_id = i.object_id + join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id - join sys.columns co on co.object_id = i.object_id + join sys.columns co on co.object_id = i.object_id and co.column_id = ic.column_id - where i.[type] = 2 - and i.is_unique = 0 + where i.[type] = 2 + and i.is_unique = 0 and i.is_primary_key = 0 and o.[type] = 'U' and ic.is_included_column = 0 diff --git a/application/models/LSActiveRecord.php b/application/models/LSActiveRecord.php index 5b30c280572..447100aa9a2 100644 --- a/application/models/LSActiveRecord.php +++ b/application/models/LSActiveRecord.php @@ -14,10 +14,10 @@ */ class LSActiveRecord extends CActiveRecord { - + /** - * Lists the behaviors of this model - * + * Lists the behaviors of this model + * * Below is a list of all behaviors we register: * @see CTimestampBehavior * @see PluginEventBehavior @@ -27,7 +27,7 @@ public function behaviors(){ $aBehaviors=array(); $sCreateFieldName=($this->hasAttribute('created')?'created':null); $sUpdateFieldName=($this->hasAttribute('modified')?'modified':null); - $sDriverName = Yii::app()->db->getDriverName(); + $sDriverName = Yii::app()->db->getDriverName(); if ($sDriverName=='sqlsrv' || $sDriverName=='dblib') { $sTimestampExpression=new CDbExpression('GETDATE()'); @@ -42,8 +42,8 @@ public function behaviors(){ 'updateAttribute' => $sUpdateFieldName, 'timestampExpression' => $sTimestampExpression ); - // Table plugins might not exist during a database upgrade so in that case disconnect events - if (Yii::app()->db->schema->getTable('{{plugins}}') !== null) + // Some tables might not exist/not be up to date during a database upgrade so in that case disconnect plugin events + if (!Yii::app()->getConfig('Updating')) { $aBehaviors['PluginEventBehavior']= array( 'class' => 'application.models.behaviors.PluginEventBehavior' @@ -51,7 +51,7 @@ public function behaviors(){ } return $aBehaviors; } - + /** * Modified version that default to do the same as the original, but allows via a * third parameter to retrieve the result as array instead of active records. This @@ -100,21 +100,21 @@ public function findAllAsArray($condition = '', $params = array()) $criteria = $this->getCommandBuilder()->createCriteria($condition, $params); return $this->query($criteria, true, false); //Notice the third parameter 'false' } - - + + /** * Return the max value for a field - * - * This is a convenience method, that uses the primary key of the model to + * + * This is a convenience method, that uses the primary key of the model to * retrieve the highest value. - * + * * @param string $field The field that contains the Id, when null primary key is used if it is a single field - * @param boolean $forceRefresh Don't use value from static cache but always requery the database + * @param boolean $forceRefresh Don't use value from static cache but always requery the database * @return false|int */ public function getMaxId($field = null, $forceRefresh = false) { static $maxIds = array(); - + if (is_null($field)) { $primaryKey = $this->getMetaData()->tableSchema->primaryKey; if (is_string($primaryKey)) { @@ -124,28 +124,28 @@ public function getMaxId($field = null, $forceRefresh = false) { throw new Exception(sprintf('Table %s has a composite primary key, please explicitly state what field you need the max value for.', $this->tableName())); } } - + if ($forceRefresh || !array_key_exists($field, $maxIds)) { $maxId = $this->dbConnection->createCommand() ->select('MAX(' . $this->dbConnection->quoteColumnName($field) . ')') ->from($this->tableName()) ->queryScalar(); - + // Save so we can reuse in the same request $maxIds[$field] = $maxId; } - + return $maxIds[$field]; } /** * @todo This should also be moved to the behavior at some point. * This method overrides the parent in order to raise PluginEvents for Bulk delete operations. - * + * * Filter Criteria are wrapped into a CDBCriteria instance so we have a single instance responsible for holding the filter criteria - * to be passed to the PluginEvent, + * to be passed to the PluginEvent, * this also enables us to pass the fully configured CDBCriteria instead of the original Parameters. - * + * * See {@link find()} for detailed explanation about $condition and $params. * @param array $attributes list of attribute values (indexed by attribute names) that the active records should match. * An attribute value can be an array which will be used to generate an IN condition.