From 79736eb1cf6abacb2b79c8b8f8ca52d1cfcc6715 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 25 Nov 2015 19:52:04 +0100 Subject: fix tests --- README.md | 12 +++++-- appinfo/register_command.php | 4 +-- appinfo/routes.php | 1 + command/generatesearchindices.php | 47 -------------------------- command/migrate.php | 48 +++++++++++++++++++++++++++ controller/admincontroller.php | 20 ++++++++--- css/admin.css | 6 +++- js/admin/Admin.js | 13 +++++++- service/itemservice.php | 23 ++++++++----- templates/admin.php | 15 ++++++++- tests/integration/command/CommandTest.php | 4 +-- tests/unit/controller/AdminControllerTest.php | 16 ++++++++- tests/unit/fetcher/FeedFetcherTest.php | 2 +- 13 files changed, 139 insertions(+), 72 deletions(-) delete mode 100644 command/generatesearchindices.php create mode 100644 command/migrate.php diff --git a/README.md b/README.md index 79587fe48..6f8fc28c4 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,13 @@ We switched to a different feed parsing library which creates article ids differ 5.3.0 adds the possibility to search your articles. To do this efficiently however, the News app needs to generate an index. This is done automatically for new articles, but older articles need to be migrated. Because large installations have millions of articles, generating the search index has been offloaded to a separate command to prevent timeouts when upgrading the app. To make your old articles searchable run this command in your ownCloud top directory: - ./occ news:create-search-indices + ./occ news:migrate + +### Updating from versions prior to 7 + +Version 7 adds article deduplication. In order for this to work properly, database columns need to be generated. Since this can time out + + ./occ news:migrate ## FAQ @@ -188,9 +194,9 @@ The following commands are available when using the **occ** file in the top dire ./occ -* **Generate search indices**: +* **Migrate older data (only needed for certain versions, see Updating section)**: - ./occ news:create-search-indices + ./occ news:migrate Translations ------------ diff --git a/appinfo/register_command.php b/appinfo/register_command.php index dc6b4fe31..91e379525 100644 --- a/appinfo/register_command.php +++ b/appinfo/register_command.php @@ -9,12 +9,12 @@ * @copyright Bernhard Posselt 2015 */ -use OCA\News\Command\GenerateSearchIndices; +use OCA\News\Command\Migrate; use OCA\News\Command\VerifyInstall; $newsApp = new OCA\News\AppInfo\Application(); $newsContainer = $newsApp->getContainer(); -$newsCmd = $newsContainer->query(GenerateSearchIndices::class); +$newsCmd = $newsContainer->query(Migrate::class); $verifyCmd = $newsContainer->query(VerifyInstall::class); $application->add($newsCmd); diff --git a/appinfo/routes.php b/appinfo/routes.php index 979ca3dac..9ebee93ef 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,6 +21,7 @@ return ['routes' => [ // admin ['name' => 'admin#update', 'url' => '/admin', 'verb' => 'PUT'], +['name' => 'admin#migrate', 'url' => '/admin/migrate', 'verb' => 'POST'], // folders ['name' => 'folder#index', 'url' => '/folders', 'verb' => 'GET'], diff --git a/command/generatesearchindices.php b/command/generatesearchindices.php deleted file mode 100644 index 4bc41d04b..000000000 --- a/command/generatesearchindices.php +++ /dev/null @@ -1,47 +0,0 @@ - - * @copyright Bernhard Posselt 2015 - */ - -namespace OCA\News\Command; - -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Helper\ProgressBar; - -use OCA\News\Service\ItemService; - - -class GenerateSearchIndices extends Command { - - private $service; - - public function __construct(ItemService $service) { - parent::__construct(); - $this->service = $service; - } - - protected function configure() { - $this->setName('news:create-search-indices') - ->setDescription('Recreates the search indices for all items'); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $output->writeln( - "\nCreating search indices, this could take a while...\n" - ); - $progressbar = function ($steps) use ($output) { - return new ProgressBar($output, $steps); - }; - $this->service->generateSearchIndicies($progressbar); - $output->writeln("\n"); - } - -} diff --git a/command/migrate.php b/command/migrate.php new file mode 100644 index 000000000..364ceda42 --- /dev/null +++ b/command/migrate.php @@ -0,0 +1,48 @@ + + * @copyright Bernhard Posselt 2015 + */ + +namespace OCA\News\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Helper\ProgressBar; + +use OCA\News\Service\ItemService; + + +class Migrate extends Command { + + private $service; + + public function __construct(ItemService $service) { + parent::__construct(); + $this->service = $service; + } + + protected function configure() { + $this->setName('news:migrate') + ->setDescription('Migrates the database schema. Needed when ' . + 'updating from versions prior to: 7.0.0'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $output->writeln( + "\nMigrating data, this could take a while...\n" + ); + $progressbar = function ($steps) use ($output) { + return new ProgressBar($output, $steps); + }; + $this->service->generateSearchIndices($progressbar); + $output->writeln("\n"); + } + +} diff --git a/controller/admincontroller.php b/controller/admincontroller.php index 7c9ccf7f2..3d8b272ba 100644 --- a/controller/admincontroller.php +++ b/controller/admincontroller.php @@ -13,22 +13,25 @@ namespace OCA\News\Controller; -use \OCP\AppFramework\Http\TemplateResponse; -use \OCP\IRequest; -use \OCP\AppFramework\Controller; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IRequest; +use OCP\AppFramework\Controller; -use \OCA\News\Config\Config; +use OCA\News\Config\Config; +use OCA\News\Service\itemService; class AdminController extends Controller { private $config; private $configPath; + private $itemService; public function __construct($AppName, IRequest $request, Config $config, - $configFile){ + ItemService $itemService, $configFile){ parent::__construct($AppName, $request); $this->config = $config; $this->configPath = $configFile; + $this->itemService = $itemService; } // There are no checks for the index method since the output is rendered @@ -82,5 +85,12 @@ class AdminController extends Controller { ]; } + /** + * Generates indices + */ + public function migrate() { + $this->itemService->generateSearchIndices(); + return []; + } } diff --git a/css/admin.css b/css/admin.css index 8571bc860..518508ffb 100644 --- a/css/admin.css +++ b/css/admin.css @@ -5,4 +5,8 @@ #news .form-line { margin: 25px 0 15px 0; -} \ No newline at end of file +} + +#news .notice { + color: red; +} diff --git a/js/admin/Admin.js b/js/admin/Admin.js index 823d6b0ab..93b5f0268 100644 --- a/js/admin/Admin.js +++ b/js/admin/Admin.js @@ -87,7 +87,18 @@ $('#news input[type="text"]').blur(submit); $('#news input[type="checkbox"]').change(submit); + $('#news-migrate').click(function () { + var button = $(this); + button.addClass('loading'); + + $.post(OC.generateUrl('/apps/news/admin/migrate')) + .always(function (data) { + button.removeClass('loading'); + }); + + return false; + }); }); -}(window, document, jQuery)); \ No newline at end of file +}(window, document, jQuery)); diff --git a/service/itemservice.php b/service/itemservice.php index 6f454dbf5..0b0746258 100644 --- a/service/itemservice.php +++ b/service/itemservice.php @@ -259,23 +259,30 @@ class ItemService extends Service { /** * Regenerates the search index for all items */ - public function generateSearchIndicies($progressbar) { - $this->systemConfig->setSystemValue('maintenance', true); - + public function generateSearchIndices($progressbar=null) { + if ($progressbar) { + $this->systemConfig->setSystemValue('maintenance', true); + $progressbar = $progressbar(count($rows)); + $progressbar->setFormat('verbose'); + } + $rows = $this->itemMapper->findAllItemIdsAndUsers(); - $progressbar = $progressbar(count($rows)); - $progressbar->setFormat('verbose'); foreach ($rows as $row) { $item = $this->find($row['id'], $row['user_id']); $item->generateSearchIndex(); $this->itemMapper->update($item); - $progressbar->advance(); + + if ($progressbar) { + $progressbar->advance(); + } } - $progressbar->finish(); + if ($progressbar) { + $progressbar->finish(); + $this->systemConfig->setSystemValue('maintenance', false); + } - $this->systemConfig->setSystemValue('maintenance', false); } } diff --git a/templates/admin.php b/templates/admin.php index 9f2a815ac..f4eac5089 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -122,7 +122,20 @@ style('news', 'admin');

+
+

+ +

+

t('Migrates existing data after updating from versions prior to 7.0.0')); ?>

+

t( + 'This takes about 1 minute per user. For bigger installations use ' . + 'this console command to prevent timeouts:')); ?> php -f owncloud/occ news:migrate +

+

+
t('Saved')); ?>
- \ No newline at end of file + diff --git a/tests/integration/command/CommandTest.php b/tests/integration/command/CommandTest.php index 4501f3b95..72d7602ff 100644 --- a/tests/integration/command/CommandTest.php +++ b/tests/integration/command/CommandTest.php @@ -17,8 +17,8 @@ class CommandTest extends \PHPUnit_Framework_TestCase { $this->corePath = __DIR__ . '/../../../../../'; } - public function testGenerateIndices() { - $command = $this->corePath . 'occ news:create-search-indices'; + public function testMigrate() { + $command = $this->corePath . 'occ news:migrate'; exec($command, $_, $success); $this->assertSame(0, $success); diff --git a/tests/unit/controller/AdminControllerTest.php b/tests/unit/controller/AdminControllerTest.php index d86c4cf98..c74b8570e 100644 --- a/tests/unit/controller/AdminControllerTest.php +++ b/tests/unit/controller/AdminControllerTest.php @@ -21,6 +21,7 @@ class AdminControllerTest extends \PHPUnit_Framework_TestCase { private $controller; private $config; private $configPath; + private $itemService; /** * Gets run before each test @@ -35,9 +36,14 @@ class AdminControllerTest extends \PHPUnit_Framework_TestCase { '\OCA\News\Config\Config') ->disableOriginalConstructor() ->getMock(); + $this->itemService = $this->getMockBuilder( + '\OCA\News\Service\ItemService') + ->disableOriginalConstructor() + ->getMock(); + $this->configPath = 'my.ini'; $this->controller = new AdminController($this->appName, $this->request, - $this->config, $this->configPath); + $this->config, $this->itemService, $this->configPath); } @@ -151,4 +157,12 @@ class AdminControllerTest extends \PHPUnit_Framework_TestCase { $this->assertEquals($expected, $response); } + + public function testMigrate() { + $this->itemService->expects($this->once()) + ->method('generateSearchIndices'); + $this->controller->migrate(); + } + + } diff --git a/tests/unit/fetcher/FeedFetcherTest.php b/tests/unit/fetcher/FeedFetcherTest.php index 276a3e485..f6a03afa2 100644 --- a/tests/unit/fetcher/FeedFetcherTest.php +++ b/tests/unit/fetcher/FeedFetcherTest.php @@ -235,7 +235,7 @@ class FeedFetcherTest extends \PHPUnit_Framework_TestCase { $item->setEnclosureLink($this->enclosureLink); } $item->generateSearchIndex(); - + return $item; } -- cgit v1.2.3