Skip to content

Commit

Permalink
Dev Fixed update
Browse files Browse the repository at this point in the history
  • Loading branch information
c-schmitz committed Jan 17, 2018
1 parent 8d7a034 commit 0e7b6a1
Showing 1 changed file with 116 additions and 115 deletions.
231 changes: 116 additions & 115 deletions application/helpers/update/updatedb_helper.php
Expand Up @@ -925,121 +925,6 @@ function db_upgrade_all($iOldDBVersion, $bSilent = false)
$oDB->createCommand()->update('{{settings_global}}', array('stg_value'=>341), "stg_name='DBVersion'");
$oTransaction->commit();
}
if ($iOldDBVersion < 350) {
// This update moves localization-dependant strings from question group/question/answer tables to related localization tables
$oTransaction = $oDB->beginTransaction();

// Question table
$oDB->createCommand()->createTable('{{question_l10ns}}', array(
'id' => "pk",
'qid' => "integer NOT NULL",
'question' => "text NOT NULL",
'help' => "text",
'language' => "string(20) NOT NULL"
));
$oDB->createCommand()->createIndex('{{idx1_question_l10ns}}', '{{question_l10ns}}', ['qid', 'language'], true);
$oDB->createCommand("INSERT INTO {{question_l10ns}} (qid, question, help, language) select qid, question, help, language from {{questions}}")->execute();
$dataReader = $oDB->createCommand("select q1.language,q1.qid FROM {{questions}} q1 INNER JOIN {{questions}} q2 WHERE q1.qid = q2.qid and q1.language<q2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{questions}} where qid={$row['qid']} and language='{$row['language']}'")->execute();
}
alterColumn('{{questions}}', 'qid', "int", true);
dropPrimaryKey('questions');
alterColumn('{{questions}}', 'qid', "pk", false);
$oDB->createCommand()->dropColumn('{{questions}}', 'question');
$oDB->createCommand()->dropColumn('{{questions}}', 'help');
$oDB->createCommand()->dropColumn('{{questions}}', 'language');
// Groups table
$oDB->createCommand()->createTable('{{group_l10ns}}', array(
'id' => "pk",
'gid' => "integer NOT NULL",
'group_name' => "text NOT NULL",
'description' => "text",
'language' => "string(20) NOT NULL"
));
$oDB->createCommand()->createIndex('{{idx1_group_l10ns}}', '{{group_l10ns}}', ['gid', 'language'], true);
$oDB->createCommand("INSERT INTO {{group_l10ns}} (gid, group_name, description, language) select gid, group_name, description, language from {{groups}}")->execute();
$dataReader = $oDB->createCommand("select g1.language,g1.gid FROM {{groups}} g1 INNER JOIN {{groups}} g2 WHERE g1.gid = g2.gid and g1.language<g2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{groups}} where gid={$row['gid']} and language='{$row['language']}'")->execute();
}
alterColumn('{{groups}}', 'gid', "int", true);
dropPrimaryKey('groups');
alterColumn('{{groups}}', 'gid', "pk", false);
$oDB->createCommand()->dropColumn('{{groups}}', 'group_name');
$oDB->createCommand()->dropColumn('{{groups}}', 'description');
$oDB->createCommand()->dropColumn('{{groups}}', 'language');
// Answers table
// Answers now have a proper answer ID - wohoo!
$oDB->createCommand()->createTable('{{answer_l10ns}}', array(
'id' => "pk",
'aid' => "integer NOT NULL",
'answer' => "text NOT NULL",
'language' => "string(20) NOT NULL"
));
$oDB->createCommand()->createIndex('{{idx1_answer_l10ns}}', '{{answer_l10ns}}', ['aid', 'language'], true);
dropPrimaryKey('answers');

addColumn('{{answers}}', 'aid', 'int');
$oDB->createCommand()->createIndex('answer_idx_10','{{answers}}',['qid','code','scale_id']);
$dataReader = $oDB->createCommand("select qid, code, scale_id from {{answers}} group by qid, code, scale_id")->query();
$iCounter = 1;
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("update {{answers}} set aid={$iCounter} where qid={$row['qid']} and code='{$row['code']}' and scale_id={$row['scale_id']}")->execute();
$iCounter++;
}
$oDB->createCommand("INSERT INTO {{answer_l10ns}} (aid, answer, language) select aid, answer, language from {{answers}}")->execute();
$dataReader = $oDB->createCommand("select a1.language,a1.aid FROM {{answers}} a1 INNER JOIN {{answers}} a2 WHERE a1.aid = a2.aid and a1.language<a2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{answers}} where aid={$row['aid']} and language='{$row['language']}'")->execute();
}
alterColumn('{{answers}}', 'aid', "pk", false);
$oDB->createCommand()->dropColumn('{{answers}}', 'answer');
$oDB->createCommand()->dropColumn('{{answers}}', 'language');
$oDB->createCommand()->dropindex('answer_idx_10','{{answers}}');
$oDB->createCommand()->createIndex('{{answers_idx}}', '{{answers}}', ['qid', 'code', 'scale_id'], true);

// Labels table
// label_l10ns
$oDB->createCommand()->createTable('{{label_l10ns}}', array(
'id' => "pk",
'label_id' => "integer NOT NULL",
'title' => "text",
'language' => "string(20) NOT NULL DEFAULT 'en'"
));
$oDB->createCommand()->createIndex('{{idx1_label_l10ns}}', '{{label_l10ns}}', ['label_id', 'language'], true);

alterColumn('{{labels}}', 'id', "int", true);
dropPrimaryKey('labels');
$dataReader = $oDB->createCommand("select lid,code from {{labels}} group by lid,code")->query();
$iCounter = 1;
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("update {{labels}} set id={$iCounter} where lid={$row['lid']} and code='{$row['code']}'")->execute();
$iCounter++;
}
$oDB->createCommand("INSERT INTO {{label_l10ns}} (label_id, title, language) select id, title, language from {{labels}}")->execute();
$dataReader = $oDB->createCommand("select l1.language,l1.id FROM {{labels}} l1 INNER JOIN {{labels}} l2 WHERE l1.id = l2.id and l1.language<l2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{labels}} where id={$row['id']} and language='{$row['language']}'")->execute();
}
alterColumn('{{labels}}', 'id', "pk", false);
$oDB->createCommand()->dropColumn('{{labels}}', 'title');
$oDB->createCommand()->dropColumn('{{labels}}', 'language');


// Extend language field on labelsets
alterColumn('{{labelsets}}', 'languages', "string(255)", false);

// Extend question type field length
alterColumn('{{questions}}', 'type', 'string(30)', false, 'T');

// Drop autoincrement on timings table primary key
upgradeSurveyTimings350();

$oDB->createCommand()->update('{{settings_global}}', array('stg_value'=>350), "stg_name='DBVersion'");
$oTransaction->commit();
}

/**
* Url parameter "surveyid" should be "sid" for this link.
*/
Expand Down Expand Up @@ -1226,7 +1111,123 @@ function db_upgrade_all($iOldDBVersion, $bSilent = false)
$oDB->createCommand()->update('{{settings_global}}', ['stg_value'=>345], "stg_name='DBVersion'");
$oTransaction->commit();
}



if ($iOldDBVersion < 350) {
// This update moves localization-dependant strings from question group/question/answer tables to related localization tables
$oTransaction = $oDB->beginTransaction();

// Question table
$oDB->createCommand()->createTable('{{question_l10ns}}', array(
'id' => "pk",
'qid' => "integer NOT NULL",
'question' => "text NOT NULL",
'help' => "text",
'language' => "string(20) NOT NULL"
));
$oDB->createCommand()->createIndex('{{idx1_question_l10ns}}', '{{question_l10ns}}', ['qid', 'language'], true);
$oDB->createCommand("INSERT INTO {{question_l10ns}} (qid, question, help, language) select qid, question, help, language from {{questions}}")->execute();
$dataReader = $oDB->createCommand("select q1.language,q1.qid FROM {{questions}} q1 INNER JOIN {{questions}} q2 WHERE q1.qid = q2.qid and q1.language<q2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{questions}} where qid={$row['qid']} and language='{$row['language']}'")->execute();
}
alterColumn('{{questions}}', 'qid', "int", true);
dropPrimaryKey('questions');
alterColumn('{{questions}}', 'qid', "pk", false);
$oDB->createCommand()->dropColumn('{{questions}}', 'question');
$oDB->createCommand()->dropColumn('{{questions}}', 'help');
$oDB->createCommand()->dropColumn('{{questions}}', 'language');
// Groups table
$oDB->createCommand()->createTable('{{group_l10ns}}', array(
'id' => "pk",
'gid' => "integer NOT NULL",
'group_name' => "text NOT NULL",
'description' => "text",
'language' => "string(20) NOT NULL"
));
$oDB->createCommand()->createIndex('{{idx1_group_l10ns}}', '{{group_l10ns}}', ['gid', 'language'], true);
$oDB->createCommand("INSERT INTO {{group_l10ns}} (gid, group_name, description, language) select gid, group_name, description, language from {{groups}}")->execute();
$dataReader = $oDB->createCommand("select g1.language,g1.gid FROM {{groups}} g1 INNER JOIN {{groups}} g2 WHERE g1.gid = g2.gid and g1.language<g2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{groups}} where gid={$row['gid']} and language='{$row['language']}'")->execute();
}
alterColumn('{{groups}}', 'gid', "int", true);
dropPrimaryKey('groups');
alterColumn('{{groups}}', 'gid', "pk", false);
$oDB->createCommand()->dropColumn('{{groups}}', 'group_name');
$oDB->createCommand()->dropColumn('{{groups}}', 'description');
$oDB->createCommand()->dropColumn('{{groups}}', 'language');
// Answers table
// Answers now have a proper answer ID - wohoo!
$oDB->createCommand()->createTable('{{answer_l10ns}}', array(
'id' => "pk",
'aid' => "integer NOT NULL",
'answer' => "text NOT NULL",
'language' => "string(20) NOT NULL"
));
$oDB->createCommand()->createIndex('{{idx1_answer_l10ns}}', '{{answer_l10ns}}', ['aid', 'language'], true);
dropPrimaryKey('answers');

addColumn('{{answers}}', 'aid', 'int');
$oDB->createCommand()->createIndex('answer_idx_10','{{answers}}',['qid','code','scale_id']);
$dataReader = $oDB->createCommand("select qid, code, scale_id from {{answers}} group by qid, code, scale_id")->query();
$iCounter = 1;
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("update {{answers}} set aid={$iCounter} where qid={$row['qid']} and code='{$row['code']}' and scale_id={$row['scale_id']}")->execute();
$iCounter++;
}
$oDB->createCommand("INSERT INTO {{answer_l10ns}} (aid, answer, language) select aid, answer, language from {{answers}}")->execute();
$dataReader = $oDB->createCommand("select a1.language,a1.aid FROM {{answers}} a1 INNER JOIN {{answers}} a2 WHERE a1.aid = a2.aid and a1.language<a2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{answers}} where aid={$row['aid']} and language='{$row['language']}'")->execute();
}
alterColumn('{{answers}}', 'aid', "pk", false);
$oDB->createCommand()->dropColumn('{{answers}}', 'answer');
$oDB->createCommand()->dropColumn('{{answers}}', 'language');
$oDB->createCommand()->dropindex('answer_idx_10','{{answers}}');
$oDB->createCommand()->createIndex('{{answers_idx}}', '{{answers}}', ['qid', 'code', 'scale_id'], true);

// Labels table
// label_l10ns
$oDB->createCommand()->createTable('{{label_l10ns}}', array(
'id' => "pk",
'label_id' => "integer NOT NULL",
'title' => "text",
'language' => "string(20) NOT NULL DEFAULT 'en'"
));
$oDB->createCommand()->createIndex('{{idx1_label_l10ns}}', '{{label_l10ns}}', ['label_id', 'language'], true);

alterColumn('{{labels}}', 'id', "int", true);
dropPrimaryKey('labels');
$dataReader = $oDB->createCommand("select lid,code from {{labels}} group by lid,code")->query();
$iCounter = 1;
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("update {{labels}} set id={$iCounter} where lid={$row['lid']} and code='{$row['code']}'")->execute();
$iCounter++;
}
$oDB->createCommand("INSERT INTO {{label_l10ns}} (label_id, title, language) select id, title, language from {{labels}}")->execute();
$dataReader = $oDB->createCommand("select l1.language,l1.id FROM {{labels}} l1 INNER JOIN {{labels}} l2 WHERE l1.id = l2.id and l1.language<l2.language")->query();
while (($row = $dataReader->read()) !== false) {
$oDB->createCommand("delete from {{labels}} where id={$row['id']} and language='{$row['language']}'")->execute();
}
alterColumn('{{labels}}', 'id', "pk", false);
$oDB->createCommand()->dropColumn('{{labels}}', 'title');
$oDB->createCommand()->dropColumn('{{labels}}', 'language');


// Extend language field on labelsets
alterColumn('{{labelsets}}', 'languages', "string(255)", false);

// Extend question type field length
alterColumn('{{questions}}', 'type', 'string(30)', false, 'T');

// Drop autoincrement on timings table primary key
upgradeSurveyTimings350();

$oDB->createCommand()->update('{{settings_global}}', array('stg_value'=>350), "stg_name='DBVersion'");
$oTransaction->commit();
}

} catch (Exception $e) {
Yii::app()->setConfig('Updating', false);
Expand Down

0 comments on commit 0e7b6a1

Please sign in to comment.