]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/DatabaseStructure.php
Moved the functions update_db and run_update_function to a Friendica\Core\Update...
[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|-?] [-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 HELP;
40                 return $help;
41         }
42
43         protected function doExecute()
44         {
45                 $a = get_app();
46
47                 if ($this->getOption('v')) {
48                         $this->out('Class: ' . __CLASS__);
49                         $this->out('Arguments: ' . var_export($this->args, true));
50                         $this->out('Options: ' . var_export($this->options, true));
51                 }
52
53                 if (count($this->args) == 0) {
54                         $this->out($this->getHelp());
55                         return 0;
56                 }
57
58                 if (count($this->args) > 1) {
59                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
60                 }
61
62                 if (!DBA::connected()) {
63                         throw new RuntimeException('Unable to connect to database');
64                 }
65
66                 Core\Config::load();
67
68                 switch ($this->getArgument(0)) {
69                         case "dryrun":
70                                 $output = DBStructure::update(true, false);
71                                 break;
72                         case "update":
73                                 $build = Core\Config::get('system', 'build');
74                                 if (empty($build)) {
75                                         Core\Config::set('system', 'build', DB_UPDATE_VERSION);
76                                         $build = DB_UPDATE_VERSION;
77                                 }
78
79                                 $stored = intval($build);
80                                 $current = intval(DB_UPDATE_VERSION);
81
82                                 // run the pre_update_nnnn functions in update.php
83                                 for ($x = $stored; $x < $current; $x ++) {
84                                         $r = Update::runUpdateFunction($x, 'pre_update');
85                                         if (!$r) {
86                                                 break;
87                                         }
88                                 }
89
90                                 $output = DBStructure::update(true, true);
91
92                                 // run the update_nnnn functions in update.php
93                                 for ($x = $stored; $x < $current; $x ++) {
94                                         $r = Update::runUpdateFunction($x, 'update');
95                                         if (!$r) {
96                                                 break;
97                                         }
98                                 }
99
100                                 Core\Config::set('system', 'build', DB_UPDATE_VERSION);
101                                 break;
102                         case "dumpsql":
103                                 ob_start();
104                                 DBStructure::printStructure();
105                                 $output = ob_get_clean();
106                                 break;
107                         case "toinnodb":
108                                 ob_start();
109                                 DBStructure::convertToInnoDB();
110                                 $output = ob_get_clean();
111                                 break;
112                 }
113
114                 $this->out($output);
115
116                 return 0;
117         }
118
119 }