Show hint in CLI

This commit is contained in:
Victor Dubiniuk 2016-03-29 16:57:41 +03:00 committed by Joas Schilling
parent cfd8cc3fd8
commit b87b27cbd9
No known key found for this signature in database
GPG Key ID: 70A0B324C41C0946
7 changed files with 179 additions and 158 deletions

View File

@ -30,6 +30,7 @@
namespace OC\Core\Command;
use OC\Console\TimestampFormatter;
use OC\ReleaseNotes;
use OC\Updater;
use OCP\IConfig;
use OCP\ILogger;
@ -37,8 +38,9 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ConfirmationQuestion;
class Upgrade extends Command {
class Upgrade extends Base {
const ERROR_SUCCESS = 0;
const ERROR_NOT_INSTALLED = 1;
@ -53,17 +55,23 @@ class Upgrade extends Command {
/** @var ILogger */
private $logger;
/** @var ReleaseNotes */
private $releaseNotes;
/**
* @param IConfig $config
* @param ILogger $logger
* @param ReleaseNotes $releaseNotes
*/
public function __construct(IConfig $config, ILogger $logger) {
public function __construct(IConfig $config, ILogger $logger, ReleaseNotes $releaseNotes) {
parent::__construct();
$this->config = $config;
$this->logger = $logger;
$this->releaseNotes = $releaseNotes;
}
protected function configure() {
parent::configure();
$this
->setName('upgrade')
->setDescription('run upgrade routines after installation of a new release. The release has to be installed before.')
@ -95,6 +103,19 @@ class Upgrade extends Command {
*/
protected function execute(InputInterface $input, OutputInterface $output) {
if ($input->isInteractive()) {
$installedVersion = $this->config->getSystemValue('version', '0.0.0');
$currentVersion = \OCP\Util::getVersion();
$releaseNotesArray = $this->releaseNotes->getReleaseNotes($installedVersion, $currentVersion);
if (!empty($releaseNotesArray)) {
$this->writeArrayInOutputFormat($input, $output, $releaseNotesArray);
if (!$this->ask($input, $output)){
return self::ERROR_SUCCESS;
}
}
}
$simulateStepEnabled = true;
$updateStepEnabled = true;
$skip3rdPartyAppsDisable = false;
@ -262,4 +283,17 @@ class Upgrade extends Command {
);
}
}
/**
* Ask for confirmation
* @param InputInterface $input
* @param OutputInterface $output
* @return bool
*/
public function ask(InputInterface $input, OutputInterface $output){
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Continue with update (y/n)' . PHP_EOL, true);
return $helper->ask($input, $output, $question);
}
}

View File

@ -114,7 +114,11 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) {
$application->add(new OC\Core\Command\Maintenance\Repair(new \OC\Repair(\OC\Repair::getRepairSteps()), \OC::$server->getConfig()));
$application->add(new OC\Core\Command\Maintenance\SingleUser(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger()));
$application->add(new OC\Core\Command\Upgrade(
\OC::$server->getConfig(),
\OC::$server->getLogger(),
new \OC\ReleaseNotes(\OC::$server->getDatabaseConnection())
));
$application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
$application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager()));

View File

@ -392,7 +392,7 @@ class OC {
$tmpl->assign('isAppsOnlyUpgrade', false);
}
$releaseNotes = \OC::$server->getReleaseNotes();
$releaseNotes = new \OC\ReleaseNotes(\OC::$server->getDatabaseConnection());
// get third party apps
$ocVersion = \OCP\Util::getVersion();

View File

@ -32,18 +32,18 @@ class ReleaseNotes {
protected $dbConnection;
/**
* @param OCP\IDBConnection $connection
* @param \OCP\IDBConnection $dbConnection
*/
public function __construct(IDBConnection $dbConnection){
public function __construct(IDBConnection $dbConnection) {
$this->dbConnection = $dbConnection;
}
/**
* @param string $fromVersion
* @param string $toVersion
* @return array
* @return string[]
*/
public function getReleaseNotes($fromVersion, $toVersion){
public function getReleaseNotes($fromVersion, $toVersion) {
$releaseNotes = [];
$l10n = \OC::$server->getL10N('core');
@ -59,16 +59,16 @@ class ReleaseNotes {
$toVersionMajorMinor = '';
}
if ( $fromVersionMajorMinor === '8.2' && $toVersionMajorMinor === '9.0' ) {
if ($fromVersionMajorMinor === '8.2' && $toVersionMajorMinor === '9.0') {
if (!$this->isCliMode() && $this->countFilecacheEntries() > 200000) {
$releaseNotes[] = $l10n->t(
"You have an ownCloud installation with over 200.000 files so the upgrade might take a while. The recommendation is to use the command-line instead of the web interface for big ownCloud servers."
'You have an ownCloud installation with over 200.000 files so the upgrade might take a while. The recommendation is to use the command-line instead of the web interface for big ownCloud servers.'
);
}
if ($this->isMysql() && $this->countFilecacheEntries() > 200000) {
$releaseNotes[] = $l10n->t(
"Hint: You can speed up the upgrade by executing this SQL command manually: ALTER TABLE %s ADD COLUMN checksum varchar(255) DEFAULT NULL AFTER permissions;",
[$this->dbConnection->getPrefix().'filecache']
'Hint: You can speed up the upgrade by executing this SQL command manually: ALTER TABLE %s ADD COLUMN checksum varchar(255) DEFAULT NULL AFTER permissions;',
[$this->dbConnection->getQueryBuilder()->getTableName('filecache')]
);
}
}
@ -78,7 +78,7 @@ class ReleaseNotes {
/**
* @return bool
*/
protected function isCliMode(){
protected function isCliMode() {
return \OC::$CLI;
}
@ -94,9 +94,15 @@ class ReleaseNotes {
* @return int
*/
protected function countFilecacheEntries(){
$result = $this->dbConnection->executeQuery("SELECT COUNT(*) FROM *PREFIX*filecache");
$count = $result->fetchColumn();
return $count ? $count : 0;
$query = $this->dbConnection->getQueryBuilder();
$query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries')
->from('filecache');
$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();
return (int) $row['num_entries'];
}
/**

View File

@ -623,12 +623,6 @@ class Server extends ServerContainer implements IServerContainer {
return $manager;
});
$this->registerService('ReleaseNotes', function (Server $c) {
return new \OC\ReleaseNotes(
$c->getDatabaseConnection()
);
});
}
/**
@ -1282,11 +1276,4 @@ class Server extends ServerContainer implements IServerContainer {
return $this->query('ShareManager');
}
/**
* @return \OC\ReleaseNotes
*/
public function getReleaseNotes() {
return $this->query('ReleaseNotes');
}
}

View File

@ -1,129 +0,0 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
class Test_ReleaseNotes extends \Test\TestCase {
protected $prefix = 'ocx_';
protected function setUp() {
parent::setUp();
}
protected function tearDown() {
$this->expected = [];
parent::tearDown();
}
public function resultProvider82to90(){
$l10n = \OC::$server->getL10N('core');
$alterTableMessage = $l10n->t(
"Hint: You can speed up the upgrade by executing this SQL command manually: ALTER TABLE %s ADD COLUMN checksum varchar(255) DEFAULT NULL AFTER permissions;",
['ocx_filecache']
);
$useCliMessage = $l10n->t(
"You have an ownCloud installation with over 200.000 files so the upgrade might take a while. The recommendation is to use the command-line instead of the web interface for big ownCloud servers."
);
return [
[ [], false, false, 20 ],
[ [], false, true, 20 ],
[ [], true, false, 20 ],
[ [], true, true, 20 ],
[ [ $useCliMessage ], false, false, 1000000 ],
[ [], false, true, 1000000 ],
[ [ $useCliMessage, $alterTableMessage ], true, false, 1000000 ],
[ [ $alterTableMessage ], true, true, 1000000 ],
];
}
/**
* @dataProvider resultProvider82to90
*/
public function test82to90($expected, $isMysql, $isCliMode, $fileCount){
$releaseNotesMock = $this->getReleaseNotesMock($isMysql, $isCliMode, $fileCount);
$actual = $releaseNotesMock->getReleaseNotes('8.2.22', '9.0.1');
$this->assertEquals($expected, $actual);
}
public function resultProvider90to91(){
return [
[ [], false, false, 20 ],
[ [], false, true, 20 ],
[ [], true, false, 20 ],
[ [], true, true, 20 ],
[ [], false, false, 1000000 ],
[ [], false, true, 1000000 ],
[ [], true, false, 1000000 ],
[ [], true, true, 1000000 ],
];
}
/**
* @dataProvider resultProvider90to91
*/
public function test90to91($expected, $isMysql, $isCliMode, $fileCount){
$releaseNotesMock = $this->getReleaseNotesMock($isMysql, $isCliMode, $fileCount);
$actual = $releaseNotesMock->getReleaseNotes('9.0.1', '9.1.0');
$this->assertEquals($expected, $actual);
}
private function getReleaseNotesMock($isMysql, $isCliMode, $fileCount){
$dbConnectionMock = $this->getMockBuilder('OCP\IDBConnection')
->setMethods(array_merge($this->getMethods('OCP\IDBConnection'), ['getPrefix']))
->getMock()
;
$dbConnectionMock->expects($this->any())
->method('getPrefix')
->willReturn($this->prefix)
;
$releaseNotesMock = $this->getMockBuilder('OC\ReleaseNotes')
->setConstructorArgs([$dbConnectionMock])
->setMethods(['isMysql', 'isCliMode', 'countFilecacheEntries'])
->getMock()
;
$releaseNotesMock->expects($this->any())
->method('isMysql')
->willReturn($isMysql)
;
$releaseNotesMock->expects($this->any())
->method('isCliMode')
->willReturn($isCliMode)
;
$releaseNotesMock->expects($this->any())
->method('countFilecacheEntries')
->willReturn($fileCount)
;
return $releaseNotesMock;
}
private function getMethods($class){
$methods = [];
if (class_exists($class) || interface_exists($class)) {
$reflector = new ReflectionClass($class);
foreach ($reflector->getMethods( ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_ABSTRACT ) as $method) {
$methods[] = $method->getName();
}
}
return $methods;
}
}

View File

@ -0,0 +1,119 @@
<?php
/**
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace Test;
class ReleaseNotesTest extends \Test\TestCase {
/**
* @param bool $isMysql
* @param bool $isCliMode
* @param int $fileCount
* @return \PHPUnit_Framework_MockObject_MockObject|\OC\ReleaseNotes
*/
protected function getReleaseNotesMock($isMysql, $isCliMode, $fileCount) {
$query = $this->getMockBuilder('OCP\DB\QueryBuilder\IQueryBuilder')
->disableOriginalConstructor()
->getMock();
$query->expects($this->any())
->method('getTableName')
->willReturnCallback(function($tableName) {
return 'ocx_' . $tableName;
});
$dbConnectionMock = $this->getMockBuilder('OCP\IDBConnection')
->disableOriginalConstructor()
->getMock();
$dbConnectionMock->expects($this->any())
->method('getQueryBuilder')
->willReturn($query);
$releaseNotesMock = $this->getMockBuilder('OC\ReleaseNotes')
->setConstructorArgs([$dbConnectionMock])
->setMethods(['isMysql', 'isCliMode', 'countFilecacheEntries'])
->getMock();
$releaseNotesMock->expects($this->any())
->method('isMysql')
->willReturn($isMysql);
$releaseNotesMock->expects($this->any())
->method('isCliMode')
->willReturn($isCliMode);
$releaseNotesMock->expects($this->any())
->method('countFilecacheEntries')
->willReturn($fileCount);
return $releaseNotesMock;
}
public function data82to90() {
$alterTableMessage = 'Hint: You can speed up the upgrade by executing this SQL command manually: ALTER TABLE ocx_filecache ADD COLUMN checksum varchar(255) DEFAULT NULL AFTER permissions;';
$useCliMessage = 'You have an ownCloud installation with over 200.000 files so the upgrade might take a while. The recommendation is to use the command-line instead of the web interface for big ownCloud servers.';
return [
[[], false, false, 20],
[[], false, true, 20],
[[], true, false, 20],
[[], true, true, 20],
[[$useCliMessage], false, false, 1000000],
[[], false, true, 1000000],
[[$useCliMessage, $alterTableMessage], true, false, 1000000],
[[$alterTableMessage], true, true, 1000000],
];
}
/**
* @dataProvider data82to90
*
* @param string[] $expected
* @param bool $isMysql
* @param bool $isCliMode
* @param int $fileCount
*/
public function test82to90($expected, $isMysql, $isCliMode, $fileCount) {
$releaseNotesMock = $this->getReleaseNotesMock($isMysql, $isCliMode, $fileCount);
$actual = $releaseNotesMock->getReleaseNotes('8.2.22', '9.0.1');
$this->assertEquals($expected, $actual);
}
public function data90to91() {
return [
[false, false, 20],
[false, true, 20],
[true, false, 20],
[true, true, 20],
[false, false, 1000000],
[false, true, 1000000],
[true, false, 1000000],
[true, true, 1000000],
];
}
/**
* @dataProvider data90to91
*
* @param bool $isMysql
* @param bool $isCliMode
* @param int $fileCount
*/
public function test90to91($isMysql, $isCliMode, $fileCount) {
$releaseNotesMock = $this->getReleaseNotesMock($isMysql, $isCliMode, $fileCount);
$actual = $releaseNotesMock->getReleaseNotes('9.0.1', '9.1.0');
$this->assertCount(0, $actual);
}
}