]> git.mxchange.org Git - friendica.git/blob - src/Console/DatabaseStructure.php
Inverted condition
[friendica.git] / src / Console / DatabaseStructure.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Cache;
25 use Friendica\Core\Update;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBStructure;
28 use RuntimeException;
29
30 /**
31  * Performs database updates from the command line
32  */
33 class DatabaseStructure extends \Asika\SimpleConsole\Console
34 {
35         protected $helpOptions = ['h', 'help', '?'];
36
37         /**
38          * @var Database
39          */
40         private $dba;
41         /**
42          * @var Cache
43          */
44         private $configCache;
45
46         protected function getHelp()
47         {
48                 $help = <<<HELP
49 console dbstructure - Performs database updates
50 Usage
51         bin/console dbstructure <command> [options]
52
53 Commands
54     drop     Show tables that aren't in use by Friendica anymore and can be dropped
55        -e|--execute    Execute the dropping
56
57     update   Update database schema
58        -f|--force      Force the update command (Even if the database structure matches)
59        -o|--override   Override running or stalling updates
60
61     dryrun   Show database update schema queries without running them
62     dumpsql  Dump database schema
63     toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format
64     initial  Set needed initial values in the tables
65     version  Set the database to a given number
66
67 General Options
68     -h|--help|-?       Show help information
69     -v                 Show more debug information.
70 HELP;
71                 return $help;
72         }
73
74         public function __construct(Database $dba, Cache $configCache, $argv = null)
75         {
76                 parent::__construct($argv);
77
78                 $this->dba = $dba;
79                 $this->configCache = $configCache;
80         }
81
82         protected function doExecute()
83         {
84                 if ($this->getOption('v')) {
85                         $this->out('Class: ' . __CLASS__);
86                         $this->out('Arguments: ' . var_export($this->args, true));
87                         $this->out('Options: ' . var_export($this->options, true));
88                 }
89
90                 if (count($this->args) == 0) {
91                         $this->out($this->getHelp());
92                         return 0;
93                 }
94
95                 if ((count($this->args) > 1) && ($this->getArgument(0) != 'version')) {
96                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
97                 } elseif ((count($this->args) != 2) && ($this->getArgument(0) == 'version')) {
98                         throw new \Asika\SimpleConsole\CommandArgsException('This command needs two arguments');
99                 }
100
101                 if (!$this->dba->isConnected()) {
102                         throw new RuntimeException('Unable to connect to database');
103                 }
104
105                 $basePath = $this->configCache->get('system', 'basepath');
106
107                 switch ($this->getArgument(0)) {
108                         case "dryrun":
109                                 $output = DBStructure::dryRun();
110                                 break;
111                         case "update":
112                                 $force    = $this->getOption(['f', 'force'], false);
113                                 $override = $this->getOption(['o', 'override'], false);
114                                 $output = Update::run($basePath, $force, $override,true, false);
115                                 break;
116                         case "drop":
117                                 $execute = $this->getOption(['e', 'execute'], false);
118                                 ob_start();
119                                 DBStructure::dropTables($execute);
120                                 $output = ob_get_clean();
121                                 break;
122                         case "dumpsql":
123                                 DBStructure::writeStructure();
124                                 ob_start();
125                                 DBStructure::printStructure($basePath);
126                                 $output = ob_get_clean();
127                                 break;
128                         case "toinnodb":
129                                 ob_start();
130                                 DBStructure::convertToInnoDB();
131                                 $output = ob_get_clean();
132                                 break;
133                         case "version":
134                                 ob_start();
135                                 DBStructure::setDatabaseVersion($this->getArgument(1));
136                                 $output = ob_get_clean();
137                                 break;
138                         case "initial":
139                                 ob_start();
140                                 DBStructure::checkInitialValues(true);
141                                 $output = ob_get_clean();
142                                 break;
143                         default:
144                                 $output = 'Unknown command: ' . $this->getArgument(0);
145                 }
146
147                 $this->out(trim($output));
148
149                 return 0;
150         }
151 }