protected override void OnExecute()
 {
     using (var scope = new DatabaseScope(context.Database.Name))
     {
         var existingIndices = indicesRepository.GetAll();
         foreach (var i in existingIndices)
         {
             var relation           = relationsRepository.Get(i.RelationID);
             var relationData       = new RelationData(relation);
             var attributes         = new List <AttributeData>();
             var includedAttributes = new List <AttributeData>();
             foreach (var atrributeName in i.AttributesNames)
             {
                 var attribute = attributesRepository.Get(relation.ID, atrributeName);
                 var toAdd     = new AttributeData(relationData, attribute);
                 if (IsIncludedAttribute(i.CreateDefinition, attribute.Name))
                 {
                     includedAttributes.Add(toAdd);
                 }
                 else
                 {
                     attributes.Add(toAdd);
                 }
             }
             var existingIndexToAdd = new IndexDefinition(Convert(i.AccessMethod), relationData, attributes, includedAttributes);
             if (context.StatementsData.AllQueriesByRelation.TryGetValue(relation.ID, out var possibleQueriesForIndex))
             {
                 foreach (var statementQueryPair in possibleQueriesForIndex)
                 {
                     var normalizedStatementID = statementQueryPair.NormalizedStatementID;
                     var normalizedStatement   = context.StatementsData.All[normalizedStatementID].NormalizedStatement;
                     var query         = statementQueryPair.Query;
                     var extractedData = context.StatementsExtractedData.DataPerQuery[statementQueryPair];
                     if (IndexApplicability.IsIndexApplicableForQuery(extractedData, existingIndexToAdd))
                     {
                         context.IndicesDesignData.ExistingIndices.TryAddPossibleIndices(new[] { existingIndexToAdd }, normalizedStatement, query);
                     }
                 }
             }
         }
     }
 }
 public bool TryGetPrimaryKey(uint relationId, out PrimaryKeyData primaryKey)
 {
     if (!primaryKeyPerRelation.ContainsKey(relationId))
     {
         using (var scope = new DatabaseScope(databaseName))
         {
             var            relation        = relationsRepository.Get(relationId);
             PrimaryKeyData primaryKeyToAdd = null;
             if (relation != null)
             {
                 List <AttributeData> primaryKeyAttributes = new List <AttributeData>();
                 if (relation.PrimaryKeyAttributeNames != null)
                 {
                     foreach (var attributeName in relation.PrimaryKeyAttributeNames)
                     {
                         primaryKeyAttributes.Add(new AttributeData(GetRelation(relationId), attributesRepository.Get(relationId, attributeName)));
                     }
                 }
                 if (primaryKeyAttributes.Count > 0)
                 {
                     primaryKeyToAdd = new PrimaryKeyData(primaryKeyAttributes);
                 }
             }
             primaryKeyPerRelation.Add(relationId, primaryKeyToAdd);
         }
     }
     primaryKey = primaryKeyPerRelation[relationId];
     return(primaryKey != null);
 }