]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/DatabaseStructure.php
Rename Friendica\Database\dba to Friendica\Database\DBA
[friendica.git] / src / Core / Console / DatabaseStructure.php
1 <?php
2
3 namespace Friendica\Core\Console;
4
5 use Friendica\Core;
6 use Friendica\Database\DBA;
7 use Friendica\Database\DBStructure;
8 use RuntimeException;
9
10 require_once 'boot.php';
11 require_once 'include/dba.php';
12
13 /**
14  * @brief Does database updates from the command line
15  *
16  * @author Hypolite Petovan <mrpetovan@gmail.com>
17  */
18 class DatabaseStructure extends \Asika\SimpleConsole\Console
19 {
20         protected $helpOptions = ['h', 'help', '?'];
21
22         protected function getHelp()
23         {
24                 $help = <<<HELP
25 console dbstructure - Does database updates
26 Usage
27         bin/console dbstructure <command> [-h|--help|-?] [-v]
28
29 Commands
30         dryrun   Show database update schema queries without running them
31         update   Update database schema
32         dumpsql  Dump database schema
33         toinnodb Convert all tables from MyISAM to InnoDB
34
35 Options
36     -h|--help|-? Show help information
37     -v           Show more debug information.
38 HELP;
39                 return $help;
40         }
41
42         protected function doExecute()
43         {
44                 $a = get_app();
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                                 $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 }