]> git.mxchange.org Git - friendica.git/blob - src/Console/DatabaseStructure.php
API: Accept "redirect_uris" as both array and string
[friendica.git] / src / Console / DatabaseStructure.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
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;
34 use RuntimeException;
35
36 /**
37  * Performs database updates from the command line
38  */
39 class DatabaseStructure extends \Asika\SimpleConsole\Console
40 {
41         protected $helpOptions = ['h', 'help', '?'];
42
43         /** @var Database */
44         private $dba;
45
46         /** @var Cache */
47         private $configCache;
48
49         /** @var DbaDefinition */
50         private $dbaDefinition;
51
52         /** @var ViewDefinition */
53         private $viewDefinition;
54
55         /** @var string */
56         private $basePath;
57
58         protected function getHelp()
59         {
60                 $help = <<<HELP
61 console dbstructure - Performs database updates
62 Usage
63         bin/console dbstructure <command> [options]
64
65 Commands
66     drop     Show tables that aren't in use by Friendica anymore and can be dropped
67        -e|--execute    Execute the removal
68
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
72
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
78
79 General Options
80     -h|--help|-?       Show help information
81     -v                 Show more debug information.
82 HELP;
83                 return $help;
84         }
85
86         public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, Cache $configCache, $argv = null)
87         {
88                 parent::__construct($argv);
89
90                 $this->dba = $dba;
91                 $this->dbaDefinition = $dbaDefinition;
92                 $this->viewDefinition = $viewDefinition;
93                 $this->configCache = $configCache;
94                 $this->basePath = $basePath->getPath();
95         }
96
97         protected function doExecute(): int
98         {
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));
103                 }
104
105                 if (count($this->args) == 0) {
106                         $this->out($this->getHelp());
107                         return 0;
108                 }
109
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');
114                 }
115
116                 if (!$this->dba->isConnected()) {
117                         throw new RuntimeException('Unable to connect to database');
118                 }
119
120                 $basePath = $this->configCache->get('system', 'basepath');
121
122                 switch ($this->getArgument(0)) {
123                         case "dryrun":
124                                 $output = DBStructure::dryRun();
125                                 break;
126                         case "update":
127                                 $force    = $this->getOption(['f', 'force'], false);
128                                 $override = $this->getOption(['o', 'override'], false);
129                                 $output = Update::run($basePath, $force, $override,true, false);
130                                 break;
131                         case "drop":
132                                 $execute = $this->getOption(['e', 'execute'], false);
133                                 ob_start();
134                                 DBStructure::dropTables($execute);
135                                 $output = ob_get_clean();
136                                 break;
137                         case "dumpsql":
138                                 DocWriter::writeDbDefinition($this->dbaDefinition, $this->basePath);
139                                 $output = DbaDefinitionSqlWriter::create($this->dbaDefinition);
140                                 $output .= ViewDefinitionSqlWriter::create($this->viewDefinition);
141                                 break;
142                         case "toinnodb":
143                                 ob_start();
144                                 DBStructure::convertToInnoDB();
145                                 $output = ob_get_clean();
146                                 break;
147                         case "version":
148                                 ob_start();
149                                 DBStructure::setDatabaseVersion($this->getArgument(1));
150                                 $output = ob_get_clean();
151                                 break;
152                         case "initial":
153                                 ob_start();
154                                 DBStructure::checkInitialValues(true);
155                                 $output = ob_get_clean();
156                                 break;
157                         default:
158                                 $output = 'Unknown command: ' . $this->getArgument(0);
159                 }
160
161                 $this->out(trim($output));
162
163                 return 0;
164         }
165 }