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', '?'];
49 /** @var DbaDefinition */
50 private $dbaDefinition;
52 /** @var ViewDefinition */
53 private $viewDefinition;
58 protected function getHelp()
61 console dbstructure - Performs database updates
63 bin/console dbstructure <command> [options]
66 drop Show tables that aren't in use by Friendica anymore and can be dropped
67 -e|--execute Execute the removal
69 update Update database schema
70 -f|--force Force the update command (Even if the database structure matches)
71 -o|--override Override running or stalling updates
73 dryrun Show database update schema queries without running them
74 dumpsql Dump database schema
75 toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format
76 initial Set needed initial values in the tables
77 version Set the database to a given number
80 -h|--help|-? Show help information
81 -v Show more debug information.
86 public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, Cache $configCache, $argv = null)
88 parent::__construct($argv);
91 $this->dbaDefinition = $dbaDefinition;
92 $this->viewDefinition = $viewDefinition;
93 $this->configCache = $configCache;
94 $this->basePath = $basePath->getPath();
97 protected function doExecute(): int
99 if ($this->getOption('v')) {
100 $this->out('Class: ' . __CLASS__);
101 $this->out('Arguments: ' . var_export($this->args, true));
102 $this->out('Options: ' . var_export($this->options, true));
105 if (count($this->args) == 0) {
106 $this->out($this->getHelp());
110 if ((count($this->args) > 1) && ($this->getArgument(0) != 'version')) {
111 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
112 } elseif ((count($this->args) != 2) && ($this->getArgument(0) == 'version')) {
113 throw new \Asika\SimpleConsole\CommandArgsException('This command needs two arguments');
116 if (!$this->dba->isConnected()) {
117 throw new RuntimeException('Unable to connect to database');
120 $basePath = $this->configCache->get('system', 'basepath');
122 switch ($this->getArgument(0)) {
124 $output = DBStructure::dryRun();
127 $force = $this->getOption(['f', 'force'], false);
128 $override = $this->getOption(['o', 'override'], false);
129 $output = Update::run($basePath, $force, $override,true, false);
132 $execute = $this->getOption(['e', 'execute'], false);
134 DBStructure::dropTables($execute);
135 $output = ob_get_clean();
138 DocWriter::writeDbDefinition($this->dbaDefinition, $this->basePath);
139 $output = DbaDefinitionSqlWriter::create($this->dbaDefinition);
140 $output .= ViewDefinitionSqlWriter::create($this->viewDefinition);
144 DBStructure::convertToInnoDB();
145 $output = ob_get_clean();
149 DBStructure::setDatabaseVersion($this->getArgument(1));
150 $output = ob_get_clean();
154 DBStructure::checkInitialValues(true);
155 $output = ob_get_clean();
158 $output = 'Unknown command: ' . $this->getArgument(0);
161 $this->out(trim($output));