1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 22:10:45 +02:00

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
22 changed files with 144 additions and 113 deletions

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;