]> git.mjollnir.org Git - moodle.git/commitdiff
Drop all the unique keys inside PostgreSQL DB and convert them to
authorstronk7 <stronk7>
Sun, 24 Sep 2006 22:14:23 +0000 (22:14 +0000)
committerstronk7 <stronk7>
Sun, 24 Sep 2006 22:14:23 +0000 (22:14 +0000)
their equivalest unique indexes. We aren't going to allow unique
keys from 1.7 and upwards (until we decide to enforce referential
intregrity and until ADOdb support it)

lib/db/postgres7.php
version.php

index 2fe94a979c6a111a2232353cdf495c81b75a7c00..73f6b8f6f9c37e07ced8920bf96f804cbd13b691 100644 (file)
@@ -1923,6 +1923,37 @@ function main_upgrade($oldversion=0) {
         execute_sql("CREATE UNIQUE INDEX {$CFG->prefix}role_sor_uix ON {$CFG->prefix}role (sortorder);", false);
     }
 
+    if ($oldversion < 2006092510) {
+        /// Convert all the PG unique keys into their corresponding unique indexes
+        /// we don't want such keys inside Moodle 1.7 and above
+        /// Look for all the UNIQUE CONSTRAINSTS existing in DB
+        $uniquecons = get_records_sql ("SELECT conname, relname, conkey, clas.oid AS tableoid
+                                          FROM pg_constraint cons,
+                                               pg_class clas
+                                         WHERE cons.contype='u'
+                                           AND cons.conrelid = clas.oid");
+        /// Iterate over every unique constraint, calculating its fields
+        if ($uniquecons) {
+            foreach ($uniquecons as $uniquecon) {
+                $conscols = trim(trim($uniquecon->conkey, '}'), '{');
+                $conscols = explode(',', $conscols);
+            /// Iterate over each column to fetch its name
+                $indexcols = array();
+                foreach ($conscols as $conscol) {
+                    $column = get_record_sql ("SELECT attname, attname
+                                                 FROM pg_attribute
+                                                WHERE attrelid = $uniquecon->tableoid
+                                                  AND attnum   = $conscol");
+                    $indexcols[] = $column->attname;
+                }
+            /// Drop the old UNIQUE CONSTRAINT
+                execute_sql ("ALTER TABLE $uniquecon->relname DROP CONSTRAINT $uniquecon->conname", false);
+            /// Create the new UNIQUE INDEX
+                execute_sql ("CREATE UNIQUE INDEX {$uniquecon->relname}_".implode('_', $indexcols)."_uix ON $uniquecon->relname (".implode(', ', $indexcols).')', false);
+            }
+        }
+    }
+
 
     return $result;
 }
index 7b55067aee6f10a1c11627d2d2f15941adb9285e..c169f54635b8bf9da4c8668902903e760b09ae11 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-   $version = 2006092409;  // YYYYMMDD = date
+   $version = 2006092410;  // YYYYMMDD = date
                            //       XY = increments within a single day
 
    $release = '1.7 dev';    // Human-friendly version name