1
0
mirror of https://github.com/moodle/moodle.git synced 2025-04-25 10:26:17 +02:00

MDL-70965 core_dml: read_slave - mock db handles as resources

So they comply with the type hints.
This commit is contained in:
Srdjan 2021-03-10 11:34:31 +10:00 committed by Marina Glancy
parent a5f0b354e7
commit ebf19a7085
3 changed files with 41 additions and 8 deletions

@ -48,12 +48,12 @@ class core_dml_mysqli_read_slave_testcase extends base_testcase {
$this->assertEquals(0, $DB->perf_get_reads_slave());
$DB->query_start("SELECT GET_LOCK('lock',1)", null, SQL_QUERY_SELECT);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
$this->assertEquals(0, $DB->perf_get_reads_slave());
$DB->query_start("SELECT RELEASE_LOCK('lock',1)", null, SQL_QUERY_SELECT);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
$this->assertEquals(0, $DB->perf_get_reads_slave());
}

@ -62,13 +62,13 @@ class core_dml_pgsql_read_slave_testcase extends base_testcase {
// Read from the non-written to table cursor.
$sql = 'FETCH 1 FROM crs1';
$DB->query_start($sql, null, SQL_QUERY_AUX);
$this->assertEquals('test_ro', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_ro());
$DB->query_end(null);
// Read from the written to table cursor.
$sql = 'FETCH 1 FROM crs2';
$DB->query_start($sql, null, SQL_QUERY_AUX);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
}
@ -83,7 +83,7 @@ class core_dml_pgsql_read_slave_testcase extends base_testcase {
$this->assertEquals(0, $DB->perf_get_reads_slave());
$DB->query_start('SELECT pg_whatever(1)', null, SQL_QUERY_SELECT);
$this->assertEquals('test_ro', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_ro());
$DB->query_end(null);
$this->assertEquals(1, $DB->perf_get_reads_slave());
}
@ -101,7 +101,7 @@ class core_dml_pgsql_read_slave_testcase extends base_testcase {
foreach (['pg_try_advisory_lock', 'pg_advisory_unlock'] as $fn) {
$DB->query_start("SELECT $fn(1)", null, SQL_QUERY_SELECT);
$this->assertEquals('test_rw', $DB->get_db_handle());
$this->assertTrue($DB->db_handle_is_rw());
$DB->query_end(null);
$this->assertEquals(0, $DB->perf_get_reads_slave());
}

@ -46,14 +46,47 @@ trait test_moodle_read_slave_trait {
// @codingStandardsIgnoreEnd
parent::__construct($external);
$rw = fopen("php://memory", 'r+');
fputs($rw, 'rw');
$ro = fopen("php://memory", 'r+');
fputs($ro, 'ro');
$this->wantreadslave = true;
$this->dbhwrite = 'test_rw';
$this->dbhreadonly = 'test_ro';
$this->dbhwrite = $rw;
$this->dbhreadonly = $ro;
$this->set_db_handle($this->dbhwrite);
$this->temptables = new moodle_temptables($this);
}
/**
* Check db handle
* @param string $id
* @return bool
*/
public function db_handle_is($id) {
$dbh = $this->get_db_handle();
rewind($dbh);
return stream_get_contents($dbh) == $id;
}
/**
* Check db handle is rw
* @return bool
*/
public function db_handle_is_rw() {
return $this->db_handle_is('rw');
}
/**
* Check db handle is ro
* @return bool
*/
public function db_handle_is_ro() {
return $this->db_handle_is('ro');
}
/**
* Upgrade to public
* @return resource