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