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