From 6ebd023c4e4b43ebeba0684613bfca4681a4b1e7 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 20 Dec 2020 14:01:46 +0000 Subject: [PATCH 1/3] Drop unused friendica tables --- src/Console/DatabaseStructure.php | 14 ++++++++--- src/Database/DBStructure.php | 40 +++++++++++++++++++++++++++++++ src/Database/View.php | 2 +- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/Console/DatabaseStructure.php b/src/Console/DatabaseStructure.php index 5a92587fc3..b998c420b8 100644 --- a/src/Console/DatabaseStructure.php +++ b/src/Console/DatabaseStructure.php @@ -48,11 +48,12 @@ class DatabaseStructure extends \Asika\SimpleConsole\Console $help = << [-h|--help|-?] |-f|--force] [-v] + bin/console dbstructure [-h|--help|-?] [-e|--execute] |-f|--force] [-o|--override] [-v] Commands dryrun Show database update schema queries without running them update Update database schema + drop Drop tables that aren't in use anymore dumpsql Dump database schema toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format initial Set needed initial values in the tables @@ -61,8 +62,9 @@ Commands Options -h|--help|-? Show help information -v Show more debug information. + -e|--execute Execute the dropping. -f|--force Force the update command (Even if the database structure matches) - -o|--override Override running or stalling updates + -o|--override Override running or stalling updates HELP; return $help; } @@ -109,6 +111,12 @@ HELP; $override = $this->getOption(['o', 'override'], false); $output = Update::run($basePath, $force, $override,true, false); break; + case "drop": + $execute = $this->getOption(['e', 'execute'], false); + ob_start(); + DBStructure::dropTables($execute); + $output = ob_get_clean(); + break; case "dumpsql": ob_start(); DBStructure::printStructure($basePath); @@ -133,7 +141,7 @@ HELP; $output = 'Unknown command: ' . $this->getArgument(0); } - $this->out($output); + $this->out(trim($output)); return 0; } diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index aa6a0cf29b..9b69f7b111 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -64,6 +64,46 @@ class DBStructure echo DI::l10n()->t('The database version had been set to %s.', $version); } + /** + * Drop unused tables + * + * @param boolean $execute + * @return void + */ + public static function dropTables(bool $execute) + { + $old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data', + 'item_id', 'poll', 'poll_result', 'queue', 'retriever_rule', 'sign', 'spam', 'term']; + + $tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'], + ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']); + + if (empty($tables)) { + echo DI::l10n()->t('No unused tables found.'); + return; + } + + if (!$execute) { + echo DI::l10n()->t('These tables are not used for friendica and will be deleted when you execute "dbstructure drop -e":') . "\n\n"; + } + + foreach ($tables as $table) { + if (in_array($table['TABLE_NAME'], $old_tables)) { + if ($execute) { + $sql = 'DROP TABLE ' . DBA::quoteIdentifier($table['TABLE_NAME']) . ';'; + echo $sql . "\n"; + + $result = DBA::e($sql); + if (!DBA::isResult($result)) { + self::printUpdateError($sql); + } + } else { + echo $table['TABLE_NAME'] . "\n"; + } + } + } + } + /** * Converts all tables from MyISAM/InnoDB Antelope to InnoDB Barracuda */ diff --git a/src/Database/View.php b/src/Database/View.php index fd0a8ce1a5..8457063205 100644 --- a/src/Database/View.php +++ b/src/Database/View.php @@ -158,7 +158,7 @@ class View } /** - * Check if the given table/view is a view + * Check if the given table/view is a table * * @param string $table * @return boolean "true" if it's a table From 246a90b0e6a130517ef2a9cec86d5cd87d3ba6ee Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 20 Dec 2020 14:04:00 +0000 Subject: [PATCH 2/3] Replaced tabs with spaces --- src/Console/DatabaseStructure.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/DatabaseStructure.php b/src/Console/DatabaseStructure.php index b998c420b8..a7934ef84c 100644 --- a/src/Console/DatabaseStructure.php +++ b/src/Console/DatabaseStructure.php @@ -62,9 +62,9 @@ Commands Options -h|--help|-? Show help information -v Show more debug information. - -e|--execute Execute the dropping. + -e|--execute Execute the dropping. -f|--force Force the update command (Even if the database structure matches) - -o|--override Override running or stalling updates + -o|--override Override running or stalling updates HELP; return $help; } From 1e273968513b7ae89e03732d2d7ea8eabbf84e6a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 20 Dec 2020 16:22:25 +0000 Subject: [PATCH 3/3] Improved help texts --- src/Console/DatabaseStructure.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Console/DatabaseStructure.php b/src/Console/DatabaseStructure.php index a7934ef84c..343c90023d 100644 --- a/src/Console/DatabaseStructure.php +++ b/src/Console/DatabaseStructure.php @@ -48,23 +48,25 @@ class DatabaseStructure extends \Asika\SimpleConsole\Console $help = << [-h|--help|-?] [-e|--execute] |-f|--force] [-o|--override] [-v] + bin/console dbstructure [options] Commands - dryrun Show database update schema queries without running them - update Update database schema - drop Drop tables that aren't in use anymore - dumpsql Dump database schema - toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format - initial Set needed initial values in the tables - version Set the database to a given number + drop Show tables that aren't in use by Friendica anymore and can be dropped + -e|--execute Execute the dropping -Options + update Update database schema + -f|--force Force the update command (Even if the database structure matches) + -o|--override Override running or stalling updates + + dryrun Show database update schema queries without running them + dumpsql Dump database schema + toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format + initial Set needed initial values in the tables + version Set the database to a given number + +General Options -h|--help|-? Show help information -v Show more debug information. - -e|--execute Execute the dropping. - -f|--force Force the update command (Even if the database structure matches) - -o|--override Override running or stalling updates HELP; return $help; }