]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/DatabaseStructure.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[friendica.git] / src / Core / Console / DatabaseStructure.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core;
6 use Friendica\Core\Update;
7 use Friendica\Database\DBA;
8 use Friendica\Database\DBStructure;
9 use RuntimeException;
10
11 /**
12  * @brief Performs database updates from the command line
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class DatabaseStructure extends \Asika\SimpleConsole\Console
17 {
18         protected $helpOptions = ['h', 'help', '?'];
19
20         protected function getHelp()
21         {
22                 $help = <<<HELP
23 console dbstructure - Performs database updates
24 Usage
25         bin/console dbstructure <command> [-h|--help|-?] |-f|--force] [-v]
26
27 Commands
28         dryrun   Show database update schema queries without running them
29         update   Update database schema
30         dumpsql  Dump database schema
31         toinnodb Convert all tables from MyISAM to InnoDB
32
33 Options
34     -h|--help|-? Show help information
35     -v           Show more debug information.
36     -f|--force   Force the command in case of "update" (Ignore failed updates/running updates)
37 HELP;
38                 return $help;
39         }
40
41         protected function doExecute()
42         {
43                 if ($this->getOption('v')) {
44                         $this->out('Class: ' . __CLASS__);
45                         $this->out('Arguments: ' . var_export($this->args, true));
46                         $this->out('Options: ' . var_export($this->options, true));
47                 }
48
49                 if (count($this->args) == 0) {
50                         $this->out($this->getHelp());
51                         return 0;
52                 }
53
54                 if (count($this->args) > 1) {
55                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
56                 }
57
58                 if (!DBA::connected()) {
59                         throw new RuntimeException('Unable to connect to database');
60                 }
61
62                 Core\Config::load();
63
64                 switch ($this->getArgument(0)) {
65                         case "dryrun":
66                                 $output = DBStructure::update(true, false);
67                                 break;
68                         case "update":
69                                 $force = $this->getOption(['f', 'force'], false);
70                                 $output = Update::run($force, true, false);
71                                 break;
72                         case "dumpsql":
73                                 ob_start();
74                                 DBStructure::printStructure();
75                                 $output = ob_get_clean();
76                                 break;
77                         case "toinnodb":
78                                 ob_start();
79                                 DBStructure::convertToInnoDB();
80                                 $output = ob_get_clean();
81                                 break;
82                 }
83
84                 $this->out($output);
85
86                 return 0;
87         }
88
89 }