Skip to content

Commit

Permalink
Fixed issue #9282: Deactivating survey with tokens fails with postgre…
Browse files Browse the repository at this point in the history
…s - patch provided by random1
  • Loading branch information
c-schmitz committed Nov 16, 2014
1 parent 8771146 commit 8d481e8
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -326,18 +326,19 @@ public function deactivate($iSurveyID = null)
//See if there is a tokens table for this survey
if (tableExists("{{tokens_{$iSurveyID}}}"))
{
$toldtable = Yii::app()->db->tablePrefix."tokens_{$iSurveyID}";
$tnewtable = Yii::app()->db->tablePrefix."old_tokens_{$iSurveyID}_{$date}";
if (Yii::app()->db->getDriverName() == 'pgsql')
{
$deactivateresult = Yii::app()->db->createCommand()->renameTable($toldtable . '_tid_seq', $tnewtable . '_tid_seq');
$setsequence = "ALTER TABLE ".Yii::app()->db->quoteTableName($tnewtable)." ALTER COLUMN tid SET DEFAULT nextval('{{{$tnewtable}}}_tid_seq'::regclass);";
$deactivateresult = Yii::app()->db->createCommand($setsequence)->query();
$setidx = "ALTER INDEX {{{$toldtable}}}_idx RENAME TO {{{$tnewtable}}}_idx;";
$deactivateresult = Yii::app()->db->createCommand($setidx)->query();
$tidDefault = Yii::app()->db->createCommand("SELECT pg_attrdef.adsrc FROM pg_attribute JOIN pg_class ON (pg_attribute.attrelid=pg_class.oid) JOIN pg_attrdef ON(pg_attribute.attrelid=pg_attrdef.adrelid AND pg_attribute.attnum=pg_attrdef.adnum) WHERE pg_class.relname='$toldtable' and pg_attribute.attname='tid'")->queryScalar();
if(preg_match("/nextval\('(tokens_\d+_tid_seq\d*)'::regclass\)/", $tidDefault, $matches)){
$oldSeq = $matches[1];
$deactivateresult = Yii::app()->db->createCommand()->renameTable($oldSeq, $tnewtable . '_tid_seq');
$setsequence = "ALTER TABLE ".Yii::app()->db->quoteTableName($toldtable)." ALTER COLUMN tid SET DEFAULT nextval('{$tnewtable}_tid_seq'::regclass);";
$deactivateresult = Yii::app()->db->createCommand($setsequence)->query();
}
}

$toldtable = "{{tokens_{$iSurveyID}}}";
$tnewtable = "{{old_tokens_{$iSurveyID}_{$date}}}";

$tdeactivateresult = Yii::app()->db->createCommand()->renameTable($toldtable, $tnewtable);

$aData['tnewtable'] = $tnewtable;
Expand All @@ -362,6 +363,16 @@ public function deactivate($iSurveyID = null)
$survey = Survey::model()->findByAttributes(array('sid' => $iSurveyID));
$survey->autonumber_start = $new_autonumber_start;
$survey->save();
if (Yii::app()->db->getDriverName() == 'pgsql')
{
$idDefault = Yii::app()->db->createCommand("SELECT pg_attrdef.adsrc FROM pg_attribute JOIN pg_class ON (pg_attribute.attrelid=pg_class.oid) JOIN pg_attrdef ON(pg_attribute.attrelid=pg_attrdef.adrelid AND pg_attribute.attnum=pg_attrdef.adnum) WHERE pg_class.relname='$sOldSurveyTableName' and pg_attribute.attname='id'")->queryScalar();
if(preg_match("/nextval\('(survey_\d+_id_seq\d*)'::regclass\)/", $idDefault, $matches)){
$oldSeq = $matches[1];
$deactivateresult = Yii::app()->db->createCommand()->renameTable($oldSeq, $sNewSurveyTableName . '_id_seq');
$setsequence = "ALTER TABLE ".Yii::app()->db->quoteTableName($sOldSurveyTableName)." ALTER COLUMN id SET DEFAULT nextval('{{{$sNewSurveyTableName}}}_id_seq'::regclass);";
$deactivateresult = Yii::app()->db->createCommand($setsequence)->query();
}
}

$deactivateresult = Yii::app()->db->createCommand()->renameTable($sOldSurveyTableName, $sNewSurveyTableName);

Expand Down

0 comments on commit 8d481e8

Please sign in to comment.