]> git.mxchange.org Git - friendica.git/blob - src/Console/DatabaseStructure.php
b7eafaafdb77f44383702049d7f9eb1709ee434e
[friendica.git] / src / Console / DatabaseStructure.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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         /**
44          * @var Database
45          */
46         private $dba;
47         /**
48          * @var Cache
49          */
50         private $configCache;
51         /**
52          * @var DbaDefinition
53          */
54         private $dbaDefinition;
55         /**
56          * @var ViewDefinition
57          */
58         private $viewDefinition;
59         /**
60          * @var string
61          */
62         private $basePath;
63
64         protected function getHelp()
65         {
66                 $help = <<<HELP
67 console dbstructure - Performs database updates
68 Usage
69         bin/console dbstructure <command> [options]
70
71 Commands
72     drop     Show tables that aren't in use by Friendica anymore and can be dropped
73        -e|--execute    Execute the removal
74
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
78
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
84
85 General Options
86     -h|--help|-?       Show help information
87     -v                 Show more debug information.
88 HELP;
89                 return $help;
90         }
91
92         public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, Cache $configCache, $argv = null)
93         {
94                 parent::__construct($argv);
95
96                 $this->dba = $dba;
97                 $this->dbaDefinition = $dbaDefinition;
98                 $this->viewDefinition = $viewDefinition;
99                 $this->configCache = $configCache;
100                 $this->basePath = $basePath->getPath();
101         }
102
103         protected function doExecute()
104         {
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));
109                 }
110
111                 if (count($this->args) == 0) {
112                         $this->out($this->getHelp());
113                         return 0;
114                 }
115
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');
120                 }
121
122                 if (!$this->dba->isConnected()) {
123                         throw new RuntimeException('Unable to connect to database');
124                 }
125
126                 $basePath = $this->configCache->get('system', 'basepath');
127
128                 switch ($this->getArgument(0)) {
129                         case "dryrun":
130                                 $output = DBStructure::dryRun();
131                                 break;
132                         case "update":
133                                 $force    = $this->getOption(['f', 'force'], false);
134                                 $override = $this->getOption(['o', 'override'], false);
135                                 $output = Update::run($basePath, $force, $override,true, false);
136                                 break;
137                         case "drop":
138                                 $execute = $this->getOption(['e', 'execute'], false);
139                                 ob_start();
140                                 DBStructure::dropTables($execute);
141                                 $output = ob_get_clean();
142                                 break;
143                         case "dumpsql":
144                                 DocWriter::writeDbDefinition($this->dbaDefinition, $this->basePath);
145                                 $output = DbaDefinitionSqlWriter::create($this->dbaDefinition);
146                                 $output .= ViewDefinitionSqlWriter::create($this->viewDefinition);
147                                 break;
148                         case "toinnodb":
149                                 ob_start();
150                                 DBStructure::convertToInnoDB();
151                                 $output = ob_get_clean();
152                                 break;
153                         case "version":
154                                 ob_start();
155                                 DBStructure::setDatabaseVersion($this->getArgument(1));
156                                 $output = ob_get_clean();
157                                 break;
158                         case "initial":
159                                 ob_start();
160                                 DBStructure::checkInitialValues(true);
161                                 $output = ob_get_clean();
162                                 break;
163                         default:
164                                 $output = 'Unknown command: ' . $this->getArgument(0);
165                 }
166
167                 $this->out(trim($output));
168
169                 return 0;
170         }
171 }