Permalink
Browse files

Merge branch 'w51_MDL-37420_m24_pgschema' of git://github.com/skodak/…

…moodle into MOODLE_24_STABLE
  • Loading branch information...
2 parents 47ee763 + a668f57 commit b56324b8299c1137a01395bf7f8f337751678e29 @stronk7 stronk7 committed Jan 9, 2013
Showing with 42 additions and 28 deletions.
  1. +10 −2 enrol/database/tests/sync_test.php
  2. +15 −9 lib/ddl/postgres_sql_generator.php
  3. +17 −17 lib/dml/pgsql_native_moodle_database.php
@@ -107,7 +107,11 @@ protected function init_enrol_database() {
$dbman->drop_table($table);
}
$dbman->create_table($table);
- set_config('remoteenroltable', $CFG->prefix.'enrol_database_test_enrols', 'enrol_database');
+ if (!empty($CFG->dboptions['dbschema'])) {
+ set_config('remoteenroltable', $CFG->dboptions['dbschema'].'.'.$CFG->prefix.'enrol_database_test_enrols', 'enrol_database');
+ } else {
+ set_config('remoteenroltable', $CFG->prefix.'enrol_database_test_enrols', 'enrol_database');
+ }
set_config('remotecoursefield', 'courseid', 'enrol_database');
set_config('remoteuserfield', 'userid', 'enrol_database');
set_config('remoterolefield', 'roleid', 'enrol_database');
@@ -123,7 +127,11 @@ protected function init_enrol_database() {
$dbman->drop_table($table);
}
$dbman->create_table($table);
- set_config('newcoursetable', $CFG->prefix.'enrol_database_test_courses', 'enrol_database');
+ if (!empty($CFG->dboptions['dbschema'])) {
+ set_config('newcoursetable', $CFG->dboptions['dbschema'].'.'.$CFG->prefix.'enrol_database_test_courses', 'enrol_database');
+ } else {
+ set_config('newcoursetable', $CFG->prefix.'enrol_database_test_courses', 'enrol_database');
+ }
set_config('newcoursefullname', 'fullname', 'enrol_database');
set_config('newcourseshortname', 'shortname', 'enrol_database');
set_config('newcourseidnumber', 'idnumber', 'enrol_database');
@@ -445,9 +445,11 @@ function getSequenceFromDB($xmldb_table) {
$tablename = $this->getTableName($xmldb_table);
$sequencename = $tablename . '_id_seq';
- if (!$this->mdb->get_record_sql("SELECT *
- FROM pg_class
- WHERE relname = ? AND relkind = 'S'",
+ if (!$this->mdb->get_record_sql("SELECT c.*
+ FROM pg_catalog.pg_class c
+ JOIN pg_catalog.pg_namespace as ns ON ns.oid = c.relnamespace
+ WHERE c.relname = ? AND c.relkind = 'S'
+ AND (ns.nspname = current_schema() OR ns.oid = pg_my_temp_schema())",
array($sequencename))) {
$sequencename = false;
}
@@ -473,19 +475,23 @@ public function isNameInUse($object_name, $type, $table_name) {
case 'ix':
case 'uix':
case 'seq':
- if ($check = $this->mdb->get_records_sql("SELECT relname
- FROM pg_class
- WHERE lower(relname) = ?", array(strtolower($object_name)))) {
+ if ($check = $this->mdb->get_records_sql("SELECT c.relname
+ FROM pg_class c
+ JOIN pg_catalog.pg_namespace as ns ON ns.oid = c.relnamespace
+ WHERE lower(c.relname) = ?
+ AND (ns.nspname = current_schema() OR ns.oid = pg_my_temp_schema())", array(strtolower($object_name)))) {
return true;
}
break;
case 'pk':
case 'uk':
case 'fk':
case 'ck':
- if ($check = $this->mdb->get_records_sql("SELECT conname
- FROM pg_constraint
- WHERE lower(conname) = ?", array(strtolower($object_name)))) {
+ if ($check = $this->mdb->get_records_sql("SELECT c.conname
+ FROM pg_constraint c
+ JOIN pg_catalog.pg_namespace as ns ON ns.oid = c.connamespace
+ WHERE lower(c.conname) = ?
+ AND (ns.nspname = current_schema() OR ns.oid = pg_my_temp_schema())", array(strtolower($object_name)))) {
return true;
}
break;
@@ -184,6 +184,11 @@ public function connect($dbhost, $dbuser, $dbpass, $dbname, $prefix, array $dbop
$sql = "SET bytea_output = 'escape'; ";
}
+ // Select schema if specified, otherwise the first one wins.
+ if (isset($this->dboptions['dbschema'])) {
+ $sql .= "SET search_path = '".$this->dboptions['dbschema']."'; ";
+ }
+
// Find out the bytea oid.
$sql .= "SELECT oid FROM pg_type WHERE typname = 'bytea'";
$this->query_start($sql, null, SQL_QUERY_AUX);
@@ -311,21 +316,12 @@ public function get_tables($usecache=true) {
}
$this->tables = array();
$prefix = str_replace('_', '|_', $this->prefix);
- if ($this->is_min_version('9.1')) {
- // Use ANSI standard information_schema in recent versions where it is fast enough.
- $sql = "SELECT table_name
- FROM information_schema.tables
- WHERE table_name LIKE '$prefix%' ESCAPE '|'
- AND table_type IN ('BASE TABLE', 'LOCAL TEMPORARY')";
- } else {
- // information_schema is horribly slow in <= 9.0, so use pg internals.
- // Note the pg_is_other_temp_schema. We only want temp objects from our own session.
- $sql = "SELECT c.relname
- FROM pg_class c
- WHERE c.relname LIKE '$prefix%' ESCAPE '|'
+ $sql = "SELECT c.relname
+ FROM pg_catalog.pg_class c
+ JOIN pg_catalog.pg_namespace as ns ON ns.oid = c.relnamespace
+ WHERE c.relname LIKE '$prefix%' ESCAPE '|'
AND c.relkind = 'r'
- AND NOT pg_is_other_temp_schema(c.relnamespace)";
- }
+ AND (ns.nspname = current_schema() OR ns.oid = pg_my_temp_schema())";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);
@@ -355,9 +351,11 @@ public function get_indexes($table) {
$indexes = array();
$tablename = $this->prefix.$table;
- $sql = "SELECT *
- FROM pg_catalog.pg_indexes
- WHERE tablename = '$tablename'";
+ $sql = "SELECT i.*
+ FROM pg_catalog.pg_indexes i
+ JOIN pg_catalog.pg_namespace as ns ON ns.nspname = i.schemaname
+ WHERE i.tablename = '$tablename'
+ AND (i.schemaname = current_schema() OR ns.oid = pg_my_temp_schema())";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
@@ -409,10 +407,12 @@ public function get_columns($table, $usecache=true) {
$sql = "SELECT a.attnum, a.attname AS field, t.typname AS type, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, d.adsrc
FROM pg_catalog.pg_class c
+ JOIN pg_catalog.pg_namespace as ns ON ns.oid = c.relnamespace
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
JOIN pg_catalog.pg_type t ON t.oid = a.atttypid
LEFT JOIN pg_catalog.pg_attrdef d ON (d.adrelid = c.oid AND d.adnum = a.attnum)
WHERE relkind = 'r' AND c.relname = '$tablename' AND c.reltype > 0 AND a.attnum > 0
+ AND (ns.nspname = current_schema() OR ns.oid = pg_my_temp_schema())
ORDER BY a.attnum";
$this->query_start($sql, null, SQL_QUERY_AUX);

0 comments on commit b56324b

Please sign in to comment.