]> git.mxchange.org Git - friendica.git/blob - src/Console/DatabaseStructure.php
Merge pull request #7069 from nupplaphil/task/mod_allfriends
[friendica.git] / src / Console / DatabaseStructure.php
1 <?php
2
3 namespace Friendica\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 update command (Even if the database structure matches)
37     -o|--override      Override running or stalling updates
38 HELP;
39                 return $help;
40         }
41
42         protected function doExecute()
43         {
44                 if ($this->getOption('v')) {
45                         $this->out('Class: ' . __CLASS__);
46                         $this->out('Arguments: ' . var_export($this->args, true));
47                         $this->out('Options: ' . var_export($this->options, true));
48                 }
49
50                 if (count($this->args) == 0) {
51                         $this->out($this->getHelp());
52                         return 0;
53                 }
54
55                 if (count($this->args) > 1) {
56                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
57                 }
58
59                 if (!DBA::connected()) {
60                         throw new RuntimeException('Unable to connect to database');
61                 }
62
63                 Core\Config::load();
64
65                 $a = get_app();
66
67                 switch ($this->getArgument(0)) {
68                         case "dryrun":
69                                 $output = DBStructure::update($a->getBasePath(), true, false);
70                                 break;
71                         case "update":
72                                 $force    = $this->getOption(['f', 'force'], false);
73                                 $override = $this->getOption(['o', 'override'], false);
74                                 $output = Update::run($a->getBasePath(), $force, $override,true, false);
75                                 break;
76                         case "dumpsql":
77                                 ob_start();
78                                 DBStructure::printStructure($a->getBasePath());
79                                 $output = ob_get_clean();
80                                 break;
81                         case "toinnodb":
82                                 ob_start();
83                                 DBStructure::convertToInnoDB();
84                                 $output = ob_get_clean();
85                                 break;
86                         default:
87                                 $output = 'Unknown command: ' . $this->getArgument(0);
88                 }
89
90                 $this->out($output);
91
92                 return 0;
93         }
94 }