2018-03-24 14:39:13 -04:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-03-24 14:39:13 -04:00
|
|
|
|
2019-05-02 17:17:35 -04:00
|
|
|
namespace Friendica\Console;
|
2018-03-24 14:39:13 -04:00
|
|
|
|
2020-01-19 16:23:44 -05:00
|
|
|
use Friendica\Core\Config\Cache;
|
2018-10-05 18:53:13 -04:00
|
|
|
use Friendica\Core\Update;
|
2019-07-28 16:06:33 -04:00
|
|
|
use Friendica\Database\Database;
|
2018-03-24 14:39:13 -04:00
|
|
|
use Friendica\Database\DBStructure;
|
2018-07-19 22:15:21 -04:00
|
|
|
use RuntimeException;
|
2018-03-24 14:39:13 -04:00
|
|
|
|
|
|
|
/**
|
2020-01-19 01:05:23 -05:00
|
|
|
* Performs database updates from the command line
|
2018-03-24 14:39:13 -04:00
|
|
|
*/
|
|
|
|
class DatabaseStructure extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
2019-07-28 16:06:33 -04:00
|
|
|
/**
|
|
|
|
* @var Database
|
|
|
|
*/
|
|
|
|
private $dba;
|
|
|
|
/**
|
2020-01-19 16:23:44 -05:00
|
|
|
* @var Cache
|
2019-07-28 16:06:33 -04:00
|
|
|
*/
|
|
|
|
private $configCache;
|
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
protected function getHelp()
|
|
|
|
{
|
|
|
|
$help = <<<HELP
|
2018-08-29 09:06:56 -04:00
|
|
|
console dbstructure - Performs database updates
|
2018-03-24 14:39:13 -04:00
|
|
|
Usage
|
2018-10-29 05:16:07 -04:00
|
|
|
bin/console dbstructure <command> [-h|--help|-?] |-f|--force] [-v]
|
2018-03-24 14:39:13 -04:00
|
|
|
|
|
|
|
Commands
|
|
|
|
dryrun Show database update schema queries without running them
|
|
|
|
update Update database schema
|
|
|
|
dumpsql Dump database schema
|
|
|
|
toinnodb Convert all tables from MyISAM to InnoDB
|
|
|
|
|
|
|
|
Options
|
2019-02-24 06:24:09 -05:00
|
|
|
-h|--help|-? Show help information
|
|
|
|
-v Show more debug information.
|
|
|
|
-f|--force Force the update command (Even if the database structure matches)
|
|
|
|
-o|--override Override running or stalling updates
|
2018-03-24 14:39:13 -04:00
|
|
|
HELP;
|
|
|
|
return $help;
|
|
|
|
}
|
|
|
|
|
2020-01-19 16:23:44 -05:00
|
|
|
public function __construct(Database $dba, Cache $configCache, $argv = null)
|
2019-07-28 16:06:33 -04:00
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
|
|
|
$this->dba = $dba;
|
|
|
|
$this->configCache = $configCache;
|
|
|
|
}
|
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
protected function doExecute()
|
|
|
|
{
|
|
|
|
if ($this->getOption('v')) {
|
|
|
|
$this->out('Class: ' . __CLASS__);
|
|
|
|
$this->out('Arguments: ' . var_export($this->args, true));
|
|
|
|
$this->out('Options: ' . var_export($this->options, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->args) == 0) {
|
|
|
|
$this->out($this->getHelp());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->args) > 1) {
|
|
|
|
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
|
|
|
}
|
|
|
|
|
2019-07-28 16:06:33 -04:00
|
|
|
if (!$this->dba->isConnected()) {
|
2018-07-19 22:15:21 -04:00
|
|
|
throw new RuntimeException('Unable to connect to database');
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
|
2019-07-28 16:06:33 -04:00
|
|
|
$basePath = $this->configCache->get('system', 'basepath');
|
2019-02-03 16:46:50 -05:00
|
|
|
|
2018-03-24 14:39:13 -04:00
|
|
|
switch ($this->getArgument(0)) {
|
|
|
|
case "dryrun":
|
2019-07-28 16:06:33 -04:00
|
|
|
$output = DBStructure::update($basePath, true, false);
|
2018-03-24 14:39:13 -04:00
|
|
|
break;
|
|
|
|
case "update":
|
2019-02-24 06:24:09 -05:00
|
|
|
$force = $this->getOption(['f', 'force'], false);
|
|
|
|
$override = $this->getOption(['o', 'override'], false);
|
2019-07-28 16:06:33 -04:00
|
|
|
$output = Update::run($basePath, $force, $override,true, false);
|
2018-03-24 14:39:13 -04:00
|
|
|
break;
|
|
|
|
case "dumpsql":
|
|
|
|
ob_start();
|
2019-07-28 16:06:33 -04:00
|
|
|
DBStructure::printStructure($basePath);
|
2018-03-24 14:39:13 -04:00
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
|
|
|
case "toinnodb":
|
|
|
|
ob_start();
|
|
|
|
DBStructure::convertToInnoDB();
|
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
2019-01-07 12:51:48 -05:00
|
|
|
default:
|
|
|
|
$output = 'Unknown command: ' . $this->getArgument(0);
|
2018-03-24 14:39:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out($output);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|