Update "mrpetovan" email address
[friendica.git/.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 Performs database updates from the command line
15  *
16  * @author Hypolite Petovan <hypolite@mrpetovan.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 - Performs 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                                 $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 the pre_update_nnnn functions in update.php
82                                 for ($x = $stored; $x < $current; $x ++) {
83                                         $r = run_update_function($x, 'pre_update');
84                                         if (!$r) {
85                                                 break;
86                                         }
87                                 }
88
89                                 $output = DBStructure::update(true, true);
90
91                                 // run the update_nnnn functions in update.php
92                                 for ($x = $stored; $x < $current; $x ++) {
93                                         $r = run_update_function($x, 'update');
94                                         if (!$r) {
95                                                 break;
96                                         }
97                                 }
98
99                                 Core\Config::set('system', 'build', DB_UPDATE_VERSION);
100                                 break;
101                         case "dumpsql":
102                                 ob_start();
103                                 DBStructure::printStructure();
104                                 $output = ob_get_clean();
105                                 break;
106                         case "toinnodb":
107                                 ob_start();
108                                 DBStructure::convertToInnoDB();
109                                 $output = ob_get_clean();
110                                 break;
111                 }
112
113                 $this->out($output);
114
115                 return 0;
116         }
117
118 }