]> git.mxchange.org Git - friendica.git/blob - src/Console/DatabaseStructure.php
The value is used twice, so use a variable
[friendica.git] / src / Console / DatabaseStructure.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Update;
7 use Friendica\Database\Database;
8 use Friendica\Database\DBStructure;
9 use RuntimeException;
10
11 /**
12  * @brief Performs database updates from the command line
13  *
14  * @author Hypolite Petovan <hypolite@mrpetovan.com>
15  */
16 class DatabaseStructure extends \Asika\SimpleConsole\Console
17 {
18         protected $helpOptions = ['h', 'help', '?'];
19
20         /**
21          * @var Database
22          */
23         private $dba;
24         /**
25          * @var ConfigCache
26          */
27         private $configCache;
28
29         protected function getHelp()
30         {
31                 $help = <<<HELP
32 console dbstructure - Performs database updates
33 Usage
34         bin/console dbstructure <command> [-h|--help|-?] |-f|--force] [-v]
35
36 Commands
37         dryrun   Show database update schema queries without running them
38         update   Update database schema
39         dumpsql  Dump database schema
40         toinnodb Convert all tables from MyISAM to InnoDB
41
42 Options
43     -h|--help|-?       Show help information
44     -v                 Show more debug information.
45     -f|--force         Force the update command (Even if the database structure matches)
46     -o|--override      Override running or stalling updates
47 HELP;
48                 return $help;
49         }
50
51         public function __construct(Database $dba, ConfigCache $configCache, $argv = null)
52         {
53                 parent::__construct($argv);
54
55                 $this->dba = $dba;
56                 $this->configCache = $configCache;
57         }
58
59         protected function doExecute()
60         {
61                 if ($this->getOption('v')) {
62                         $this->out('Class: ' . __CLASS__);
63                         $this->out('Arguments: ' . var_export($this->args, true));
64                         $this->out('Options: ' . var_export($this->options, true));
65                 }
66
67                 if (count($this->args) == 0) {
68                         $this->out($this->getHelp());
69                         return 0;
70                 }
71
72                 if (count($this->args) > 1) {
73                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
74                 }
75
76                 if (!$this->dba->isConnected()) {
77                         throw new RuntimeException('Unable to connect to database');
78                 }
79
80                 $basePath = $this->configCache->get('system', 'basepath');
81
82                 switch ($this->getArgument(0)) {
83                         case "dryrun":
84                                 $output = DBStructure::update($basePath, true, false);
85                                 break;
86                         case "update":
87                                 $force    = $this->getOption(['f', 'force'], false);
88                                 $override = $this->getOption(['o', 'override'], false);
89                                 $output = Update::run($basePath, $force, $override,true, false);
90                                 break;
91                         case "dumpsql":
92                                 ob_start();
93                                 DBStructure::printStructure($basePath);
94                                 $output = ob_get_clean();
95                                 break;
96                         case "toinnodb":
97                                 ob_start();
98                                 DBStructure::convertToInnoDB();
99                                 $output = ob_get_clean();
100                                 break;
101                         default:
102                                 $output = 'Unknown command: ' . $this->getArgument(0);
103                 }
104
105                 $this->out($output);
106
107                 return 0;
108         }
109 }