Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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          $DB = new read_slave_moodle_database_mock_pgsql();
 121  
 122          $this->assertEquals(0, $DB->perf_get_reads_slave());
 123  
 124          $dbman = $DB->get_manager();
 125          $table = new xmldb_table('silly_test_table');
 126          $table->add_field('id', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
 127          $table->add_field('msg', XMLDB_TYPE_CHAR, 255);
 128          $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
 129          $dbman->create_temp_table($table);
 130  
 131          $DB->get_columns('silly_test_table');
 132          $DB->get_records('silly_test_table');
 133          $this->assertEquals(0, $DB->perf_get_reads_slave());
 134      }
 135  
 136      /**
 137       * Test readonly connection failure with real pgsql connection
 138       *
 139       * @return void
 140       */
 141      public function test_real_readslave_connect_fail() : void {
 142          global $DB;
 143  
 144          if ($DB->get_dbfamily() != 'postgres') {
 145              $this->markTestSkipped("Not postgres");
 146          }
 147  
 148          // Open second connection.
 149          $cfg = $DB->export_dbconfig();
 150          if (!isset($cfg->dboptions)) {
 151              $cfg->dboptions = array();
 152          }
 153          $cfg->dboptions['readonly'] = [
 154              'instance' => ['host.that.is.not'],
 155              'connecttimeout' => 1
 156          ];
 157  
 158          $db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
 159          $db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
 160          $this->assertTrue(count($db2->get_records('user')) > 0);
 161      }
 162  }