View Issue Details

This bug affects 1 person(s).
 22
IDProjectCategoryView StatusLast Update
09282Bug reportsOtherpublic2014-11-23 23:42
Reportersveinp Assigned Toc_schmitz  
PrioritynormalSeverityminor 
Status closedResolutionfixed 
Fixed in Version2.05+ 
Summary09282: Deactivating survey with tokens fails with postgres
Description

Deactivating a survey with tokens fails with postgresql in Version 2.05+ Build 140915 (internal server error). The reasons are outlined in additional information.

Additional Information

Around line 330 in serveyadmin.php:

            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();
            }

In an earlier version I have installed, this block did not execute (I think) due to a bug where the condition was the the driver name should be 'postgre'.

Anyhow, the variables $toldtable and $tnewtable are defined below this block, so when it is executed - it fails.

Further:
In:
$setsequence = "ALTER TABLE ".Yii::app()->db->quoteTableName($tnewtable)." ALTER COLUMN tid SET DEFAULT nextval('" . $tnewtable . "_tid_seq'::regclass);";

$toldtable should be used, not $tnewtable, since the table hasn't been renamed yet.

Also, at least for me, the $setidx statement will fail because there is no such index - the indexes present are:

"tokens_239431_pkey" PRIMARY KEY, btree (tid)
"idx_token_token_239431_3853" btree (token)
TagsNo tags attached.
Attached Files
surveyadmin.diff (3,616 bytes)   
--- /var/www/limesurvey/application/controllers/admin/surveyadmin.php	2014-11-14 23:46:15.486568303 +0100
+++ /tmp/limesurvey/application/controllers/admin/surveyadmin.php	2014-11-14 15:53:05.000000000 +0100
@@ -326,17 +326,26 @@
             //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);
 
@@ -363,6 +372,23 @@
             $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);
 
             $insertdata = array('active' => 'N');
surveyadmin.diff (3,616 bytes)   
Bug heat22
Complete LimeSurvey version number (& build)140915
I will donate to the project if issue is resolvedNo
Browser
Database type & versionPostgres 9.3
Server OS (if known)Linux
Webserver software & version (if known)Apache
PHP Version5.5.9

Relationships

has duplicate 09360 closedc_schmitz Crash when disabling a survey 
has duplicate 09306 closedc_schmitz Table "_tid_seq" didn't exist 

Users monitoring this issue

There are no users monitoring this issue.

Activities

sveinp

sveinp

2014-10-07 17:12

reporter   ~30748

Probably, the easiest solution is to simply remove the whole block

random1

random1

2014-11-16 12:59

reporter   ~30991

I had the same problem and fixed it (see surveyadmin.diff)

c_schmitz

c_schmitz

2014-11-16 18:37

administrator   ~30996

Fix committed to master branch: http://bugs.limesurvey.org/plugin.php?page=Source/view&id=14627

c_schmitz

c_schmitz

2014-11-16 18:37

administrator   ~30997

Great patch, random1, much appreciated.

c_schmitz

c_schmitz

2014-11-23 23:42

administrator   ~31072

Version 2.05 Build 141123 released.

Related Changesets

LimeSurvey: master 8d481e84

2014-11-16 17:37:03

c_schmitz

Details Diff
Fixed issue 09282: Deactivating survey with tokens fails with postgres - patch provided by random1 Affected Issues
09282
mod - application/controllers/admin/surveyadmin.php Diff File

Issue History

Date Modified Username Field Change
2014-10-07 15:00 sveinp New Issue
2014-10-07 17:12 sveinp Note Added: 30748
2014-11-16 12:58 random1 File Added: surveyadmin.diff
2014-11-16 12:59 random1 Note Added: 30991
2014-11-16 13:19 DenisChenu Relationship added has duplicate 09360
2014-11-16 18:30 c_schmitz Relationship added has duplicate 09306
2014-11-16 18:36 c_schmitz Assigned To => c_schmitz
2014-11-16 18:36 c_schmitz Status new => assigned
2014-11-16 18:37 c_schmitz Changeset attached => LimeSurvey master 8d481e84
2014-11-16 18:37 c_schmitz Note Added: 30996
2014-11-16 18:37 c_schmitz Resolution open => fixed
2014-11-16 18:37 c_schmitz Note Added: 30997
2014-11-16 18:37 c_schmitz Status assigned => resolved
2014-11-16 18:37 c_schmitz Fixed in Version => 2.05+
2014-11-23 23:42 c_schmitz Note Added: 31072
2014-11-23 23:42 c_schmitz Status resolved => closed
2021-08-02 22:25 guest Bug heat 20 => 22