Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * DML read/read-write database handle tests for pgsql_native_moodle_database
  19   *
  20   * @package    core
  21   * @category   dml
  22   * @copyright  2018 Srdjan Janković, Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once (__DIR__.'/fixtures/read_slave_moodle_database_mock_pgsql.php');
  29  
  30  /**
  31   * DML pgsql_native_moodle_database read slave specific tests
  32   *
  33   * @package    core
  34   * @category   dml
  35   * @copyright  2018 Catalyst IT
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @covers     \pgsql_native_moodle_database
  38   */
  39  class dml_pgsql_read_slave_test extends base_testcase {
  40      /**
  41       * Test correct database handles are used for cursors
  42       *
  43       * @return void
  44       */
  45      public function test_cursors() : void {
  46          $DB = new read_slave_moodle_database_mock_pgsql();
  47  
  48          // Declare a cursor on a table that has not been written to.
  49          list($sql, $params, $type) = $DB->fix_sql_params("SELECT * FROM {table}");
  50          $sql = "DECLARE crs1 NO SCROLL CURSOR WITH HOLD FOR $sql";
  51          $DB->query_start($sql, null, SQL_QUERY_SELECT);
  52          $DB->query_end(null);
  53  
  54          // Declare a cursor on a table that has been written to.
  55          list($sql, $params, $type) = $DB->fix_sql_params("INSERT INTO {table2} (name) VALUES ('blah')");
  56          $DB->query_start($sql, null, SQL_QUERY_INSERT);
  57          $DB->query_end(null);
  58          list($sql, $params, $type) = $DB->fix_sql_params("SELECT * FROM {table2}");
  59          $sql = "DECLARE crs2 NO SCROLL CURSOR WITH HOLD FOR $sql";
  60          $DB->query_start($sql, null, SQL_QUERY_SELECT);
  61          $DB->query_end(null);
  62  
  63          // Read from the non-written to table cursor.
  64          $sql = 'FETCH 1 FROM crs1';
  65          $DB->query_start($sql, null, SQL_QUERY_AUX);
  66          $this->assertTrue($DB->db_handle_is_ro());
  67          $DB->query_end(null);
  68  
  69          // Read from the written to table cursor.
  70          $sql = 'FETCH 1 FROM crs2';
  71          $DB->query_start($sql, null, SQL_QUERY_AUX);
  72          $this->assertTrue($DB->db_handle_is_rw());
  73          $DB->query_end(null);
  74      }
  75  
  76      /**
  77       * Test readonly handle is used for reading from random pg_*() call queries.
  78       *
  79       * @return void
  80       */
  81      public function test_read_pg_table() : void {
  82          $DB = new read_slave_moodle_database_mock_pgsql();
  83  
  84          $this->assertEquals(0, $DB->perf_get_reads_slave());
  85  
  86          $DB->query_start('SELECT pg_whatever(1)', null, SQL_QUERY_SELECT);
  87          $this->assertTrue($DB->db_handle_is_ro());
  88          $DB->query_end(null);
  89          $this->assertEquals(1, $DB->perf_get_reads_slave());
  90      }
  91  
  92      /**
  93       * Test readonly handle is not used for reading from special pg_*() call queries,
  94       * pg_try_advisory_lock and pg_advisory_unlock.
  95       *
  96       * @return void
  97       */
  98      public function test_read_pg_lock_table() : void {
  99          $DB = new read_slave_moodle_database_mock_pgsql();
 100  
 101          $this->assertEquals(0, $DB->perf_get_reads_slave());
 102  
 103          foreach (['pg_try_advisory_lock', 'pg_advisory_unlock'] as $fn) {
 104              $DB->query_start("SELECT $fn(1)", null, SQL_QUERY_SELECT);
 105              $this->assertTrue($DB->db_handle_is_rw());
 106              $DB->query_end(null);
 107              $this->assertEquals(0, $DB->perf_get_reads_slave());
 108          }
 109      }
 110  
 111      /**
 112       * Test readonly handle is not used for reading from temptables
 113       * and getting temptables metadata.
 114       * This test is only possible because of no pg_query error reporting.
 115       * It may need to be removed in the future if we decide to handle null
 116       * results in pgsql_native_moodle_database differently.
 117       *
 118       * @return void
 119       */
 120      public function test_temp_table() : void {
 121          global $DB;
 122  
 123          if ($DB->get_dbfamily() != 'postgres') {
 124              $this->markTestSkipped("Not postgres");
 125          }
 126  
 127          // Open second connection.
 128          $cfg = $DB->export_dbconfig();
 129          if (!isset($cfg->dboptions)) {
 130              $cfg->dboptions = [];
 131          }
 132          if (!isset($cfg->dboptions['readonly'])) {
 133              $cfg->dboptions['readonly'] = [
 134                  'instance' => [$cfg->dbhost]
 135              ];
 136          }
 137  
 138          // Get a separate disposable db connection handle with guaranteed 'readonly' config.
 139          $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
 140          $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
 141  
 142          $dbman = $db2->get_manager();
 143  
 144          $table = new xmldb_table('silly_test_table');
 145          $table->add_field('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
 146          $table->add_field('msg', XMLDB_TYPE_CHAR, 255);
 147          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 148          $dbman->create_temp_table($table);
 149  
 150          // We need to go through the creation proces twice.
 151          // create_temp_table() performs some reads before the temp table is created.
 152          // First time around those reads should go to ro ...
 153          $reads = $db2->perf_get_reads_slave();
 154  
 155          $db2->get_columns('silly_test_table');
 156          $db2->get_records('silly_test_table');
 157          $this->assertEquals($reads, $db2->perf_get_reads_slave());
 158  
 159          $table2 = new xmldb_table('silly_test_table2');
 160          $table2->add_field('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
 161          $table2->add_field('msg', XMLDB_TYPE_CHAR, 255);
 162          $table2->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 163          $dbman->create_temp_table($table2);
 164  
 165          // ... but once the first temp table is created no more ro reads should occur.
 166          $db2->get_columns('silly_test_table2');
 167          $db2->get_records('silly_test_table2');
 168          $this->assertEquals($reads, $db2->perf_get_reads_slave());
 169  
 170          // Make database driver happy.
 171          $dbman->drop_table($table2);
 172          $dbman->drop_table($table);
 173      }
 174  
 175      /**
 176       * Test readonly connection failure with real pgsql connection
 177       *
 178       * @return void
 179       */
 180      public function test_real_readslave_connect_fail() : void {
 181          global $DB;
 182  
 183          if ($DB->get_dbfamily() != 'postgres') {
 184              $this->markTestSkipped("Not postgres");
 185          }
 186  
 187          // Open second connection.
 188          $cfg = $DB->export_dbconfig();
 189          if (!isset($cfg->dboptions)) {
 190              $cfg->dboptions = array();
 191          }
 192          $cfg->dboptions['readonly'] = [
 193              'instance' => ['host.that.is.not'],
 194              'connecttimeout' => 1
 195          ];
 196  
 197          $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
 198          $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
 199          $this->assertTrue(count($db2->get_records('user')) > 0);
 200      }
 201  }