Merge pull request #6564 from marc1706/ticket/13162

[ticket/13162] Add truncate table functionality to database tools
This commit is contained in:
Marc Alexander 2024-02-23 19:57:49 +01:00 committed by GitHub
commit 12a942747a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 144 additions and 113 deletions

View file

@ -19,6 +19,7 @@ services:
- '@user'
- '@cache.driver'
- '@dbal.conn'
- '@dbal.tools'
- '@auth'
- '@log'
- '@config'

View file

@ -26,6 +26,7 @@ services:
arguments:
- '@config'
- '@dbal.conn'
- '@dbal.tools'
- '@event_dispatcher'
- '@language'
- '@user'

View file

@ -68,9 +68,10 @@ switch ($mode)
if (!$start)
{
$db->sql_query('TRUNCATE TABLE ' . POSTS_TABLE);
$db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE);
// $db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE . '_prefetch');
$db_tools = $phpbb_container->get('dbal.tools');
$db_tools->sql_truncate_table(POSTS_TABLE);
$db_tools->sql_truncate_table(TOPICS_TABLE);
}
$db->sql_query('LOCK TABLES ' . POSTS_TABLE . ' WRITE, ' . TOPICS_TABLE . ' WRITE');

View file

@ -105,7 +105,10 @@ function add_bots($bots)
$result = $db->sql_query($sql);
$group_id = (int) $db->sql_fetchfield('group_id', false, $result);
$db->sql_freeresult($result);
$db->sql_query('TRUNCATE TABLE ' . BOTS_TABLE);
// Truncate bots table
$db_tools = $phpbb_container->get('dbal.tools');
$db_tools->sql_truncate_table(BOTS_TABLE);
if (!$group_id)
{
@ -118,9 +121,6 @@ function add_bots($bots)
}
foreach ($bots as $bot_name => $bot_ary)
{
$user_row = array(

View file

@ -27,7 +27,7 @@ class acp_forums
function main($id, $mode)
{
global $db, $user, $auth, $template, $cache, $request, $phpbb_dispatcher;
global $phpbb_admin_path, $phpbb_root_path, $phpEx, $phpbb_log;
global $phpbb_admin_path, $phpbb_container, $phpbb_root_path, $phpEx, $phpbb_log;
$user->add_lang('acp/forums');
$this->tpl_name = 'acp_forums';
@ -210,7 +210,7 @@ class acp_forums
($action != 'edit' || empty($forum_id) || ($auth->acl_get('a_fauth') && $auth->acl_get('a_authusers') && $auth->acl_get('a_authgroups') && $auth->acl_get('a_mauth'))))
{
copy_forum_permissions($forum_perm_from, $forum_data['forum_id'], ($action == 'edit') ? true : false);
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
$copied_permissions = true;
}
/* Commented out because of questionable UI workflow - re-visit for 3.0.7
@ -789,7 +789,7 @@ class acp_forums
if (!empty($forum_perm_from) && $forum_perm_from != $forum_id)
{
copy_forum_permissions($forum_perm_from, $forum_id, true);
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
$auth->acl_clear_prefetch();
$cache->destroy('sql', FORUMS_TABLE);

View file

@ -574,16 +574,8 @@ class acp_icons
// The user has already selected a smilies_pak file
if ($current == 'delete')
{
switch ($db->get_sql_layer())
{
case 'sqlite3':
$db->sql_query('DELETE FROM ' . $table);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . $table);
break;
}
$db_tools = $phpbb_container->get('dbal.tools');
$db_tools->sql_truncate_table($table);
switch ($mode)
{

View file

@ -281,16 +281,8 @@ class acp_main
break;
case 'db_track':
switch ($db->get_sql_layer())
{
case 'sqlite3':
$db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
break;
}
$db_tools = $phpbb_container->get('dbal.tools');
$db_tools->sql_truncate_table(TOPICS_POSTED_TABLE);
// This can get really nasty... therefore we only do the last six months
$get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);
@ -370,7 +362,7 @@ class acp_main
// Clear permissions
$auth->acl_clear_prefetch();
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PURGE_CACHE');
@ -388,19 +380,11 @@ class acp_main
}
$tables = array(CONFIRM_TABLE, SESSIONS_TABLE);
$db_tools = $phpbb_container->get('dbal.tools');
foreach ($tables as $table)
{
switch ($db->get_sql_layer())
{
case 'sqlite3':
$db->sql_query("DELETE FROM $table");
break;
default:
$db->sql_query("TRUNCATE TABLE $table");
break;
}
$db_tools->sql_truncate_table($table);
}
// let's restore the admin session

View file

@ -679,7 +679,7 @@ class acp_permissions
function set_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id)
{
global $db, $cache, $user, $auth;
global $request;
global $request, $phpbb_container;
$psubmit = $request->variable('psubmit', array(0 => array(0 => 0)));
@ -747,7 +747,7 @@ class acp_permissions
// Do we need to recache the moderator lists?
if ($permission_type == 'm_')
{
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
}
// Remove users who are now moderators or admins from everyones foes list
@ -768,7 +768,7 @@ class acp_permissions
function set_all_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id)
{
global $db, $cache, $user, $auth;
global $request;
global $request, $phpbb_container;
// User or group to be set?
$ug_type = (count($user_id)) ? 'user' : 'group';
@ -817,7 +817,7 @@ class acp_permissions
// Do we need to recache the moderator lists?
if ($permission_type == 'm_')
{
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
}
// Remove users who are now moderators or admins from everyones foes list
@ -883,7 +883,7 @@ class acp_permissions
*/
function remove_permissions($mode, $permission_type, $auth_admin, &$user_id, &$group_id, &$forum_id)
{
global $user, $db, $cache, $auth;
global $user, $db, $cache, $auth, $phpbb_container;
// User or group to be set?
$ug_type = (count($user_id)) ? 'user' : 'group';
@ -900,7 +900,7 @@ class acp_permissions
// Do we need to recache the moderator lists?
if ($permission_type == 'm_')
{
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
}
$this->log_action($mode, 'del', $permission_type, $ug_type, (($ug_type == 'user') ? $user_id : $group_id), (count($forum_id) ? $forum_id : array(0 => 0)));
@ -1202,7 +1202,7 @@ class acp_permissions
*/
function copy_forum_permissions()
{
global $db, $auth, $cache, $template, $user, $request;
global $db, $auth, $cache, $phpbb_container, $template, $user, $request;
$user->add_lang('acp/forums');
@ -1217,7 +1217,7 @@ class acp_permissions
{
if (copy_forum_permissions($src, $dest))
{
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
$auth->acl_clear_prefetch();
$cache->destroy('sql', FORUMS_TABLE);

View file

@ -2490,26 +2490,17 @@ function auto_prune($forum_id, $prune_mode, $prune_flags, $prune_days, $prune_fr
* must be carried through for the moderators table.
*
* @param \phpbb\db\driver\driver_interface $db Database connection
* @param \phpbb\db\tools\tools_interface $db_tools Database tools
* @param \phpbb\cache\driver\driver_interface $cache Cache driver
* @param \phpbb\auth\auth $auth Authentication object
* @return void
*/
function phpbb_cache_moderators($db, $cache, $auth)
function phpbb_cache_moderators($db, $db_tools, $cache, $auth)
{
// Remove cached sql results
$cache->destroy('sql', MODERATOR_CACHE_TABLE);
// Clear table
switch ($db->get_sql_layer())
{
case 'sqlite3':
$db->sql_query('DELETE FROM ' . MODERATOR_CACHE_TABLE);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . MODERATOR_CACHE_TABLE);
break;
}
$db_tools->sql_truncate_table(MODERATOR_CACHE_TABLE);
// We add moderators who have forum moderator permissions without an explicit ACL_NEVER setting
$sql_ary = array();

View file

@ -126,8 +126,8 @@ function tz_select($default = '', $truncate = false)
*/
function cache_moderators()
{
global $db, $cache, $auth;
phpbb_cache_moderators($db, $cache, $auth);
global $db, $cache, $auth, $phpbb_container;
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
}
/**

View file

@ -1873,18 +1873,11 @@ function update_dynamic_config()
*/
function update_topics_posted()
{
global $db;
global $db, $phpbb_container;
switch ($db->get_sql_layer())
{
case 'sqlite3':
$db->sql_query('DELETE FROM ' . TOPICS_POSTED_TABLE);
break;
default:
$db->sql_query('TRUNCATE TABLE ' . TOPICS_POSTED_TABLE);
break;
}
/** @var \phpbb\db\tools\tools_interface $db_tools */
$db_tools = $phpbb_container->get('dbal.tools');
$db_tools->sql_truncate_table(TOPICS_POSTED_TABLE);
// This can get really nasty... therefore we only do the last six months
$get_from_time = time() - (6 * 4 * 7 * 24 * 60 * 60);

View file

@ -2255,7 +2255,7 @@ function group_delete($group_id, $group_name = false)
include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
}
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_GROUP_DELETE', false, array($group_name));
@ -3167,7 +3167,10 @@ function group_update_listings($group_id)
global $phpbb_root_path, $phpEx;
include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
}
phpbb_cache_moderators($db, $cache, $auth);
global $phpbb_container;
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
}
if ($mod_permissions || $admin_permissions)

View file

@ -416,18 +416,11 @@ class convertor
$this->db->sql_freeresult($result);
switch ($this->db->get_sql_layer())
{
case 'sqlite3':
$this->db->sql_query('DELETE FROM ' . $this->session_keys_table);
$this->db->sql_query('DELETE FROM ' . $this->session_table);
break;
$tools_factory = new \phpbb\db\tools\factory();
$db_tools = $tools_factory->get($this->db_doctrine);
default:
$this->db->sql_query('TRUNCATE TABLE ' . $this->session_keys_table);
$this->db->sql_query('TRUNCATE TABLE ' . $this->session_table);
break;
}
$db_tools->sql_truncate_table($this->session_keys_table);
$db_tools->sql_truncate_table($this->session_table);
return $this->controller_helper->render('installer_convert.html', 'CONVERT_COMPLETE');
}

View file

@ -1022,7 +1022,7 @@ class convertor
$db->sql_query('DELETE FROM ' . SESSIONS_TABLE);
@unlink($phpbb_container->getParameter('core.cache_dir') . 'data_global.' . $phpEx);
phpbb_cache_moderators($db, $cache, $auth);
phpbb_cache_moderators($db, $phpbb_container->get('dbal.tools'), $cache, $auth);
// And finally, add a note to the log
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_INSTALL_CONVERTED', false, array($convert->convertor_data['forum_name'], $config['version']));

View file

@ -25,6 +25,9 @@ class purge extends \phpbb\console\command\command
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\db\tools\tools_interface */
protected $db_tools;
/** @var \phpbb\auth\auth */
protected $auth;
@ -40,14 +43,17 @@ class purge extends \phpbb\console\command\command
* @param \phpbb\user $user User instance
* @param \phpbb\cache\driver\driver_interface $cache Cache instance
* @param \phpbb\db\driver\driver_interface $db Database connection
* @param \phpbb\db\tools\tools_interface $db_tools Database tools
* @param \phpbb\auth\auth $auth Auth instance
* @param \phpbb\log\log_interface $log Logger instance
* @param \phpbb\config\config $config Config instance
*/
public function __construct(\phpbb\user $user, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\config\config $config)
public function __construct(\phpbb\user $user, \phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db,
\phpbb\db\tools\tools_interface $db_tools, \phpbb\auth\auth $auth, \phpbb\log\log_interface $log, \phpbb\config\config $config)
{
$this->cache = $cache;
$this->db = $db;
$this->db_tools = $db_tools;
$this->auth = $auth;
$this->log = $log;
$this->config = $config;
@ -82,7 +88,7 @@ class purge extends \phpbb\console\command\command
// Clear permissions
$this->auth->acl_clear_prefetch();
phpbb_cache_moderators($this->db, $this->cache, $this->auth);
phpbb_cache_moderators($this->db, $this->db_tools, $this->cache, $this->auth);
$this->log->add('admin', ANONYMOUS, '', 'LOG_PURGE_CACHE', time(), array());

View file

@ -363,6 +363,21 @@ class doctrine implements tools_interface
);
}
/**
* {@inheritDoc}
*/
public function sql_truncate_table(string $table_name): void
{
try
{
$this->connection->executeQuery($this->get_schema_manager()->getDatabasePlatform()->getTruncateTableSQL($table_name));
}
catch (Exception $e)
{
return;
}
}
/**
* Returns an array of indices for either unique and primary keys, or simple indices.
*

View file

@ -207,4 +207,12 @@ interface tools_interface
* @return bool|string[] True if the statements have been executed
*/
public function sql_create_primary_key(string $table_name, $column);
/**
* Truncate the table
*
* @param string $table_name
* @return void
*/
public function sql_truncate_table(string $table_name): void;
}

View file

@ -16,6 +16,7 @@ namespace phpbb\install\module\install_data\task;
use Doctrine\DBAL\Exception;
use phpbb\auth\auth;
use phpbb\db\driver\driver_interface;
use phpbb\db\tools\tools_interface;
use phpbb\event\dispatcher;
use phpbb\install\database_task;
use phpbb\install\helper\config;
@ -50,6 +51,11 @@ class create_search_index extends database_task
*/
protected $db;
/**
* @var tools_interface
*/
protected $db_tools;
/**
* @var config
*/
@ -118,6 +124,7 @@ class create_search_index extends database_task
$this->auth = $container->get('auth');
$this->config = $container->get('config');
$this->db = $container->get('dbal.conn');
$this->db_tools = $container->get('dbal.tools');
$this->iohandler = $iohandler;
$this->installer_config = $config;
$this->phpbb_dispatcher = $container->get('event_dispatcher');
@ -130,6 +137,7 @@ class create_search_index extends database_task
$this->search_indexer = new fulltext_native(
$this->config,
$this->db,
$this->db_tools,
$this->phpbb_dispatcher,
$container->get('language'),
$this->user,

View file

@ -15,6 +15,7 @@ namespace phpbb\search\backend;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\db\tools\tools_interface;
use phpbb\event\dispatcher_interface;
use phpbb\language\language;
use phpbb\user;
@ -87,6 +88,12 @@ class fulltext_native extends base implements search_backend_interface
*/
protected $php_ext;
/**
* DBAL tools
* @var tools_interface
*/
protected $db_tools;
/**
* phpBB event dispatcher object
* @var dispatcher_interface
@ -107,6 +114,7 @@ class fulltext_native extends base implements search_backend_interface
*
* @param config $config Config object
* @param driver_interface $db Database object
* @param tools_interface $db_tools Database tools
* @param dispatcher_interface $phpbb_dispatcher Event dispatcher object
* @param language $language
* @param user $user User object
@ -116,13 +124,14 @@ class fulltext_native extends base implements search_backend_interface
* @param string $phpbb_root_path phpBB root path
* @param string $phpEx PHP file extension
*/
public function __construct(config $config, driver_interface $db, dispatcher_interface $phpbb_dispatcher,
public function __construct(config $config, driver_interface $db, tools_interface $db_tools, dispatcher_interface $phpbb_dispatcher,
language $language, user $user, string $search_results_table, string $search_wordlist_table,
string $search_wordmatch_table, string $phpbb_root_path, string $phpEx)
{
global $cache;
parent::__construct($cache, $config, $db, $user, $search_results_table);
$this->db_tools = $db_tools;
$this->phpbb_dispatcher = $phpbb_dispatcher;
$this->language = $language;
@ -1610,22 +1619,11 @@ class fulltext_native extends base implements search_backend_interface
*/
public function delete_index(int &$post_counter = null): ?array
{
$sql_queries = [];
switch ($this->db->get_sql_layer())
{
case 'sqlite3':
$sql_queries[] = 'DELETE FROM ' . $this->search_wordlist_table;
$sql_queries[] = 'DELETE FROM ' . $this->search_wordmatch_table;
$sql_queries[] = 'DELETE FROM ' . $this->search_results_table;
break;
default:
$sql_queries[] = 'TRUNCATE TABLE ' . $this->search_wordlist_table;
$sql_queries[] = 'TRUNCATE TABLE ' . $this->search_wordmatch_table;
$sql_queries[] = 'TRUNCATE TABLE ' . $this->search_results_table;
break;
}
$truncate_tables = [
$this->search_wordlist_table,
$this->search_wordmatch_table,
$this->search_results_table,
];
$stats = $this->stats;
@ -1633,19 +1631,22 @@ class fulltext_native extends base implements search_backend_interface
* Event to modify SQL queries before the native search index is deleted
*
* @event core.search_native_delete_index_before
* @var array sql_queries Array with queries for deleting the search index
*
* @var array stats Array with statistics of the current index (read only)
* @var array truncate_tables Array with tables that will be truncated
*
* @since 3.2.3-RC1
* @changed 4.0.0-a1 Removed sql_queries, only add/remove tables to truncate to truncate_tables
*/
$vars = array(
'sql_queries',
'stats',
'truncate_tables',
);
extract($this->phpbb_dispatcher->trigger_event('core.search_native_delete_index_before', compact($vars)));
foreach ($sql_queries as $sql_query)
foreach ($truncate_tables as $table)
{
$this->db->sql_query($sql_query);
$this->db_tools->sql_truncate_table($table);
}
return null;

View file

@ -22,6 +22,7 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case
protected $cache_dir;
protected $cache;
protected $db;
protected $db_tools;
protected $config;
protected $user;
@ -42,6 +43,8 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case
$this->cache = new \phpbb\cache\driver\file($this->cache_dir);
$this->db = $this->createMock('\phpbb\db\driver\driver_interface');
$tools_factory = new \phpbb\db\tools\factory();
$this->db_tools = $this->createMock('\phpbb\db\tools\doctrine');
$this->config = new \phpbb\config\config(array('assets_version' => 1));
$this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
@ -86,7 +89,7 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case
public function get_command_tester()
{
$application = new Application();
$application->add(new purge($this->user, $this->cache, $this->db, $this->createMock('\phpbb\auth\auth'), new \phpbb\log\dummy(), $this->config));
$application->add(new purge($this->user, $this->cache, $this->db, $this->db_tools, $this->createMock('\phpbb\auth\auth'), new \phpbb\log\dummy(), $this->config));
$command = $application->find('cache:purge');
return new CommandTester($command);

View file

@ -357,6 +357,34 @@ class phpbb_dbal_db_tools_test extends phpbb_database_test_case
$this->assertFalse($this->tools->sql_table_exists('prefix_test_table'));
}
public function test_truncate_table()
{
$this->tools->sql_create_table('truncate_test_table',
['COLUMNS' => [
'foo' => ['UINT', 42],
]]
);
$this->assertTrue($this->tools->sql_table_exists('truncate_test_table'));
$sql = 'INSERT INTO truncate_test_table(foo) VALUES(19)';
$this->db->sql_query($sql);
$sql = 'SELECT * FROM truncate_test_table';
$result = $this->db->sql_query($sql);
$rowset = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$this->assertGreaterThan(0, count($rowset), 'Failed asserting that data exists in truncate_test_table.');
$this->tools->sql_truncate_table('truncate_test_table');
$result = $this->db->sql_query($sql);
$rowset = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$this->assertEquals(0, count($rowset), 'Failed asserting that truncate was successful for table.');
}
public function test_perform_schema_changes_drop_tables()
{
$db_tools = $this->getMockBuilder('\phpbb\db\tools\doctrine')

View file

@ -16,6 +16,7 @@ require_once __DIR__ . '/../test_framework/phpbb_search_test_case.php';
class phpbb_search_native_test extends phpbb_search_test_case
{
protected $db;
protected $db_tools;
public function getDataSet()
{
@ -34,11 +35,13 @@ class phpbb_search_native_test extends phpbb_search_test_case
$user = $this->createMock('\phpbb\user');
$this->db = $this->new_dbal();
$tools_factory = new \phpbb\db\tools\factory();
$this->db_tools = $tools_factory->get($this->new_doctrine_dbal());
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
$class = self::get_search_wrapper('\phpbb\search\backend\fulltext_native');
$config['fulltext_native_min_chars'] = 2;
$config['fulltext_native_max_chars'] = 14;
$this->search = new $class($config, $this->db, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx);
$this->search = new $class($config, $this->db, $this->db_tools, $phpbb_dispatcher, $language, $user, SEARCH_RESULTS_TABLE, SEARCH_WORDLIST_TABLE, SEARCH_WORDMATCH_TABLE, $phpbb_root_path, $phpEx);
}
public function keywords()