]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/DatabaseStructure.php
Line feeds fixed, not change in functionality
[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                 if ($this->getOption('v')) {
43                         $this->out('Class: ' . __CLASS__);
44                         $this->out('Arguments: ' . var_export($this->args, true));
45                         $this->out('Options: ' . var_export($this->options, true));
46                 }
47
48                 if (count($this->args) == 0) {
49                         $this->out($this->getHelp());
50                         return 0;
51                 }
52
53                 if (count($this->args) > 1) {
54                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
55                 }
56
57                 require_once '.htconfig.php';
58                 $result = \dba::connect($db_host, $db_user, $db_pass, $db_data);
59                 unset($db_host, $db_user, $db_pass, $db_data);
60
61                 if (!$result) {
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                                 $output = DBStructure::update(true, true);
73
74                                 $build = Core\Config::get('system', 'build');
75                                 if (empty($build)) {
76                                         Core\Config::set('system', 'build', DB_UPDATE_VERSION);
77                                         $build = DB_UPDATE_VERSION;
78                                 }
79
80                                 $stored = intval($build);
81                                 $current = intval(DB_UPDATE_VERSION);
82
83                                 // run any left update_nnnn functions in update.php
84                                 for ($x = $stored; $x < $current; $x ++) {
85                                         $r = run_update_function($x);
86                                         if (!$r) {
87                                                 break;
88                                         }
89                                 }
90
91                                 Core\Config::set('system', 'build', DB_UPDATE_VERSION);
92                                 break;
93                         case "dumpsql":
94                                 ob_start();
95                                 DBStructure::printStructure();
96                                 $output = ob_get_clean();
97                                 break;
98                         case "toinnodb":
99                                 ob_start();
100                                 DBStructure::convertToInnoDB();
101                                 $output = ob_get_clean();
102                                 break;
103                 }
104
105                 $this->out($output);
106
107                 return 0;
108         }
109
110 }