Handle calendar migration issue by writing the faulty event to the log and continue

This commit is contained in:
Thomas Müller 2016-03-07 14:11:14 +01:00
parent b3b57621b7
commit 6413fffdcb
3 changed files with 34 additions and 11 deletions

View File

@ -108,9 +108,12 @@ class Application extends App {
$container->registerService('MigrateCalendars', function($c) {
/** @var IAppContainer $c */
$db = $c->getServer()->getDatabaseConnection();
$logger = $c->getServer()->getLogger();
return new MigrateCalendars(
new CalendarAdapter($db),
$c->query('CalDavBackend')
$c->query('CalDavBackend'),
$logger,
null
);
});

View File

@ -23,9 +23,7 @@ namespace OCA\Dav\Migration;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use OCP\ILogger;
use Symfony\Component\Console\Output\OutputInterface;
class MigrateCalendars {
@ -36,15 +34,25 @@ class MigrateCalendars {
/** @var CalDavBackend */
private $backend;
/** @var ILogger */
private $logger;
/** @var OutputInterface */
private $consoleOutput;
/**
* @param CalendarAdapter $adapter
* @param CalDavBackend $backend
*/
function __construct(CalendarAdapter $adapter,
CalDavBackend $backend
CalDavBackend $backend,
ILogger $logger,
OutputInterface $consoleOutput = null
) {
$this->adapter = $adapter;
$this->backend = $backend;
$this->logger = $logger;
$this->consoleOutput = $consoleOutput;
}
/**
@ -82,7 +90,17 @@ class MigrateCalendars {
*/
private function migrateCalendar($calendarId, $newCalendarId) {
$this->adapter->foreachCalendarObject($calendarId, function($calObject) use ($newCalendarId) {
try {
$this->backend->createCalendarObject($newCalendarId, $calObject['uri'], $calObject['calendardata']);
} catch (\Exception $ex) {
$eventId = $calObject['id'];
$calendarId = $calObject['calendarId'];
$msg = "One event could not be migrated. (id: $eventId, calendarid: $calendarId)";
$this->logger->logException($ex, ['app' => 'dav', 'message' => $msg]);
if (!is_null($this->consoleOutput)) {
$this->consoleOutput->writeln($msg);
}
}
});
}

View File

@ -22,6 +22,7 @@ namespace OCA\DAV\Tests\Unit\Migration;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\Dav\Migration\CalendarAdapter;
use OCP\ILogger;
use Test\TestCase;
class MigrateCalendarTest extends TestCase {
@ -35,15 +36,17 @@ class MigrateCalendarTest extends TestCase {
$cardDav->method('createCalendar')->willReturn(666);
$cardDav->expects($this->once())->method('createCalendar')->with('principals/users/test01', 'test_contacts');
$cardDav->expects($this->once())->method('createCalendarObject')->with(666, '63f0dd6c-39d5-44be-9d34-34e7a7441fc2.ics', 'BEGIN:VCARD');
/** @var ILogger $logger */
$logger = $this->getMockBuilder('\OCP\ILogger')->disableOriginalConstructor()->getMock();
$m = new \OCA\Dav\Migration\MigrateCalendars($adapter, $cardDav);
$m = new \OCA\Dav\Migration\MigrateCalendars($adapter, $cardDav, $logger, null);
$m->migrateForUser('test01');
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function mockAdapter($shares = []) {
private function mockAdapter($shares = [], $calData = 'BEGIN:VCARD') {
$adapter = $this->getMockBuilder('\OCA\Dav\Migration\CalendarAdapter')
->disableOriginalConstructor()
->getMock();
@ -62,15 +65,14 @@ class MigrateCalendarTest extends TestCase {
'components' => 'VEVENT,VTODO,VJOURNAL'
]);
});
$adapter->method('foreachCalendarObject')->willReturnCallback(function ($addressBookId, \Closure $callBack) {
$adapter->method('foreachCalendarObject')->willReturnCallback(function ($addressBookId, \Closure $callBack) use ($calData) {
$callBack([
'userid' => $addressBookId,
'uri' => '63f0dd6c-39d5-44be-9d34-34e7a7441fc2.ics',
'calendardata' => 'BEGIN:VCARD'
'calendardata' => $calData
]);
});
$adapter->method('getShares')->willReturn($shares);
return $adapter;
}
}