2018-03-24 19:39:13 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01: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 19:39:13 +01:00
|
|
|
|
2019-05-02 23:17:35 +02:00
|
|
|
namespace Friendica\Console;
|
2018-03-24 19:39:13 +01:00
|
|
|
|
2020-01-19 22:23:44 +01:00
|
|
|
use Friendica\Core\Config\Cache;
|
2018-10-06 00:53:13 +02:00
|
|
|
use Friendica\Core\Update;
|
2019-07-28 22:06:33 +02:00
|
|
|
use Friendica\Database\Database;
|
2018-03-24 19:39:13 +01:00
|
|
|
use Friendica\Database\DBStructure;
|
2018-07-20 04:15:21 +02:00
|
|
|
use RuntimeException;
|
2018-03-24 19:39:13 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Performs database updates from the command line
|
2018-03-24 19:39:13 +01:00
|
|
|
*/
|
|
|
|
class DatabaseStructure extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
2019-07-28 22:06:33 +02:00
|
|
|
/**
|
|
|
|
* @var Database
|
|
|
|
*/
|
|
|
|
private $dba;
|
|
|
|
/**
|
2020-01-19 22:23:44 +01:00
|
|
|
* @var Cache
|
2019-07-28 22:06:33 +02:00
|
|
|
*/
|
|
|
|
private $configCache;
|
|
|
|
|
2018-03-24 19:39:13 +01:00
|
|
|
protected function getHelp()
|
|
|
|
{
|
|
|
|
$help = <<<HELP
|
2018-08-29 15:06:56 +02:00
|
|
|
console dbstructure - Performs database updates
|
2018-03-24 19:39:13 +01:00
|
|
|
Usage
|
2018-10-29 10:16:07 +01:00
|
|
|
bin/console dbstructure <command> [-h|--help|-?] |-f|--force] [-v]
|
2018-03-24 19:39:13 +01: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 12:24:09 +01: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 19:39:13 +01:00
|
|
|
HELP;
|
|
|
|
return $help;
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:23:44 +01:00
|
|
|
public function __construct(Database $dba, Cache $configCache, $argv = null)
|
2019-07-28 22:06:33 +02:00
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
|
|
|
$this->dba = $dba;
|
|
|
|
$this->configCache = $configCache;
|
|
|
|
}
|
|
|
|
|
2018-03-24 19:39:13 +01: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 22:06:33 +02:00
|
|
|
if (!$this->dba->isConnected()) {
|
2018-07-20 04:15:21 +02:00
|
|
|
throw new RuntimeException('Unable to connect to database');
|
2018-03-24 19:39:13 +01:00
|
|
|
}
|
|
|
|
|
2019-07-28 22:06:33 +02:00
|
|
|
$basePath = $this->configCache->get('system', 'basepath');
|
2019-02-03 22:46:50 +01:00
|
|
|
|
2018-03-24 19:39:13 +01:00
|
|
|
switch ($this->getArgument(0)) {
|
|
|
|
case "dryrun":
|
2019-07-28 22:06:33 +02:00
|
|
|
$output = DBStructure::update($basePath, true, false);
|
2018-03-24 19:39:13 +01:00
|
|
|
break;
|
|
|
|
case "update":
|
2019-02-24 12:24:09 +01:00
|
|
|
$force = $this->getOption(['f', 'force'], false);
|
|
|
|
$override = $this->getOption(['o', 'override'], false);
|
2019-07-28 22:06:33 +02:00
|
|
|
$output = Update::run($basePath, $force, $override,true, false);
|
2018-03-24 19:39:13 +01:00
|
|
|
break;
|
|
|
|
case "dumpsql":
|
|
|
|
ob_start();
|
2019-07-28 22:06:33 +02:00
|
|
|
DBStructure::printStructure($basePath);
|
2018-03-24 19:39:13 +01:00
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
|
|
|
case "toinnodb":
|
|
|
|
ob_start();
|
|
|
|
DBStructure::convertToInnoDB();
|
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
2019-01-07 18:51:48 +01:00
|
|
|
default:
|
|
|
|
$output = 'Unknown command: ' . $this->getArgument(0);
|
2018-03-24 19:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out($output);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|