Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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   */
  38  class core_dml_pgsql_read_slave_testcase extends base_testcase {
  39      /**
  40       * Test correct database handles are used for cursors
  41       *
  42       * @return void
  43       */
  44      public function test_cursors() : void {
  45          $DB = new read_slave_moodle_database_mock_pgsql();
  46  
  47          // Declare a cursor on a table that has not been written to.
  48          list($sql, $params, $type) = $DB->fix_sql_params("SELECT * FROM {table}");
  49          $sql = "DECLARE crs1 NO SCROLL CURSOR WITH HOLD FOR $sql";
  50          $DB->query_start($sql, null, SQL_QUERY_SELECT);
  51          $DB->query_end(null);
  52  
  53          // Declare a cursor on a table that has been written to.
  54          list($sql, $params, $type) = $DB->fix_sql_params("INSERT INTO {table2} (name) VALUES ('blah')");
  55          $DB->query_start($sql, null, SQL_QUERY_INSERT);
  56          $DB->query_end(null);
  57          list($sql, $params, $type) = $DB->fix_sql_params("SELECT * FROM {table2}");
  58          $sql = "DECLARE crs2 NO SCROLL CURSOR WITH HOLD FOR $sql";
  59          $DB->query_start($sql, null, SQL_QUERY_SELECT);
  60          $DB->query_end(null);
  61  
  62          // Read from the non-written to table cursor.
  63          $sql = 'FETCH 1 FROM crs1';
  64          $DB->query_start($sql, null, SQL_QUERY_AUX);
  65          $this->assertEquals('test_ro', $DB->get_db_handle());
  66          $DB->query_end(null);
  67  
  68          // Read from the written to table cursor.
  69          $sql = 'FETCH 1 FROM crs2';
  70          $DB->query_start($sql, null, SQL_QUERY_AUX);
  71          $this->assertEquals('test_rw', $DB->get_db_handle());
  72          $DB->query_end(null);
  73      }
  74  
  75      /**
  76       * Test readonly handle is used for reading from random pg_*() call queries.
  77       *
  78       * @return void
  79       */
  80      public function test_read_pg_table() : void {
  81          $DB = new read_slave_moodle_database_mock_pgsql();
  82  
  83          $this->assertEquals(0, $DB->perf_get_reads_slave());
  84  
  85          $DB->query_start('SELECT pg_whatever(1)', null, SQL_QUERY_SELECT);
  86          $this->assertEquals('test_ro', $DB->get_db_handle());
  87          $DB->query_end(null);
  88          $this->assertEquals(1, $DB->perf_get_reads_slave());
  89      }
  90  
  91      /**
  92       * Test readonly handle is not used for reading from special pg_*() call queries,
  93       * pg_try_advisory_lock and pg_advisory_unlock.
  94       *
  95       * @return void
  96       */
  97      public function test_read_pg_lock_table() : void {
  98          $DB = new read_slave_moodle_database_mock_pgsql();
  99  
 100          $this->assertEquals(0, $DB->perf_get_reads_slave());
 101  
 102          foreach (['pg_try_advisory_lock', 'pg_advisory_unlock'] as $fn) {
 103              $DB->query_start("SELECT $fn(1)", null, SQL_QUERY_SELECT);
 104              $this->assertEquals('test_rw', $DB->get_db_handle());
 105              $DB->query_end(null);
 106              $this->assertEquals(0, $DB->perf_get_reads_slave());
 107          }
 108      }
 109  
 110      /**
 111       * Test readonly handle is not used for reading from temptables
 112       * and getting temptables metadata.
 113       * This test is only possible because of no pg_query error reporting.
 114       * It may need to be removed in the future if we decide to handle null
 115       * results in pgsql_native_moodle_database differently.
 116       *
 117       * @return void
 118       */
 119      public function test_temp_table() : void {
 120          global $DB;
 121  
 122          if ($DB->get_dbfamily() != 'postgres') {
 123              $this->markTestSkipped("Not postgres");
 124          }
 125  
 126          // Open second connection.
 127          $cfg = $DB->export_dbconfig();
 128          if (!isset($cfg->dboptions)) {
 129              $cfg->dboptions = [];
 130          }
 131          if (!isset($cfg->dboptions['readonly'])) {
 132              $cfg->dboptions['readonly'] = [
 133                  'instance' => [$cfg->dbhost]
 134              ];
 135          }
 136  
 137          // Get a separate disposable db connection handle with guaranteed 'readonly' config.
 138          $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
 139          $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
 140  
 141          $dbman = $db2->get_manager();
 142  
 143          $table = new xmldb_table('silly_test_table');
 144          $table->add_field('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
 145          $table->add_field('msg', XMLDB_TYPE_CHAR, 255);
 146          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 147          $dbman->create_temp_table($table);
 148  
 149          // We need to go through the creation proces twice.
 150          // create_temp_table() performs some reads before the temp table is created.
 151          // First time around those reads should go to ro ...
 152          $reads = $db2->perf_get_reads_slave();
 153  
 154          $db2->get_columns('silly_test_table');
 155          $db2->get_records('silly_test_table');
 156          $this->assertEquals($reads, $db2->perf_get_reads_slave());
 157  
 158          $table2 = new xmldb_table('silly_test_table2');
 159          $table2->add_field('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
 160          $table2->add_field('msg', XMLDB_TYPE_CHAR, 255);
 161          $table2->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 162          $dbman->create_temp_table($table2);
 163  
 164          // ... but once the first temp table is created no more ro reads should occur.
 165          $db2->get_columns('silly_test_table2');
 166          $db2->get_records('silly_test_table2');
 167          $this->assertEquals($reads, $db2->perf_get_reads_slave());
 168  
 169          // Make database driver happy.
 170          $dbman->drop_table($table2);
 171          $dbman->drop_table($table);
 172      }
 173  
 174      /**
 175       * Test readonly connection failure with real pgsql connection
 176       *
 177       * @return void
 178       */
 179      public function test_real_readslave_connect_fail() : void {
 180          global $DB;
 181  
 182          if ($DB->get_dbfamily() != 'postgres') {
 183              $this->markTestSkipped("Not postgres");
 184          }
 185  
 186          // Open second connection.
 187          $cfg = $DB->export_dbconfig();
 188          if (!isset($cfg->dboptions)) {
 189              $cfg->dboptions = array();
 190          }
 191          $cfg->dboptions['readonly'] = [
 192              'instance' => ['host.that.is.not'],
 193              'connecttimeout' => 1
 194          ];
 195  
 196          $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
 197          $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
 198          $this->assertTrue(count($db2->get_records('user')) > 0);
 199      }
 200  }