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 a61fa5b526
commit d32b35fc7f
3 changed files with 34 additions and 11 deletions

View File

@ -108,9 +108,12 @@ class Application extends App {
$container->registerService('MigrateCalendars', function($c) { $container->registerService('MigrateCalendars', function($c) {
/** @var IAppContainer $c */ /** @var IAppContainer $c */
$db = $c->getServer()->getDatabaseConnection(); $db = $c->getServer()->getDatabaseConnection();
$logger = $c->getServer()->getLogger();
return new MigrateCalendars( return new MigrateCalendars(
new CalendarAdapter($db), 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\CalDavBackend;
use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\Calendar;
use Symfony\Component\Console\Command\Command; use OCP\ILogger;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class MigrateCalendars { class MigrateCalendars {
@ -36,15 +34,25 @@ class MigrateCalendars {
/** @var CalDavBackend */ /** @var CalDavBackend */
private $backend; private $backend;
/** @var ILogger */
private $logger;
/** @var OutputInterface */
private $consoleOutput;
/** /**
* @param CalendarAdapter $adapter * @param CalendarAdapter $adapter
* @param CalDavBackend $backend * @param CalDavBackend $backend
*/ */
function __construct(CalendarAdapter $adapter, function __construct(CalendarAdapter $adapter,
CalDavBackend $backend CalDavBackend $backend,
ILogger $logger,
OutputInterface $consoleOutput = null
) { ) {
$this->adapter = $adapter; $this->adapter = $adapter;
$this->backend = $backend; $this->backend = $backend;
$this->logger = $logger;
$this->consoleOutput = $consoleOutput;
} }
/** /**
@ -82,7 +90,17 @@ class MigrateCalendars {
*/ */
private function migrateCalendar($calendarId, $newCalendarId) { private function migrateCalendar($calendarId, $newCalendarId) {
$this->adapter->foreachCalendarObject($calendarId, function($calObject) use ($newCalendarId) { $this->adapter->foreachCalendarObject($calendarId, function($calObject) use ($newCalendarId) {
$this->backend->createCalendarObject($newCalendarId, $calObject['uri'], $calObject['calendardata']); 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\CalDAV\CalDavBackend;
use OCA\Dav\Migration\CalendarAdapter; use OCA\Dav\Migration\CalendarAdapter;
use OCP\ILogger;
use Test\TestCase; use Test\TestCase;
class MigrateCalendarTest extends TestCase { class MigrateCalendarTest extends TestCase {
@ -35,15 +36,17 @@ class MigrateCalendarTest extends TestCase {
$cardDav->method('createCalendar')->willReturn(666); $cardDav->method('createCalendar')->willReturn(666);
$cardDav->expects($this->once())->method('createCalendar')->with('principals/users/test01', 'test_contacts'); $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'); $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'); $m->migrateForUser('test01');
} }
/** /**
* @return \PHPUnit_Framework_MockObject_MockObject * @return \PHPUnit_Framework_MockObject_MockObject
*/ */
private function mockAdapter($shares = []) { private function mockAdapter($shares = [], $calData = 'BEGIN:VCARD') {
$adapter = $this->getMockBuilder('\OCA\Dav\Migration\CalendarAdapter') $adapter = $this->getMockBuilder('\OCA\Dav\Migration\CalendarAdapter')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
@ -62,15 +65,14 @@ class MigrateCalendarTest extends TestCase {
'components' => 'VEVENT,VTODO,VJOURNAL' 'components' => 'VEVENT,VTODO,VJOURNAL'
]); ]);
}); });
$adapter->method('foreachCalendarObject')->willReturnCallback(function ($addressBookId, \Closure $callBack) { $adapter->method('foreachCalendarObject')->willReturnCallback(function ($addressBookId, \Closure $callBack) use ($calData) {
$callBack([ $callBack([
'userid' => $addressBookId, 'userid' => $addressBookId,
'uri' => '63f0dd6c-39d5-44be-9d34-34e7a7441fc2.ics', 'uri' => '63f0dd6c-39d5-44be-9d34-34e7a7441fc2.ics',
'calendardata' => 'BEGIN:VCARD' 'calendardata' => $calData
]); ]);
}); });
$adapter->method('getShares')->willReturn($shares); $adapter->method('getShares')->willReturn($shares);
return $adapter; return $adapter;
} }
} }