3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Friendica\Core\Config\ValueObject\Cache;
25 use Friendica\Core\Update;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBStructure;
28 use Friendica\Database\Definition\DbaDefinition;
29 use Friendica\Database\Definition\ViewDefinition;
30 use Friendica\Util\BasePath;
31 use Friendica\Util\Writer\DbaDefinitionSqlWriter;
32 use Friendica\Util\Writer\DocWriter;
33 use Friendica\Util\Writer\ViewDefinitionSqlWriter;
37 * Performs database updates from the command line
39 class DatabaseStructure extends \Asika\SimpleConsole\Console
41 protected $helpOptions = ['h', 'help', '?'];
54 private $dbaDefinition;
58 private $viewDefinition;
64 protected function getHelp()
67 console dbstructure - Performs database updates
69 bin/console dbstructure <command> [options]
72 drop Show tables that aren't in use by Friendica anymore and can be dropped
73 -e|--execute Execute the removal
75 update Update database schema
76 -f|--force Force the update command (Even if the database structure matches)
77 -o|--override Override running or stalling updates
79 dryrun Show database update schema queries without running them
80 dumpsql Dump database schema
81 toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format
82 initial Set needed initial values in the tables
83 version Set the database to a given number
86 -h|--help|-? Show help information
87 -v Show more debug information.
92 public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, Cache $configCache, $argv = null)
94 parent::__construct($argv);
97 $this->dbaDefinition = $dbaDefinition;
98 $this->viewDefinition = $viewDefinition;
99 $this->configCache = $configCache;
100 $this->basePath = $basePath->getPath();
103 protected function doExecute()
105 if ($this->getOption('v')) {
106 $this->out('Class: ' . __CLASS__);
107 $this->out('Arguments: ' . var_export($this->args, true));
108 $this->out('Options: ' . var_export($this->options, true));
111 if (count($this->args) == 0) {
112 $this->out($this->getHelp());
116 if ((count($this->args) > 1) && ($this->getArgument(0) != 'version')) {
117 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
118 } elseif ((count($this->args) != 2) && ($this->getArgument(0) == 'version')) {
119 throw new \Asika\SimpleConsole\CommandArgsException('This command needs two arguments');
122 if (!$this->dba->isConnected()) {
123 throw new RuntimeException('Unable to connect to database');
126 $basePath = $this->configCache->get('system', 'basepath');
128 switch ($this->getArgument(0)) {
130 $output = DBStructure::dryRun();
133 $force = $this->getOption(['f', 'force'], false);
134 $override = $this->getOption(['o', 'override'], false);
135 $output = Update::run($basePath, $force, $override,true, false);
138 $execute = $this->getOption(['e', 'execute'], false);
140 DBStructure::dropTables($execute);
141 $output = ob_get_clean();
144 DocWriter::writeDbDefinition($this->dbaDefinition, $this->basePath);
145 $output = DbaDefinitionSqlWriter::create($this->dbaDefinition);
146 $output .= ViewDefinitionSqlWriter::create($this->viewDefinition);
150 DBStructure::convertToInnoDB();
151 $output = ob_get_clean();
155 DBStructure::setDatabaseVersion($this->getArgument(1));
156 $output = ob_get_clean();
160 DBStructure::checkInitialValues(true);
161 $output = ob_get_clean();
164 $output = 'Unknown command: ' . $this->getArgument(0);
167 $this->out(trim($output));