/// <summary> /// Fixes scenarios/passes where the pass is referenced by multiple scenarios. /// </summary> public void Apply() { foreach (string tenantConnectionString in _tenantConnectionStrings) { using (IDocumentStore documentStore = DocumentStoreFactory.CreateStore(tenantConnectionString)) using (IDocumentSession session = documentStore.OpenSession()) using (var identityGenerator = new RavenIdentityGenerator(documentStore)) { // Get scenario/pass repositories var scenarioRepository = new RavenScenarioRepository(session); var passRepository = new RavenPassRepository(session); // Get all scenarios var scenarios = scenarioRepository.GetAll().OrderBy(s => s.CustomId); // Check each scenario and passes Dictionary <int, Guid> firstScenarioIdByPassId = new Dictionary <int, Guid>(); foreach (var scenario in scenarios) { bool scenarioChanged = false; // Check each pass foreach (var passRef in scenario.Passes) { if (firstScenarioIdByPassId.ContainsKey(passRef.Id)) // Another scenario references this pass { // Load pass var pass = passRepository.Get(passRef.Id); // Clone pass with new ID var newPass = (Pass)pass.Clone(); newPass.Id = identityGenerator.GetIdentities <PassIdIdentity>(1)[0].Id; newPass.Name = string.Format("Pass {0}", newPass.Id); passRef.Id = newPass.Id; // Link scenario to this pass passRepository.Add(newPass); scenarioRepository.Update(scenario); scenarioChanged = true; } else // No other scenarios reference this pass { firstScenarioIdByPassId.Add(passRef.Id, scenario.Id); } } // Save changes if (scenarioChanged) { passRepository.SaveChanges(); scenarioRepository.SaveChanges(); } } } } }
/// <summary> /// Added field IsLibraried for scenarios and passes /// </summary> public void Apply() { foreach (string tenantConnectionString in _tenantConnectionStrings) { using (IDocumentStore documentStore = DocumentStoreFactory.CreateStore(tenantConnectionString)) { using (IDocumentSession session = documentStore.OpenSession()) { var passRepository = new RavenPassRepository(session); var scenarioRepository = new RavenScenarioRepository(session); var runRepository = new RavenRunRepository(session); var passes = passRepository.GetAll().ToList(); var scenarios = scenarioRepository.GetAll().ToList(); var runs = runRepository.GetAll().ToList(); // Get scenarios linked to run var scenarioWithRunIds = new Dictionary <Guid, Guid>(); runs.ForEach(r => { r.Scenarios.ForEach(s => scenarioWithRunIds.Add(s.Id, r.Id)); }); var runScenarioIds = scenarioWithRunIds.Select(s => s.Key).ToList(); // Update field IsLibraried for scenarios not libked to run scenarios .Where(s => !runScenarioIds.Contains(s.Id)) .Distinct() .ToList() .ForEach(scenario => { scenario.IsLibraried = true; scenarioRepository.Update(scenario); }); scenarios .Where(s => s.IsLibraried == null || !s.IsLibraried.Value) .ToList() .ForEach(scenario => { scenario.IsLibraried = false; scenarioRepository.Update(scenario); }); // Get default scenario id // var defaultScenarioId = tenantSettingsRepository.Find(TenantSettings.DefaultId).DefaultScenarioId; // Get passes linked to scenario var scenarioWithPassIds = new Dictionary <int, Guid>(); scenarios.ForEach(s => { s.Passes.ForEach(p => { scenarioWithPassIds.Add(p.Id, s.Id); }); }); //// Get pass ids not linked to scenarios //var librariedPassIds = passes // .Select(pass => pass.Id) // .Except(scenarioWithPassIds // .Where(val => val.Value != defaultScenarioId) // .Select(s => s.Key)) // .Distinct() // .ToList(); var librariedPassIds = passes .Select(pass => pass.Id).Except(scenarioWithPassIds.Select(s => s.Key)) .Distinct() .ToList(); // Update field IsLibararied for passes passes .Where(pass => librariedPassIds.Contains(pass.Id)) .Distinct() .ToList() .ForEach(pass => { pass.IsLibraried = true; passRepository.Update(pass); }); passes .Where(p => p.IsLibraried == null || !p.IsLibraried.Value) .ToList() .ForEach(pass => { pass.IsLibraried = false; passRepository.Update(pass); }); scenarioRepository.SaveChanges(); passRepository.SaveChanges(); } } } }