Permalink
Browse files

fix of postgres db upgrade. - Penny please test! :-)

  • Loading branch information...
danmarsden
danmarsden committed Apr 6, 2005
1 parent 111bb08 commit 6308ca52a5128451fc38fb2f8fedf3d8768b1f30
Showing with 39 additions and 33 deletions.
  1. +39 −33 mod/choice/db/postgres7.php
@@ -41,40 +41,46 @@ function choice_upgrade($oldversion) {
modify_database('','CREATE INDEX prefix_choice_answers_choice_idx ON prefix_choice_answers (choice);');
modify_database('','CREATE INDEX prefix_choice_answers_userid_idx ON prefix_choice_answers (userid);');
}
- // if ($oldversion < 2005033000){
- // execute_sql("ALTER TABLE {$CFG->prefix}choice_answers RENAME TO {$CFG->prefix}choice_responses;");
- // execute_sql("CREATE TABLE {$CFG->prefix}choice_answers (id SERIAL PRIMARY KEY, choice integer NOT NULL default '0', answer TEXT, timemodified integer NOT NULL default '0');");
- // execute_sql("CREATE INDEX {$CFG->prefix}choice_answers_choice_idx ON {$CFG->prefix}choice_answers (choice);");
- // table_column('choice','','display','integer','10','unsigned','0','not null');
- // table_column('choice_responses','answer','answerid','integer','10','unsigned','0','not null');
- //
- // //move old answers into new answer table.
- // $records = get_records('choice');
- // if (!empty($records)) {
- // foreach ($records as $thischoice) {
- // for ($i = 1; $i < 5; $i++) {
- // $instance = new stdClass;
- // $instance->choice = $thischoice->id;
- // $instance->answerid = $thischoice->{'answer'.$i};
- // $instance->timemodified = $thischoice->timemodified;
- // $result = insert_record('choice_answers', $instance);
- // //now fix all responses to the answers.
- // execute_sql("UPDATE {$CFG->prefix}choice_responses SET answerid={$result} WHERE choice={$thischoice->id} AND answerid={$i}");
- // }
- // }
- // }
-
+ if ($oldversion < 2005033000){
+ if (execute_sql("CREATE TABLE {$CFG->prefix}choice_options (id SERIAL PRIMARY KEY, choiceid integer NOT NULL default '0', `text` TEXT, timemodified integer NOT NULL default '0');") ) {
+ execute_sql("CREATE INDEX {$CFG->prefix}choice_options_choice_idx ON {$CFG->prefix}choice_options (choiceid);");
+
+ table_column('choice_answers', 'choice', 'choiceid', 'integer', '10', 'unsigned', 0, 'not null');
+ table_column('choice_answers', 'answer', 'optionid', 'integer', '10', 'unsigned', 0, 'not null');
+ table_column('choice', '', 'display', 'integer', '4', 'unsigned', 0, 'not null', 'release');
+
+ // move old answers from choice to choice_options
+ if ($choices = get_records('choice')) {
+ foreach ($choices as $choice) {
+ for ($i=1; $i<=6; $i++) { // We used to have six columns
+ $option = new stdClass;
+ $option->text = $choice->{'answer'.$i};
+ if ($option->text) { /// Don't bother with blank options
+ $option->choiceid = $choice->id;
+ $option->timemodified = $choice->timemodified;
+ if ($option->id = insert_record('choice_options', $option)) {
+ /// Update all the user answers to fit the new value
+ execute_sql("UPDATE {$CFG->prefix}choice_answers
+ SET optionid={$option->id}
+ WHERE choiceid={$choice->id}
+ AND optionid={$i}");
+ }
+ }
+ }
+ }
+ }
+
// drop old fields
- // modify_database('','ALTER TABLE prefix_choice DROP answer1;');
- // modify_database('','ALTER TABLE prefix_choice DROP answer2;');
- // modify_database('','ALTER TABLE prefix_choice DROP answer3;');
- // modify_database('','ALTER TABLE prefix_choice DROP answer4;');
- // modify_database('','ALTER TABLE prefix_choice DROP answer5;');
- // modify_database('','ALTER TABLE prefix_choice DROP answer6;');
-
- // }
+ modify_database('','ALTER TABLE prefix_choice DROP answer1;');
+ modify_database('','ALTER TABLE prefix_choice DROP answer2;');
+ modify_database('','ALTER TABLE prefix_choice DROP answer3;');
+ modify_database('','ALTER TABLE prefix_choice DROP answer4;');
+ modify_database('','ALTER TABLE prefix_choice DROP answer5;');
+ modify_database('','ALTER TABLE prefix_choice DROP answer6;');
+
+ }
+ }
return true;
}
-?>
-
+?>

0 comments on commit 6308ca5

Please sign in to comment.