Exemplo n.º 1
0
        public override bool IsEqual(MySmoObjectBase newValue)
        {
            if (!base.IsEqual(newValue))
            {
                return(false);
            }

            var productionForeignKey = (ForeignKey)SourceSmoObject;
            var sourceForeignKey     = (ForeignKey)newValue.SourceSmoObject;

            if (productionForeignKey.Columns.Count != sourceForeignKey.Columns.Count)
            {
                return(false);
            }

            for (int num = 0; num < productionForeignKey.Columns.Count; num++)
            {
                ForeignKeyColumn productionForeignKeyColumn = productionForeignKey.Columns[num];
                ForeignKeyColumn sourceForeignKeyColumn     = sourceForeignKey.Columns[num];

                if (productionForeignKeyColumn.Name != sourceForeignKeyColumn.Name ||
                    productionForeignKeyColumn.ReferencedColumn != sourceForeignKeyColumn.ReferencedColumn)
                {
                    return(false);
                }
            }

            return(productionForeignKey.IsEnabled == sourceForeignKey.IsEnabled &&
                   productionForeignKey.IsChecked == sourceForeignKey.IsChecked &&
                   productionForeignKey.DeleteAction == sourceForeignKey.DeleteAction &&
                   productionForeignKey.UpdateAction == sourceForeignKey.UpdateAction &&
                   productionForeignKey.ReferencedTable == sourceForeignKey.ReferencedTable &&
                   productionForeignKey.ReferencedTableSchema == sourceForeignKey.ReferencedTableSchema);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return all objects from current table
        /// </summary>
        /// <param name="myObjectType">Model object for detect its type</param>
        /// <returns></returns>
        public Dictionary <string, MySmoObjectBase> GetMyObjects(MySmoObjectBase myObjectType)
        {
            if (myObjectType.SourceSmoObject.GetType() == typeof(Trigger))
            {
                return(MyTriggers);
            }

            if (myObjectType.SourceSmoObject.GetType() == typeof(Index))
            {
                return(MyIndexes);
            }

            if (myObjectType.SourceSmoObject.GetType() == typeof(ForeignKey))
            {
                return(MyForeignKeys);
            }

            if (myObjectType.SourceSmoObject.GetType() == typeof(Column))
            {
                return(MyColumns);
            }

            throw new Exception(string.Format(
                                    "Unknown type {0} in function GetMyObjects",
                                    myObjectType.SourceSmoObject.GetType()));
        }
Exemplo n.º 3
0
 public MySmoObjectBase(MySmoObjectBase mySmoObject)
 {
     SourceSmoObject           = mySmoObject.SourceSmoObject;
     Name                      = mySmoObject.Name;
     ParentName                = mySmoObject.ParentName;
     this.mProductionSmoObject = mySmoObject.mProductionSmoObject;
     ExtraParams               = new string[mySmoObject.ExtraParams.Length];
     mySmoObject.ExtraParams.CopyTo(ExtraParams, 0);
 }
Exemplo n.º 4
0
        public virtual bool IsEqual(MySmoObjectBase newValue)
        {
            if (ExtraParams.Length != newValue.ExtraParams.Length)
            {
                return(false);
            }

            if (this.ExtraParams.Where((t, i) => t != newValue.ExtraParams[i]).Any())
            {
                return(false);
            }

            return(SourceSmoObject.GetType() == newValue.SourceSmoObject.GetType() &&
                   Name == newValue.Name);
        }
Exemplo n.º 5
0
        public override bool IsEqual(MySmoObjectBase newValue)
        {
            if (!base.IsEqual(newValue))
            {
                return(false);
            }

            var productionColumn = (Column)SourceSmoObject;
            var sourceColumn     = (Column)newValue.SourceSmoObject;

            if (productionColumn.Name == sourceColumn.Name &&
                productionColumn.DataType.SqlDataType != sourceColumn.DataType.SqlDataType)
            {
                throw new Exception(string.Format(
                                        "Column {0}.{1} has different types in source and production databases",
                                        ((Table)productionColumn.Parent).Name,
                                        productionColumn.Name));
            }

            if (productionColumn.DefaultConstraint != null)
            {
                if (sourceColumn.DefaultConstraint == null)
                {
                    return(false);
                }

                string prodColName = ChangeName(productionColumn.DefaultConstraint.Name);
                string sourColName = ChangeName(sourceColumn.DefaultConstraint.Name);
                if (prodColName != sourColName ||
                    productionColumn.DefaultConstraint.Text != sourceColumn.DefaultConstraint.Text)
                {
                    return(false);
                }
            }
            else
            {
                if (sourceColumn.DefaultConstraint != null)
                {
                    return(false);
                }
            }

            return(productionColumn.Nullable == sourceColumn.Nullable &&
                   productionColumn.Identity == sourceColumn.Identity &&
                   productionColumn.IdentitySeed == sourceColumn.IdentitySeed &&
                   productionColumn.IdentityIncrement == sourceColumn.IdentityIncrement);
        }
Exemplo n.º 6
0
        public virtual bool IsEqual(MySmoObjectBase newValue)
        {
            if (ExtraParams.Length != newValue.ExtraParams.Length)
            {
                return(false);
            }
            for (int i = 0; i < ExtraParams.Length; i++)
            {
                if (ExtraParams[i] != newValue.ExtraParams[i])
                {
                    return(false);
                }
            }

            return(SourceSmoObject.GetType() == newValue.SourceSmoObject.GetType() &&
                   Name == newValue.Name);
        }
Exemplo n.º 7
0
        /// <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));
                }
            }
        }
Exemplo n.º 8
0
        public override bool IsEqual(MySmoObjectBase newValue)
        {
            if (!base.IsEqual(newValue))
            {
                return(false);
            }

            var productionTrigger = (Trigger)SourceSmoObject;
            var sourceTrigger     = (Trigger)newValue.SourceSmoObject;

            return(productionTrigger.Delete == sourceTrigger.Delete &&
                   productionTrigger.DeleteOrder == sourceTrigger.DeleteOrder &&
                   productionTrigger.Insert == sourceTrigger.Insert &&
                   productionTrigger.InsertOrder == sourceTrigger.InsertOrder &&
                   productionTrigger.Update == sourceTrigger.Update &&
                   productionTrigger.UpdateOrder == sourceTrigger.UpdateOrder &&
                   productionTrigger.QuotedIdentifierStatus == sourceTrigger.QuotedIdentifierStatus);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Get UserDefinedFunctions from indicated database
        /// </summary>
        /// <param name="database">Database</param>
        /// <returns></returns>
        public static Dictionary <string, MySmoObjectBase> GetUserDefinedFunctionsInfo(Microsoft.SqlServer.Management.Smo.Database database)
        {
            var userDefinedFunctions = new Dictionary <string, MySmoObjectBase>();

            foreach (UserDefinedFunction userDefinedFunction in database.UserDefinedFunctions)
            {
                if (userDefinedFunction.IsSystemObject == false)
                {
                    var mySmoObjectBase = new MySmoObjectBase(
                        userDefinedFunction,
                        userDefinedFunction.Name,
                        database.Name,
                        userDefinedFunction.TextBody,
                        userDefinedFunction.TextHeader);

                    userDefinedFunctions.Add(userDefinedFunction.Name, mySmoObjectBase);
                }
            }

            return(userDefinedFunctions);
        }
Exemplo n.º 10
0
        public override bool IsEqual(MySmoObjectBase newValue)
        {
            Trace.TraceInformation("Compare {0} procedures from {1} and {2} databases...\r\n", Name, ParentName, newValue.ParentName);

            if (!base.IsEqual(newValue))
            {
                return(false);
            }

            var productionStoredProcedure = (StoredProcedure)SourceSmoObject;
            var sourceStoredProcedure     = (StoredProcedure)newValue.SourceSmoObject;

            if (productionStoredProcedure.Parameters.Count != sourceStoredProcedure.Parameters.Count)
            {
                return(false);
            }

            foreach (StoredProcedureParameter productionStoredProcedureParameter in productionStoredProcedure.Parameters)
            {
                if (!sourceStoredProcedure.Parameters.Contains(productionStoredProcedureParameter.Name))
                {
                    return(false);
                }

                StoredProcedureParameter sourceStoredProcedureParameter = sourceStoredProcedure.Parameters[productionStoredProcedureParameter.Name];

                if (sourceStoredProcedureParameter.DataType.SqlDataType != productionStoredProcedureParameter.DataType.SqlDataType ||
                    sourceStoredProcedureParameter.DataType.MaximumLength != productionStoredProcedureParameter.DataType.MaximumLength ||
                    sourceStoredProcedureParameter.DataType.NumericPrecision != productionStoredProcedureParameter.DataType.NumericPrecision ||
                    sourceStoredProcedureParameter.DataType.NumericScale != productionStoredProcedureParameter.DataType.NumericScale ||
                    sourceStoredProcedureParameter.DefaultValue != productionStoredProcedureParameter.DefaultValue ||
                    sourceStoredProcedureParameter.IsOutputParameter != productionStoredProcedureParameter.IsOutputParameter)
                {
                    return(false);
                }
            }

            return(productionStoredProcedure.QuotedIdentifierStatus == sourceStoredProcedure.QuotedIdentifierStatus &&
                   productionStoredProcedure.Startup == sourceStoredProcedure.Startup);
        }
Exemplo n.º 11
0
        public override bool IsEqual(MySmoObjectBase newValue)
        {
            if (!base.IsEqual(newValue))
            {
                return(false);
            }

            var productionIndex = (Index)SourceSmoObject;
            var sourceIndex     = (Index)newValue.SourceSmoObject;

            //
            // Check include column and non include column separate
            //
            var includedColumnsProduction = new List <IndexedColumn>();
            var includedColumnsSource     = new List <IndexedColumn>();
            var indexedColumnsProduction  = new List <IndexedColumn>();
            var indexedColumnsSource      = new List <IndexedColumn>();

            // Get production DB's columns
            for (int i = 0; i < productionIndex.IndexedColumns.Count; i++)
            {
                if (productionIndex.IndexedColumns[i].IsIncluded)
                {
                    includedColumnsProduction.Add(productionIndex.IndexedColumns[i]);
                }
                else
                {
                    indexedColumnsProduction.Add(productionIndex.IndexedColumns[i]);
                }
            }

            // Get source DB's columns
            for (int i = 0; i < sourceIndex.IndexedColumns.Count; i++)
            {
                if (sourceIndex.IndexedColumns[i].IsIncluded)
                {
                    includedColumnsSource.Add(sourceIndex.IndexedColumns[i]);
                }
                else
                {
                    indexedColumnsSource.Add(sourceIndex.IndexedColumns[i]);
                }
            }

            if (includedColumnsProduction.Count != includedColumnsSource.Count ||
                indexedColumnsProduction.Count != indexedColumnsSource.Count)
            {
                return(false);
            }

            // Check include column
            foreach (IndexedColumn includedColumnSource in includedColumnsSource)
            {
                bool isContains = false;
                foreach (IndexedColumn includedColumnProduction in includedColumnsProduction)
                {
                    if (includedColumnSource.Name == includedColumnProduction.Name &&
                        includedColumnSource.Descending == includedColumnProduction.Descending)
                    {
                        isContains = true;
                        break;
                    }
                }

                if (!isContains)
                {
                    return(false);
                }
            }

            // Check non include column
            for (int i = 0; i < indexedColumnsSource.Count; i++)
            {
                if (indexedColumnsSource[i].Name != indexedColumnsProduction[i].Name ||
                    indexedColumnsSource[i].Descending != indexedColumnsProduction[i].Descending)
                {
                    return(false);
                }
            }

            return(productionIndex.PadIndex == sourceIndex.PadIndex &&
                   productionIndex.FillFactor == sourceIndex.FillFactor &&
                   productionIndex.MaximumDegreeOfParallelism == sourceIndex.MaximumDegreeOfParallelism &&
                   productionIndex.SortInTempdb == sourceIndex.SortInTempdb &&
                   productionIndex.IsFullTextKey == sourceIndex.IsFullTextKey &&
                   productionIndex.DisallowPageLocks == sourceIndex.DisallowPageLocks &&
                   productionIndex.DisallowRowLocks == sourceIndex.DisallowRowLocks &&
                   productionIndex.IgnoreDuplicateKeys == sourceIndex.IgnoreDuplicateKeys &&
                   productionIndex.IndexKeyType == sourceIndex.IndexKeyType &&
                   productionIndex.IsClustered == sourceIndex.IsClustered &&
                   productionIndex.IsUnique == sourceIndex.IsUnique &&
                   productionIndex.NoAutomaticRecomputation == sourceIndex.NoAutomaticRecomputation &&
                   productionIndex.OnlineIndexOperation == sourceIndex.OnlineIndexOperation);
        }
Exemplo n.º 12
0
        /// <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
        }