Skip to content

Commit

Permalink
Fixed issue #10036: Problems when trying to update from 2.0 and using…
Browse files Browse the repository at this point in the history
… Postgres
  • Loading branch information
c-schmitz committed Dec 4, 2015
1 parent f4881a0 commit 313548f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
51 changes: 27 additions & 24 deletions application/helpers/update/updatedb_helper.php
Expand Up @@ -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).".<br /><br />". gT('Please be patient...')."<br /><br />\n";

$oDB = Yii::app()->getDb();
Yii::app()->setConfig('Updating',true);
$oDB->schemaCachingDuration=0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
Expand Down Expand Up @@ -1147,7 +1148,7 @@ function db_upgrade_all($iOldDBVersion) {
dropColumn('{{sessions}}','data');
}
catch (Exception $e) {

}
switch (Yii::app()->db->driverName){
case 'mysql':
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -1338,6 +1340,7 @@ function db_upgrade_all($iOldDBVersion) {
return false;
}
fixLanguageConsistencyAllSurveys();
Yii::app()->setConfig('Updating',false);
echo '<br /><br />'.sprintf(gT('Database update finished (%s)'),date('Y-m-d H:i:s')).'<br /><br />';
return true;
}
Expand All @@ -1346,16 +1349,16 @@ 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 )
{
$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');
}
}
}
}
Expand All @@ -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');
}


Expand All @@ -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 )
Expand All @@ -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');
}
}
}
}
Expand Down Expand Up @@ -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');
}
}
}
}
Expand All @@ -1446,7 +1449,7 @@ function upgradeTokenTables179()
break;
default:
$sSubstringCommand='substr';
}
}
$surveyidresult = dbGetTablesLike("tokens%");
if ($surveyidresult)
{
Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions application/models/LSActiveRecord.php
Expand Up @@ -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
Expand All @@ -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()');
Expand All @@ -42,16 +42,16 @@ 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'
);
}
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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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.
Expand Down

1 comment on commit 313548f

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then no plugin during update. Maybe a "afterUpdate" plugin event ? (Not needed/used actually, just thinking).

Please sign in to comment.