From 6bb88b0306af8691ae7e4f6545b9e9ebd0520acf Mon Sep 17 00:00:00 2001
From: Jean-Michel Vedrine <vedrine@vedrine.org>
Date: Sat, 27 Apr 2019 16:08:43 +0200
Subject: change ref field name in variable table

---
 db/install.xml | 38 +++++++++++++++++++-------------------
 db/upgrade.php | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 19 deletions(-)

(limited to 'db')

diff --git a/db/install.xml b/db/install.xml
index 5e2802c..b612af2 100644
--- a/db/install.xml
+++ b/db/install.xml
@@ -4,33 +4,33 @@
     xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
 >
   <TABLES>
-    <TABLE NAME="qtype_algebra_options" COMMENT="Options for algebra questions" NEXT="qtype_algebra_variables">
+    <TABLE NAME="qtype_algebra_options" COMMENT="Options for algebra questions">
       <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="questionid"/>
-        <FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="compareby"/>
-        <FIELD NAME="compareby" TYPE="char" LENGTH="20" NOTNULL="true" DEFAULT="evaluated" SEQUENCE="false" PREVIOUS="questionid" NEXT="nchecks"/>
-        <FIELD NAME="nchecks" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="10" SEQUENCE="false" PREVIOUS="compareby" NEXT="tolerance"/>
-        <FIELD NAME="tolerance" TYPE="float" NOTNULL="true" SEQUENCE="false" PREVIOUS="nchecks" NEXT="disallow"/>
-        <FIELD NAME="disallow" TYPE="text" NOTNULL="true" SEQUENCE="false" PREVIOUS="tolerance" NEXT="allowedfuncs"/>
-        <FIELD NAME="allowedfuncs" TYPE="text" NOTNULL="true" SEQUENCE="false" PREVIOUS="disallow" NEXT="answerprefix"/>
-        <FIELD NAME="answerprefix" TYPE="text" NOTNULL="true" SEQUENCE="false" PREVIOUS="allowedfuncs"/>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
+        <FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
+        <FIELD NAME="compareby" TYPE="char" LENGTH="20" NOTNULL="true" DEFAULT="evaluated" SEQUENCE="false"/>
+        <FIELD NAME="nchecks" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="10" SEQUENCE="false"/>
+        <FIELD NAME="tolerance" TYPE="float" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="disallow" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="allowedfuncs" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="answerprefix" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
       </FIELDS>
       <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
-        <KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+        <KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id"/>
       </KEYS>
     </TABLE>
-    <TABLE NAME="qtype_algebra_variables" COMMENT="Variables for algebra questions" PREVIOUS="qtype_algebra_options">
+    <TABLE NAME="qtype_algebra_variables" COMMENT="Variables for algebra questions">
       <FIELDS>
-        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="question"/>
-        <FIELD NAME="question" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
-        <FIELD NAME="name" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false" PREVIOUS="question" NEXT="min"/>
-        <FIELD NAME="min" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="-" SEQUENCE="false" PREVIOUS="name" NEXT="max"/>
-        <FIELD NAME="max" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="-" SEQUENCE="false" PREVIOUS="min"/>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
+        <FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
+        <FIELD NAME="name" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="min" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="-" SEQUENCE="false"/>
+        <FIELD NAME="max" TYPE="char" LENGTH="30" NOTNULL="true" DEFAULT="-" SEQUENCE="false"/>
       </FIELDS>
       <KEYS>
-        <KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="question"/>
-        <KEY NAME="question" TYPE="foreign" FIELDS="question" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+        <KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id"/>
       </KEYS>
     </TABLE>
   </TABLES>
diff --git a/db/upgrade.php b/db/upgrade.php
index 48cc159..f1db600 100644
--- a/db/upgrade.php
+++ b/db/upgrade.php
@@ -80,6 +80,48 @@ function xmldb_qtype_algebra_upgrade($oldversion=0) {
         }
         upgrade_plugin_savepoint(true, 2012061702, 'qtype', 'algebra');
     }
+    
+    if ($oldversion < 2019042705) {
+
+        // Define key question (foreign) to be dropped form qtype_algebra_variables.
+        $table = new xmldb_table('qtype_algebra_variables');
+        $key = new xmldb_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id'));
+
+        // Launch drop key question.
+        $dbman->drop_key($table, $key);
+
+        // Record that qtype_algebra savepoint was reached.
+        upgrade_plugin_savepoint(true, 2019042705, 'qtype', 'algebra');
+    }
+
+    if ($oldversion < 2019042706) {
+
+        // Rename field question on table qtype_algebra_variables to questionid.
+        $table = new xmldb_table('qtype_algebra_variables');
+        $field = new xmldb_field('question', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'id');
+
+        // Launch rename field question.
+        if ($dbman->field_exists($table, $field)) {
+            $dbman->rename_field($table, $field, 'questionid');
+        }
+
+        // Record that qtype_algebra savepoint was reached.
+        upgrade_plugin_savepoint(true, 2019042706, 'qtype', 'algebra');
+    }
+
+    if ($oldversion < 2019042707) {
+
+        // Define key questionid (foreign-unique) to be added to qtype_algebra_variables.
+        $table = new xmldb_table('qtype_algebra_variables');
+        $key = new xmldb_key('questionid', XMLDB_KEY_FOREIGN_UNIQUE, array('questionid'), 'question', array('id'));
+
+        // Launch add key questionid.
+        $dbman->add_key($table, $key);
+
+        // Record that qtype_algebra savepoint was reached.
+        upgrade_plugin_savepoint(true, 2019042707, 'qtype', 'algebra');
+    }
+
     return true;
 }
 
-- 
cgit v1.2.3