Skip to content

Commit

Permalink
Merge pull request phpbb#6637 from rxu/ticket/17337
Browse files Browse the repository at this point in the history
[ticket/17337] Fix mysqli driver is missing transaction begin statement - 3.3.x
  • Loading branch information
marc1706 committed Jun 24, 2024
2 parents 15960a7 + dd3ebe2 commit 3647cf2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
4 changes: 3 additions & 1 deletion phpBB/phpbb/db/driver/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ function _sql_transaction($status = 'begin')
switch ($status)
{
case 'begin':
return @mysqli_autocommit($this->db_connect_id, false);
@mysqli_autocommit($this->db_connect_id, false);
$result = @mysqli_begin_transaction($this->db_connect_id);
return $result;
break;

case 'commit':
Expand Down
61 changes: 61 additions & 0 deletions tests/dbal/write_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,67 @@ public function test_delete()
$db->sql_freeresult($result);
}

public function test_delete_rollback()
{
$db = $this->new_dbal();

$is_myisam = false;
if ($db->get_sql_layer() === 'mysqli')
{
$table_status = $db->get_table_status('phpbb_config');
$is_myisam = isset($table_status['Engine']) && $table_status['Engine'] === 'MyISAM';
}

$db->sql_transaction('begin');

$sql = "DELETE FROM phpbb_config
WHERE config_name = 'config1'";
$db->sql_query($sql);

// Rollback and check that nothing was changed
$db->sql_transaction('rollback');

$sql = 'SELECT *
FROM phpbb_config';
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);

if (!$is_myisam)
{
$this->assertEquals(2, count($rows));
$this->assertEquals('config1', $rows[0]['config_name']);
}
else
{
// Rollback does not work on MyISAM
$this->assertEquals(1, count($rows));
$this->assertEquals('config2', $rows[0]['config_name']);

// Restore deleted config value on MyISAM
$sql = "INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('config1', 'foo', 0)";
$db->sql_query($sql);
}

$db->sql_transaction('begin');

$sql = "DELETE FROM phpbb_config
WHERE config_name = 'config1'";
$db->sql_query($sql);

// Commit and check that data was actually changed
$db->sql_transaction('commit');

$sql = 'SELECT *
FROM phpbb_config';
$result = $db->sql_query($sql);
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);

$this->assertEquals(1, count($rows));
$this->assertEquals('config2', $rows[0]['config_name']);
}

public function test_multiple_insert()
{
$db = $this->new_dbal();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_framework/phpbb_database_test_case.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public function getConnection()
return $this->createDefaultDBConnection($manager->get_pdo(), 'testdb');
}

public function new_dbal()
public function new_dbal() : \phpbb\db\driver\driver_interface
{
$config = $this->get_database_config();

Expand Down

0 comments on commit 3647cf2

Please sign in to comment.