2018-03-24 18:39:13 +00:00
|
|
|
<?php
|
2024-08-24 15:27:00 +02:00
|
|
|
|
|
|
|
// Copyright (C) 2010-2024, the Friendica project
|
|
|
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2019-05-02 23:17:35 +02:00
|
|
|
namespace Friendica\Console;
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2023-01-15 16:12:25 +01:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
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 18:39:13 +00:00
|
|
|
use Friendica\Database\DBStructure;
|
2022-07-12 23:21:16 +02:00
|
|
|
use Friendica\Database\Definition\DbaDefinition;
|
|
|
|
use Friendica\Database\Definition\ViewDefinition;
|
|
|
|
use Friendica\Util\BasePath;
|
|
|
|
use Friendica\Util\Writer\DbaDefinitionSqlWriter;
|
|
|
|
use Friendica\Util\Writer\DocWriter;
|
|
|
|
use Friendica\Util\Writer\ViewDefinitionSqlWriter;
|
2018-07-19 22:15:21 -04:00
|
|
|
use RuntimeException;
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* Performs database updates from the command line
|
2018-03-24 18:39:13 +00:00
|
|
|
*/
|
|
|
|
class DatabaseStructure extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
2022-07-13 00:23:12 +02:00
|
|
|
/** @var Database */
|
2019-07-28 22:06:33 +02:00
|
|
|
private $dba;
|
2022-07-13 00:23:12 +02:00
|
|
|
|
2023-01-15 16:12:25 +01:00
|
|
|
/** @var IManageConfigValues */
|
|
|
|
private $config;
|
2022-07-13 00:23:12 +02:00
|
|
|
|
|
|
|
/** @var DbaDefinition */
|
2022-07-12 23:21:16 +02:00
|
|
|
private $dbaDefinition;
|
2022-07-13 00:23:12 +02:00
|
|
|
|
|
|
|
/** @var ViewDefinition */
|
2022-07-12 23:21:16 +02:00
|
|
|
private $viewDefinition;
|
2022-07-13 00:23:12 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2022-07-12 23:21:16 +02:00
|
|
|
private $basePath;
|
2019-07-28 22:06:33 +02:00
|
|
|
|
2018-03-24 18:39:13 +00:00
|
|
|
protected function getHelp()
|
|
|
|
{
|
|
|
|
$help = <<<HELP
|
2018-08-29 13:06:56 +00:00
|
|
|
console dbstructure - Performs database updates
|
2018-03-24 18:39:13 +00:00
|
|
|
Usage
|
2020-12-20 16:22:25 +00:00
|
|
|
bin/console dbstructure <command> [options]
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
Commands
|
2020-12-20 16:22:25 +00:00
|
|
|
drop Show tables that aren't in use by Friendica anymore and can be dropped
|
2022-06-03 20:14:38 +00:00
|
|
|
-e|--execute Execute the removal
|
2018-03-24 18:39:13 +00:00
|
|
|
|
2020-12-20 16:22:25 +00:00
|
|
|
update Update database schema
|
|
|
|
-f|--force Force the update command (Even if the database structure matches)
|
|
|
|
-o|--override Override running or stalling updates
|
|
|
|
|
|
|
|
dryrun Show database update schema queries without running them
|
|
|
|
dumpsql Dump database schema
|
|
|
|
toinnodb Convert all tables from MyISAM or InnoDB in the Antelope file format to InnoDB in the Barracuda file format
|
|
|
|
initial Set needed initial values in the tables
|
|
|
|
version Set the database to a given number
|
|
|
|
|
|
|
|
General Options
|
2019-02-24 12:24:09 +01:00
|
|
|
-h|--help|-? Show help information
|
|
|
|
-v Show more debug information.
|
2018-03-24 18:39:13 +00:00
|
|
|
HELP;
|
|
|
|
return $help;
|
|
|
|
}
|
|
|
|
|
2023-01-15 16:12:25 +01:00
|
|
|
public function __construct(Database $dba, DbaDefinition $dbaDefinition, ViewDefinition $viewDefinition, BasePath $basePath, IManageConfigValues $config, $argv = null)
|
2019-07-28 22:06:33 +02:00
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
|
|
|
$this->dba = $dba;
|
2022-07-12 23:21:16 +02:00
|
|
|
$this->dbaDefinition = $dbaDefinition;
|
|
|
|
$this->viewDefinition = $viewDefinition;
|
2023-01-15 16:12:25 +01:00
|
|
|
$this->config = $config;
|
2022-07-12 23:21:16 +02:00
|
|
|
$this->basePath = $basePath->getPath();
|
2019-07-28 22:06:33 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 21:09:49 +02:00
|
|
|
protected function doExecute(): int
|
2018-03-24 18:39:13 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-13 15:57:24 +00:00
|
|
|
if ((count($this->args) > 1) && ($this->getArgument(0) != 'version')) {
|
2018-03-24 18:39:13 +00:00
|
|
|
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
|
2020-09-13 15:57:24 +00:00
|
|
|
} elseif ((count($this->args) != 2) && ($this->getArgument(0) == 'version')) {
|
|
|
|
throw new \Asika\SimpleConsole\CommandArgsException('This command needs two arguments');
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 22:06:33 +02:00
|
|
|
if (!$this->dba->isConnected()) {
|
2018-07-19 22:15:21 -04:00
|
|
|
throw new RuntimeException('Unable to connect to database');
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 16:12:25 +01:00
|
|
|
$basePath = $this->config->get('system', 'basepath');
|
2019-02-03 22:46:50 +01:00
|
|
|
|
2018-03-24 18:39:13 +00:00
|
|
|
switch ($this->getArgument(0)) {
|
|
|
|
case "dryrun":
|
2021-01-30 13:31:59 +00:00
|
|
|
$output = DBStructure::dryRun();
|
2018-03-24 18:39:13 +00: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 18:39:13 +00:00
|
|
|
break;
|
2020-12-20 14:01:46 +00:00
|
|
|
case "drop":
|
|
|
|
$execute = $this->getOption(['e', 'execute'], false);
|
|
|
|
ob_start();
|
|
|
|
DBStructure::dropTables($execute);
|
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
2018-03-24 18:39:13 +00:00
|
|
|
case "dumpsql":
|
2022-07-12 23:21:16 +02:00
|
|
|
DocWriter::writeDbDefinition($this->dbaDefinition, $this->basePath);
|
|
|
|
$output = DbaDefinitionSqlWriter::create($this->dbaDefinition);
|
|
|
|
$output .= ViewDefinitionSqlWriter::create($this->viewDefinition);
|
2018-03-24 18:39:13 +00:00
|
|
|
break;
|
|
|
|
case "toinnodb":
|
|
|
|
ob_start();
|
|
|
|
DBStructure::convertToInnoDB();
|
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
2020-09-13 15:57:24 +00:00
|
|
|
case "version":
|
|
|
|
ob_start();
|
|
|
|
DBStructure::setDatabaseVersion($this->getArgument(1));
|
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
2020-11-23 18:58:18 +00:00
|
|
|
case "initial":
|
|
|
|
ob_start();
|
|
|
|
DBStructure::checkInitialValues(true);
|
|
|
|
$output = ob_get_clean();
|
|
|
|
break;
|
2019-01-07 12:51:48 -05:00
|
|
|
default:
|
|
|
|
$output = 'Unknown command: ' . $this->getArgument(0);
|
2018-03-24 18:39:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 14:01:46 +00:00
|
|
|
$this->out(trim($output));
|
2018-03-24 18:39:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|