/// <summary> /// Get update information from two lists of smo ojects with current types: /// Views, CLRAssemblies and UserDefinedFunction (and other databases components) /// </summary> /// <param name="productionItems">Objects from production database</param> /// <param name="sourceItems">Objects from source database</param> /// <param name="dropItems">Objects for drop from production database</param> /// <param name="createItems">Objects for create in production database</param> private static void DifferentItems( Dictionary <string, MySmoObjectBase> productionItems, Dictionary <string, MySmoObjectBase> sourceItems, ICollection <MySmoObjectBase> dropItems, ICollection <MySmoObjectBase> createItems) { // // Get dropped items from production database // foreach (string productionItemKey in productionItems.Keys) { MySmoObjectBase productionItem = productionItems[productionItemKey]; bool isContains = false; if (sourceItems.ContainsKey(productionItemKey)) { MySmoObjectBase sourceItem = sourceItems[productionItemKey]; isContains = true; if (!productionItem.IsEqual(sourceItem)) { createItems.Add(new MySmoObjectBase(sourceItem)); isContains = false; } } if (!isContains) { dropItems.Add(new MySmoObjectBase(productionItem)); } } // // Get created items from source database // foreach (string sourceItemKey in sourceItems.Keys) { MySmoObjectBase sourceItem = sourceItems[sourceItemKey]; if (!productionItems.ContainsKey(sourceItemKey)) { createItems.Add(new MySmoObjectBase(sourceItem)); } } }
/// <summary> /// Collect items from table: drop tables, create/drop triggers, create/drop checks, /// create/drop foreignKeys, create/drop/alter indexes and create/drop/alter columns /// </summary> /// <param name="createItems">List of items for creating</param> /// <param name="dropItems">List of items for deleting</param> /// <param name="alterItems">List of items for altering (not null only for column)</param> /// <param name="productionCollection">Collection of objects from production database</param> /// <param name="sourceCollection">Collection of objects from sourse database</param> /// <param name="productionTable">Production database (null for stored procedure)</param> private void DifferentSplitItems( ICollection <MySmoObjectBase> createItems, ICollection <MySmoObjectBase> dropItems, ICollection <MySmoObjectBase> alterItems, Dictionary <string, MySmoObjectBase> productionCollection, Dictionary <string, MySmoObjectBase> sourceCollection, Table productionTable) { #region Find object from production collection in source collection foreach (string productionObjectName in productionCollection.Keys) { MySmoObjectBase productionObject = productionCollection[productionObjectName]; // // if productionObject is split stored procedure - move to next object // if (productionTable == null && !string.IsNullOrEmpty( mDatabaseEngine.GetOriginalProcedureNameByTemplateProcedureName(productionObject.Name))) { continue; } bool isNeedDrop = true; if (sourceCollection.ContainsKey(productionObjectName)) { MySmoObjectBase sourceObject = sourceCollection[productionObjectName]; // // Compare these objects (own compare for every object) // if (sourceObject.IsEqual(productionObject)) { continue; } // // if our object is column - add it into alter collection // otherwise - into create and drop collection // if (alterItems != null) { var newMySmoObject = new MySmoObjectBase(sourceObject.CreateSplitItem(productionTable, 0, null)) { MProductionSmoObject = productionObject.SourceSmoObject }; alterItems.Add(newMySmoObject); isNeedDrop = false; } else { createItems.Add( productionTable != null ? new MySmoObjectBase(sourceObject.CreateSplitItem(productionTable, 0, null)) : new MySmoObjectBase(sourceObject.CreateSplitItem(this.mProductionDatabase, 0, null))); } #region Cycle for all split objects. We will create new split versions of object, if it have split versions if ((productionTable != null && Database.IsSplitTable(productionTable.Name)) || Database.IsSplitProcedure(sourceObject.Name)) { foreach (int surveyId in mDatabaseEngine.SurveyIds) { MySmoObjectBase splitObject; var splitTable = new Table(); if (productionTable != null) { // // Get split table // string splitTableName = Database.GetTemplateTableNameByOriginalTableName( productionTable.Name, surveyId); splitTable = mProductionDatabase.Tables[splitTableName]; // // Create split object on this table // splitObject = sourceObject.CreateSplitItem( splitTable, surveyId, mDatabaseEngine); } else { // // Create split stored procedure // splitObject = sourceObject.CreateSplitItem(mProductionDatabase, surveyId, mDatabaseEngine); } // // if our object is column - add it into alter collection // otherwise - into create collection // if (alterItems != null) { // // Find old version of this object in production database // var myTable = new MyTable(splitTable); Dictionary <string, MySmoObjectBase> templateCollection = myTable.GetMyObjects(splitObject); splitObject.MProductionSmoObject = templateCollection[splitObject.Name].SourceSmoObject; alterItems.Add(new MySmoObjectBase(splitObject)); } else { createItems.Add(new MySmoObjectBase(splitObject)); } } } #endregion } #region Add object and its split versions to drop collection if (isNeedDrop) { dropItems.Add(new MySmoObjectBase(productionObject)); // Add split objects to drop collection, if this table (stored procedure) // have split versions if ((productionTable != null && Database.IsSplitTable(productionTable.Name)) || Database.IsSplitProcedure(productionObject.Name)) { foreach (int surveyId in mDatabaseEngine.SurveyIds) { if (productionTable != null) { // // Get object collection from each split table // string splitTableName = Database.GetTemplateTableNameByOriginalTableName( productionTable.Name, surveyId); var myTable = new MyTable(mProductionDatabase.Tables[splitTableName]); Dictionary <string, MySmoObjectBase> templateCollection = myTable.GetMyObjects(productionObject); string splitObjectName = productionObject.Name; if (productionObject.SourceSmoObject.GetType() != typeof(Column)) { splitObjectName = Database.GetTemplateNameByOriginalName(productionObject.Name, surveyId); } // // Find our object from this collection and add it into drop collection // MySmoObjectBase splitObject = templateCollection[splitObjectName]; dropItems.Add(new MySmoObjectBase(splitObject)); } else { StoredProcedure splitProcedure = mProductionDatabase.StoredProcedures[Database.GetTemplateNameByOriginalName(productionObject.Name, surveyId)]; // If splitProcedure is (added just now) new procedure - // getting IsSystemObject parameter will throw exception try { if (!splitProcedure.IsSystemObject) { dropItems.Add(new MySmoObjectBase( splitProcedure, splitProcedure.Name, mProductionDatabase.Name)); } } catch (Microsoft.SqlServer.Management.Smo.PropertyNotSetException) { break; } } } } } #endregion } #endregion #region Add new objects from source database foreach (string sourceObjectName in sourceCollection.Keys) { MySmoObjectBase sourceObject = sourceCollection[sourceObjectName]; // // If source database contains new object - add it into create collection // if (!productionCollection.ContainsKey(sourceObjectName)) { if (productionTable != null) { var newObject = new MySmoObjectBase(sourceObject.CreateSplitItem(productionTable, 0, null)); // If column added to new table, we wount create new column separate, // it will create with table creating if (newObject.SourceSmoObject != null) { createItems.Add(newObject); } } else { createItems.Add(new MySmoObjectBase(sourceObject.CreateSplitItem(mProductionDatabase, 0, null))); } // // Add split objects, if this table (stored procedure) have split version // if ((productionTable != null && Database.IsSplitTable(productionTable.Name)) || Database.IsSplitProcedure(sourceObject.Name)) { foreach (int surveyId in mDatabaseEngine.SurveyIds) { MySmoObjectBase splitObject; if (productionTable != null) { string splitTableName = Database.GetTemplateTableNameByOriginalTableName( productionTable.Name, surveyId); // Create split object for split table splitObject = sourceObject.CreateSplitItem( mProductionDatabase.Tables[splitTableName], surveyId, mDatabaseEngine); } else { // Create split stored procedure splitObject = sourceObject.CreateSplitItem(mProductionDatabase, surveyId, mDatabaseEngine); } createItems.Add(new MySmoObjectBase(splitObject)); } } } } #endregion }