private List <string> ApplyChangesToDatabase_Remove(List <ConceptApplication> toBeRemoved, List <ConceptApplication> oldApplications) { var newScripts = new List <string>(); toBeRemoved = toBeRemoved.OrderBy(ca => ca.OldCreationOrder).ToList(); // TopologicalSort is stable sort, so it will keep this (original) order unless current dependencies direct otherwise. Graph.TopologicalSort(toBeRemoved, ConceptApplication.GetDependencyPairs(oldApplications)); // Concept's dependencies might have changed, without dropping and recreating the concept. It is important to compute up-to-date remove order, otherwise FK constraint FK_AppliedConceptDependsOn_DependsOn might fail. toBeRemoved.Reverse(); int removedCACount = 0; foreach (var ca in toBeRemoved) { string[] removeSqlScripts = SplitSqlScript(ca.RemoveQuery); if (removeSqlScripts.Length > 0) { LogDatabaseChanges(ca, "Removing"); removedCACount++; } newScripts.AddRange(removeSqlScripts); newScripts.AddRange(_sqlTransactionBatches.JoinScripts(_conceptApplicationRepository.DeleteMetadataSql(ca))); newScripts.AddRange(MaybeCommitMetadataAfterDdl(removeSqlScripts)); } _logger.Info($"Removing {removedCACount} concept applications."); return(newScripts); }
private List <SqlBatchScript> ApplyChangesToDatabase_Remove(List <ConceptApplication> toBeRemoved, List <ConceptApplication> oldApplications) { var newScripts = new List <SqlBatchScript>(); toBeRemoved = toBeRemoved.OrderBy(ca => ca.OldCreationOrder).ToList(); // TopologicalSort is stable sort, so it will keep this (original) order unless current dependencies direct otherwise. Graph.TopologicalSort(toBeRemoved, ConceptApplication.GetDependencyPairs(oldApplications)); // Concept's dependencies might have changed, without dropping and recreating the concept. It is important to compute up-to-date remove order, otherwise FK constraint FK_AppliedConceptDependsOn_DependsOn might fail. toBeRemoved.Reverse(); int removedCACount = 0; foreach (var ca in toBeRemoved) { if (!string.IsNullOrWhiteSpace(ca.RemoveQuery)) { LogDatabaseChanges(ca, "Removing"); removedCACount++; } newScripts.Add(new SqlBatchScript { Sql = ca.RemoveQuery, IsBatch = true, Name = $"Removing {ca}." }); newScripts.AddRange(_sqlTransactionBatches.JoinScripts(_conceptApplicationRepository.DeleteMetadataSql(ca)) .Select((sql, x) => new SqlBatchScript { Sql = sql, IsBatch = false, Name = $"Removing {ca} metadata ({x + 1})." })); newScripts.AddRange(MaybeCommitMetadataAfterDdl(ca.RemoveQuery)); } _logger.Info($"Removing {removedCACount} database objects."); return(newScripts); }