public function testLoadsBlackoutSeriesByBlackoutId() { $id = 19191; $seriesId = 110101; $ownerId = 919199; $title = 'title'; $description = 'description'; $tz = 'UTC'; $repeatType = RepeatType::Daily; $repeatDaily = new RepeatDaily(1, Date::Parse('2013-04-15', $tz)); $b1Start = Date::Parse('2013-04-14 12:30:00', $tz); $b1End = Date::Parse('2013-04-14 13:30:00', $tz); $series = new BlackoutSeriesRow(); $series->With($seriesId, $ownerId, $title, $repeatType, $repeatDaily->ConfigurationString(), $b1Start, $b1End); $instances = new BlackoutInstanceRow(); $instances->With($seriesId, 1, $b1Start->ToDatabase(), $b1End->ToDatabase()); $instances->With($seriesId, 2, Date::Parse('2013-04-15 12:30:00', $tz)->ToDatabase(), Date::Parse('2013-04-15 13:30:00', $tz)->ToDatabase()); $resources = new BlackoutResourceRow(); $resources->With(1, 'r1', 2, 3, 4); $resources->With(2, 'r2', 4); $this->db->SetRow(0, $series->Rows()); $this->db->SetRow(1, $instances->Rows()); $this->db->SetRow(2, $resources->Rows()); $loadBlackoutCommand = new GetBlackoutSeriesByBlackoutIdCommand($id); $loadBlackoutInstancesCommand = new GetBlackoutInstancesCommand($seriesId); $loadBlackoutResourcesCommand = new GetBlackoutResourcesCommand($seriesId); $series = $this->repository->LoadByBlackoutId($id); $this->assertEquals($loadBlackoutCommand, $this->db->_Commands[0]); $this->assertTrue($this->db->ContainsCommand($loadBlackoutInstancesCommand)); $this->assertTrue($this->db->ContainsCommand($loadBlackoutResourcesCommand)); $this->assertEquals($seriesId, $series->Id()); $this->assertEquals($ownerId, $series->OwnerId()); $this->assertEquals($title, $series->Title()); $this->assertEquals($repeatType, $series->RepeatType()); $this->assertEquals($repeatDaily->ConfigurationString(), $series->RepeatConfigurationString()); $this->assertEquals(2, count($series->AllBlackouts())); $instances = $series->AllBlackouts(); $this->assertEquals($b1Start, $instances[0]->StartDate()); $this->assertEquals($b1End, $instances[0]->EndDate()); $this->assertEquals(count($resources->Rows()), count($series->Resources())); $this->assertEquals($b1Start, $series->CurrentBlackout()->StartDate()); }
private function MigrateReservations(Database $legacyDatabase, Database $currentDatabase) { $reservationsMigrated = MigrationSession::GetLastReservationRow(); $getMigratedCount = new AdHocCommand('SELECT (select count(*) from reservation_series where legacyid is not null) + (select count(*) from blackout_series where legacyid is not null ) as count'); $reader = ServiceLocator::GetDatabase()->Query($getMigratedCount); if ($row = $reader->GetRow()) { $reservationsMigrated = $row['count']; } Log::Debug('Start migrating reservations. Starting at row %s', $reservationsMigrated); $reservationRepository = new ReservationRepository(); $blackoutRepository = new BlackoutRepository(); $getLegacyReservations = new AdHocCommand("select r.resid, machid, scheduleid, start_date, end_date,\n starttime, endtime, created, modified, parentid, is_blackout, is_pending, summary, allow_participation, allow_anon_participation,\n ru.memberid\n FROM reservations r INNER JOIN reservation_users ru ON r.resid = ru.resid AND owner = 1\n ORDER BY r.resid LIMIT {$reservationsMigrated}, 100"); $getExisting = new AdHocCommand('select legacyid from reservation_series'); $reader = $currentDatabase->Query($getExisting); $knownIds = array(); while ($row = $reader->GetRow()) { $knownIds[] = $row['legacyid']; } $getLegacyReservationAccessories = new AdHocCommand('SELECT resid, resourceid from reservation_resources'); $getLegacyReservationParticipants = new AdHocCommand('SELECT resid, memberid, owner, invited FROM reservation_users WHERE (owner is null or owner = 0)'); $getAccessoryMapping = new AdHocCommand('select accessory_id, legacyid from accessories'); $getUserMapping = new AdHocCommand('select user_id, legacyid from users'); $getResourceMapping = new AdHocCommand('select resource_id, legacyid from resources'); $accessoryMapping = array(); $accessoryMappingReader = $currentDatabase->Query($getAccessoryMapping); while ($row = $accessoryMappingReader->GetRow()) { $legacyId = $row['legacyid']; $accessoryMapping[$legacyId] = $row['accessory_id']; } $userMapping = array(); $userMappingReader = $currentDatabase->Query($getUserMapping); while ($row = $userMappingReader->GetRow()) { $legacyId = $row['legacyid']; $userMapping[$legacyId] = $row['user_id']; } $resourceMapping = array(); $resourceMappingReader = $currentDatabase->Query($getResourceMapping); while ($row = $resourceMappingReader->GetRow()) { $legacyId = $row['legacyid']; $resourceMapping[$legacyId] = $row['resource_id']; } $reservationAccessories = array(); $legacyAccessoryReader = $legacyDatabase->Query($getLegacyReservationAccessories); while ($row = $legacyAccessoryReader->GetRow()) { $resId = $row['resid']; if (!array_key_exists($resId, $reservationAccessories)) { $reservationAccessories[$resId] = array(); } $reservationAccessories[$resId][] = $row['resourceid']; } $reservationParticipants = array(); $legacyParticipantReader = $legacyDatabase->Query($getLegacyReservationParticipants); while ($row = $legacyParticipantReader->GetRow()) { $resId = $row['resid']; if (!array_key_exists($resId, $reservationParticipants)) { $reservationParticipants[$resId] = array(); } $reservationParticipants[$resId][] = array('id' => $row['memberid'], 'invited' => $row['invited']); } $legacyReservationReader = $legacyDatabase->Query($getLegacyReservations); while ($row = $legacyReservationReader->GetRow()) { $legacyId = $row['resid']; if (in_array($legacyId, $knownIds)) { continue; } $date = $this->BuildDateRange($row['start_date'], $row['starttime'], $row['end_date'], $row['endtime']); $mappedUserId = $userMapping[$row['memberid']]; $mappedResourceId = $resourceMapping[$row['machid']]; if ($row['is_blackout'] == 1) { // handle blackout $blackout = BlackoutSeries::Create($mappedUserId, '', $date); $blackout->AddResourceId($mappedResourceId); $newId = $blackoutRepository->Add($blackout); $currentDatabase->Execute(new AdHocCommand("update blackout_series set legacyid = \"{$legacyId}\" where blackout_series_id = {$newId}")); } else { // handle reservation $mappedParticipantIds = array(); $mappedInviteeIds = array(); if (array_key_exists($legacyId, $reservationParticipants)) { $legacyParticipants = $reservationParticipants[$legacyId]; foreach ($legacyParticipants as $legacyParticipantId) { $userId = $userMapping[$legacyParticipantId['id']]; if (empty($legacyParticipantId['invited'])) { $mappedParticipantIds[] = $userId; } else { $mappedInviteeIds[] = $userId; } } } $mappedAccessoryList = array(); if (array_key_exists($legacyId, $reservationAccessories)) { $legacyAccessories = $reservationAccessories[$legacyId]; foreach ($legacyAccessories as $legacyAccessoryId) { if (array_key_exists($legacyAccessoryId, $accessoryMapping)) { $mappedAccessoryId = $accessoryMapping[$legacyAccessoryId]; $mappedAccessoryList[] = new ReservationAccessory($mappedAccessoryId, 1); } } } $currentUser = new UserSession($row['memberid']); $currentUser->Timezone = Configuration::Instance()->GetDefaultTimezone(); $mappedResource = new MigrateBookableResource($mappedResourceId); $reservation = ReservationSeries::Create($mappedUserId, $mappedResource, '', $row['summary'], $date, new RepeatNone(), $currentUser); foreach ($mappedAccessoryList as $accessory) { $reservation->AddAccessory($accessory); } $reservation->ChangeParticipants($mappedParticipantIds); $reservation->ChangeInvitees($mappedInviteeIds); try { $reservationRepository->Add($reservation); $newId = $reservation->SeriesId(); $currentDatabase->Execute(new AdHocCommand("update reservation_series set legacyid = \"{$legacyId}\" where series_id = {$newId}")); } catch (Exception $ex) { Log::Error('Error migrating reservation %s. Exception: %s', $legacyId, $ex); } } $reservationsMigrated++; MigrationSession::SetLastReservationRow($reservationsMigrated); } Log::Debug('Done migrating reservations (%s reservations)', $reservationsMigrated); $getLegacyCount = new AdHocCommand('select count(*) as count from reservations'); // $getMigratedCount = new AdHocCommand('SELECT // (select count(*) from reservation_series where legacyid is not null) + // (select count(*) from blackout_series where legacyid is not null ) // as count'); $progressCounts = $this->GetProgressCounts($getLegacyCount, $getMigratedCount); $this->page->SetProgress($progressCounts); Log::Debug('There are %s total legacy reservations and %s already migrated. Progress is %s', $progressCounts->LegacyCount, $progressCounts->MigratedCount, $progressCounts->PercentComplete); $this->page->SetReservationsMigrated($progressCounts->MigratedCount); MigrationSession::SetLastReservationRow($progressCounts->MigratedCount); }