]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/DatabaseStructure.php
Improve Console/Config display for array values
[friendica.git] / src / Core / Console / DatabaseStructure.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core;
6 use Friendica\Database\DBStructure;
7
8 require_once 'boot.php';
9 require_once 'include/dba.php';
10
11 /**
12  * @brief Does database updates from the command line
13  *
14  * @author Hypolite Petovan <mrpetovan@gmail.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 - Does database updates
24 Usage
25         bin/console dbstructure <command> [-h|--help|-?] [-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 HELP;
37                 return $help;
38         }
39
40         protected function doExecute()
41         {
42                 $a = get_app();
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                 switch ($this->getArgument(0)) {
66                         case "dryrun":
67                                 $output = DBStructure::update(true, false);
68                                 break;
69                         case "update":
70                                 $output = DBStructure::update(true, true);
71
72                                 $build = Core\Config::get('system', 'build');
73                                 if (empty($build)) {
74                                         Core\Config::set('system', 'build', DB_UPDATE_VERSION);
75                                         $build = DB_UPDATE_VERSION;
76                                 }
77
78                                 $stored = intval($build);
79                                 $current = intval(DB_UPDATE_VERSION);
80
81                                 // run any left update_nnnn functions in update.php
82                                 for ($x = $stored; $x < $current; $x ++) {
83                                         $r = run_update_function($x);
84                                         if (!$r) {
85                                                 break;
86                                         }
87                                 }
88
89                                 Core\Config::set('system', 'build', DB_UPDATE_VERSION);
90                                 break;
91                         case "dumpsql":
92                                 ob_start();
93                                 DBStructure::printStructure();
94                                 $output = ob_get_clean();
95                                 break;
96                         case "toinnodb":
97                                 ob_start();
98                                 DBStructure::convertToInnoDB();
99                                 $output = ob_get_clean();
100                                 break;
101                 }
102
103                 $this->out($output);
104
105                 return 0;
106         }
107
108 }