Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate Migrator::executeSql to OCP\EventDispatcher\Event
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Aug 25, 2022
commit a83a8f0dde07bff67e0ceb7008cc26b3fad32516
48 changes: 25 additions & 23 deletions core/Command/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
*/
namespace OC\Core\Command;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\Util;
use OC\Console\TimestampFormatter;
use OC\DB\MigratorExecuteSqlEvent;
use OC\Installer;
use OC\Updater;
use OCP\IConfig;
use OCP\Util;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -93,28 +95,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);

$dispatcher = \OC::$server->getEventDispatcher();
/** @var IEventDispatcher $newDispatcher */
$newDispatcher = \OC::$server->get(IEventDispatcher::class);
$progress = new ProgressBar($output);
$progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%");
$listener = function ($event) use ($progress, $output) {
if ($event instanceof GenericEvent) {
$message = $event->getSubject();
if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln(' Checking table ' . $message);
} else {
if (strlen($message) > 60) {
$message = substr($message, 0, 57) . '...';
}
$progress->setMessage($message);
if ($event['step'] === 1) {
$output->writeln('');
$progress->start($event['max']);
}
$progress->setProgress($event['step']);
if ($event['step'] === $event['max']) {
$progress->setMessage('Done');
$progress->finish();
$output->writeln('');
}
$listener = function (MigratorExecuteSqlEvent $event) use ($progress, $output) {
$message = $event->getSql();
if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) {
$output->writeln(' Executing SQL ' . $message);
} else {
if (strlen($message) > 60) {
$message = substr($message, 0, 57) . '...';
}
$progress->setMessage($message);
if ($event->getCurrentStep() === 1) {
$output->writeln('');
$progress->start($event->getMaxStep());
}
$progress->setProgress($event->getCurrentStep());
if ($event->getCurrentStep() === $event->getMaxStep()) {
$progress->setMessage('Done');
$progress->finish();
$output->writeln('');
}
}
};
Expand Down Expand Up @@ -161,7 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
};

$dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener);
$newDispatcher->addListener(MigratorExecuteSqlEvent::class, $listener);
$dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
$dispatcher->addListener('\OC\Repair::advance', $repairListener);
$dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
Expand Down
10 changes: 6 additions & 4 deletions core/ajax/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IEventSource;
use OCP\IL10N;
use OCP\ILogger;
use OC\DB\MigratorExecuteSqlEvent;
use Symfony\Component\EventDispatcher\GenericEvent;

if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
Expand Down Expand Up @@ -123,10 +125,10 @@ public function handleRepairFeedback($event): void {
$incompatibleApps = [];

$dispatcher = \OC::$server->getEventDispatcher();
$dispatcher->addListener('\OC\DB\Migrator::executeSql', function ($event) use ($eventSource, $l) {
if ($event instanceof GenericEvent) {
$eventSource->send('success', $l->t('[%d / %d]: %s', [$event['step'], $event['max'], $event->getSubject()]));
}
/** @var IEventDispatcher $newDispatcher */
$newDispatcher = \OC::$server->get(IEventDispatcher::class);
$newDispatcher->addListener(MigratorExecuteSqlEvent::class, function (MigratorExecuteSqlEvent $event) use ($eventSource, $l) {
$eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
});
$feedBack = new FeedBackHandler($eventSource, $l);
$dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']);
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@
'OC\\DB\\MigrationException' => $baseDir . '/lib/private/DB/MigrationException.php',
'OC\\DB\\MigrationService' => $baseDir . '/lib/private/DB/MigrationService.php',
'OC\\DB\\Migrator' => $baseDir . '/lib/private/DB/Migrator.php',
'OC\\DB\\MigratorExecuteSqlEvent' => $baseDir . '/lib/private/DB/MigratorExecuteSqlEvent.php',
'OC\\DB\\MissingColumnInformation' => $baseDir . '/lib/private/DB/MissingColumnInformation.php',
'OC\\DB\\MissingIndexInformation' => $baseDir . '/lib/private/DB/MissingIndexInformation.php',
'OC\\DB\\MissingPrimaryKeyInformation' => $baseDir . '/lib/private/DB/MissingPrimaryKeyInformation.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\DB\\MigrationException' => __DIR__ . '/../../..' . '/lib/private/DB/MigrationException.php',
'OC\\DB\\MigrationService' => __DIR__ . '/../../..' . '/lib/private/DB/MigrationService.php',
'OC\\DB\\Migrator' => __DIR__ . '/../../..' . '/lib/private/DB/Migrator.php',
'OC\\DB\\MigratorExecuteSqlEvent' => __DIR__ . '/../../..' . '/lib/private/DB/MigratorExecuteSqlEvent.php',
'OC\\DB\\MissingColumnInformation' => __DIR__ . '/../../..' . '/lib/private/DB/MissingColumnInformation.php',
'OC\\DB\\MissingIndexInformation' => __DIR__ . '/../../..' . '/lib/private/DB/MissingIndexInformation.php',
'OC\\DB\\MissingPrimaryKeyInformation' => __DIR__ . '/../../..' . '/lib/private/DB/MissingPrimaryKeyInformation.php',
Expand Down
12 changes: 5 additions & 7 deletions lib/private/DB/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use OCP\IConfig;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use function preg_match;
use OCP\EventDispatcher\IEventDispatcher;

class Migrator {

Expand All @@ -49,15 +48,14 @@ class Migrator {
/** @var IConfig */
protected $config;

/** @var ?EventDispatcherInterface */
private $dispatcher;
private ?IEventDispatcher $dispatcher;

/** @var bool */
private $noEmit = false;

public function __construct(Connection $connection,
IConfig $config,
?EventDispatcherInterface $dispatcher = null) {
?IEventDispatcher $dispatcher = null) {
$this->connection = $connection;
$this->config = $config;
$this->dispatcher = $dispatcher;
Expand Down Expand Up @@ -183,13 +181,13 @@ protected function getFilterExpression() {
return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/';
}

protected function emit($sql, $step, $max) {
protected function emit(string $sql, int $step, int $max): void {
if ($this->noEmit) {
return;
}
if (is_null($this->dispatcher)) {
return;
}
$this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, ['step' => $step + 1, 'max' => $max]));
$this->dispatcher->dispatchTyped(new MigratorExecuteSqlEvent($sql, $step, $max));
}
}
52 changes: 52 additions & 0 deletions lib/private/DB/MigratorExecuteSqlEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (c) 2022 Côme Chilliet <[email protected]>
*
* @author Côme Chilliet <[email protected]>
*
* @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 OC\DB;

use OCP\EventDispatcher\Event;

class MigratorExecuteSqlEvent extends Event {
private string $sql;
private int $current;
private int $max;

public function __construct(
string $sql,
int $current,
int $max,
) {
$this->sql = $sql;
$this->current = $current;
$this->max = $max;
}

public function getSql(): string {
return $this->sql;
}

public function getCurrentStep(): int {
return $this->current;
}

public function getMaxStep(): int {
return $this->max;
}
}
12 changes: 7 additions & 5 deletions lib/private/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,14 @@ private function logAllEvents(): void {
$log = $this->log;

$dispatcher = \OC::$server->getEventDispatcher();
$dispatcher->addListener('\OC\DB\Migrator::executeSql', function ($event) use ($log) {
if (!$event instanceof GenericEvent) {
return;
/** @var IEventDispatcher $newDispatcher */
$newDispatcher = \OC::$server->get(IEventDispatcher::class);
$newDispatcher->addListener(
MigratorExecuteSqlEvent::class,
function (MigratorExecuteSqlEvent $event) use ($log) {
$log->info(get_class($event).': ' . $event->getSql() . ' (' . $event->getCurrentStep() . ' of ' . $event->getMaxStep() . ')', ['app' => 'updater']);
}
$log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument('step') . ' of ' . $event->getArgument('max') . ')', ['app' => 'updater']);
});
);

$repairListener = function ($event) use ($log) {
if (!$event instanceof GenericEvent) {
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/DB/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function setUp(): void {
private function getMigrator(): Migrator {
$platform = $this->connection->getDatabasePlatform();
$random = \OC::$server->getSecureRandom();
$dispatcher = \OC::$server->getEventDispatcher();
$dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
if ($platform instanceof SqlitePlatform) {
return new SQLiteMigrator($this->connection, $this->config, $dispatcher);
} elseif ($platform instanceof OraclePlatform) {
Expand Down