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