Exemple #1
0
        public override List <SynchronizationItem> GetCreateItems()
        {
            var items = new List <SynchronizationItem>();
            var diff  = getDifference(DifferenceType.Create, DatabaseObject);

            if (diff != null)
            {
                var item = new SynchronizationItem(DatabaseObject);
                items.Add(item);
                item.Differences.Add(diff);
                item.AddScript(2, getCreateScript());
            }

            foreach (var fk in DatabaseObject.ForeignKeys)
            {
                items.AddRange(new ForeignKeySynchronization(TargetDatabase, fk).GetCreateItems());
            }

            items.AddRange(getIndexCreateUpdateItems(null, false));
            items.AddRange(getDefaultConstraintCreateUpdateItems(null, false));
            items.AddRange(getTriggerUpdateItems(null, false));
            foreach (var column in DatabaseObject.Columns)
            {
                items.AddRange(ExtendedPropertySynchronization.GetExtendedProperties(TargetDatabase, column, null));
            }

            return(items);
        }
        public static List <SynchronizationItem> GetExtendedProperties(DatabaseObjects.Database targetDatabase,
                                                                       DatabaseObjectWithExtendedProperties sourceObject, DatabaseObjectWithExtendedProperties targetObject)
        {
            // TODO:?
            if (sourceObject.Database.DataSource.GetType().FullName !=
                targetDatabase.DataSource.GetType().FullName)
            {
                return(new List <SynchronizationItem>());
            }

            var items = new List <SynchronizationItem>();
            var skips = new List <string>();

            if (targetObject != null)
            {
                foreach (var toProperty in targetObject.ExtendedProperties)
                {
                    var fromProp = sourceObject.ExtendedProperties.FirstOrDefault(p => p.PropName == toProperty.PropName);
                    if (fromProp == null)
                    {
                        items.AddRange(new ExtendedPropertySynchronization(targetObject.Database, toProperty).GetDropItems(sourceObject));
                    }
                    else
                    {
                        var toItems = new ExtendedPropertySynchronization(targetObject.Database, fromProp).GetSynchronizationItems(toProperty, true, false);
                        if (toItems.Any())
                        {
                            items.AddRange(toItems);
                        }

                        skips.Add(toProperty.PropName);
                    }
                }
            }

            foreach (var fromProperty in sourceObject.ExtendedProperties)
            {
                if (skips.Contains(fromProperty.PropName))
                {
                    continue;
                }

                items.AddRange(new ExtendedPropertySynchronization(targetDatabase, fromProperty).GetCreateItems());
            }

            return(items);
        }
Exemple #3
0
        public override List <SynchronizationItem> GetAlterItems(DatabaseObjectBase target, bool ignoreCase, bool condensed)
        {
            var targetTable = target as Table;

            var items = new List <SynchronizationItem>();


            bool recreate = false;

            foreach (var fc in DatabaseObject.Columns)
            {
                var tc = targetTable.Columns.FirstOrDefault(c => string.Compare(c.ColumnName, fc.ColumnName, ignoreCase) == 0);
                if (tc != null && tc.IsIdentity != fc.IsIdentity)
                {
                    var diff = getDifference(DifferenceType.Create, fc, tc, "IsIdentity", fc.IsIdentity.ToString(), tc.IsIdentity.ToString());
                    if (diff != null)
                    {
                        var item = getRecreateTableItem(targetTable, ignoreCase);
                        item.Differences.Add(diff);
                        items.Add(item);
                        recreate    = true;
                        targetTable = null;
                        break;
                    }
                }

                if (tc != null && tc.ColumnType.DataType == DataType.RowVersion)
                {
                    var diff = new ColumnSynchronization(TargetDatabase, fc).GetPropertyDifferences(tc, ignoreCase);
                    if (diff.Any())
                    {
                        var item = getRecreateTableItem(targetTable, ignoreCase);
                        item.Differences.AddRange(diff);
                        items.Add(item);
                        recreate    = true;
                        targetTable = null;
                        break;
                    }
                }
            }

            if (!recreate)
            {
                foreach (var tk in targetTable.ForeignKeys)
                {
                    if (!DatabaseObject.Database.DataSource.NamedConstraints || !targetTable.Database.DataSource.NamedConstraints)
                    {
                        if (!DatabaseObject.ForeignKeys.Any(k =>
                                                            string.Compare(k.ChildTable.ObjectName, tk.ChildTable.ObjectName, ignoreCase) == 0 &&
                                                            string.Compare(k.ParentTable.ObjectName, tk.ParentTable.ObjectName, ignoreCase) == 0 &&
                                                            k.Columns.All(x => tk.Columns.Any(c => string.Compare(c.ChildColumn.ObjectName, x.ChildColumn.ObjectName, ignoreCase) == 0 &&
                                                                                              string.Compare(c.ParentColumn.ObjectName, x.ParentColumn.ObjectName, ignoreCase) == 0))))
                        {
                            items.AddRange(new ForeignKeySynchronization(TargetDatabase, tk).GetDropItems(DatabaseObject));
                        }
                    }
                    else if (!DatabaseObject.ForeignKeys.Any(k => string.Compare(k.ForeignKeyName, tk.ForeignKeyName, ignoreCase) == 0))
                    {
                        if (targetTable.Database.DataSource.ForeignKeyDropsWithColumns)
                        {
                            // column being dropped
                            if (tk.Columns.Any(c => !DatabaseObject.Columns.Any(x => string.Compare(x.ColumnName, c.ChildColumn.ColumnName, ignoreCase) == 0)))
                            {
                                continue;
                            }
                        }

                        items.AddRange(new ForeignKeySynchronization(TargetDatabase, tk).GetDropItems(DatabaseObject));
                    }
                }

                foreach (var fk in DatabaseObject.ForeignKeys)
                {
                    var tk = targetTable.ForeignKeys.FirstOrDefault(x => string.Compare(x.ObjectName, fk.ObjectName, ignoreCase) == 0);
                    if (tk == null)
                    {
                        if (!TargetDatabase.DataSource.NamedConstraints || !DatabaseObject.Database.DataSource.NamedConstraints)
                        {
                            tk = targetTable.ForeignKeys.FirstOrDefault(x =>
                                                                        string.Compare(x.ParentTable.TableName, fk.ParentTable.TableName, ignoreCase) == 0 &&
                                                                        string.Compare(x.ChildTable.TableName, fk.ChildTable.TableName, ignoreCase) == 0 &&
                                                                        x.Columns.All(y =>
                                                                                      fk.Columns.Any(z =>
                                                                                                     string.Compare(z.ChildColumn.ColumnName, y.ChildColumn.ColumnName, ignoreCase) == 0 &&
                                                                                                     string.Compare(z.ParentColumn.ColumnName, y.ParentColumn.ColumnName, ignoreCase) == 0)));
                        }
                    }
                    if (tk != null)
                    {
                        items.AddRange(new ForeignKeySynchronization(TargetDatabase, fk).GetSynchronizationItems(tk, ignoreCase, condensed));
                    }
                    else
                    {
                        items.AddRange(new ForeignKeySynchronization(TargetDatabase, fk).GetCreateItems());
                    }
                }

                foreach (var tc in targetTable.Columns)
                {
                    if (!DatabaseObject.Columns.Any(c => string.Compare(c.ColumnName, tc.ColumnName, ignoreCase) == 0))
                    {
                        items.AddRange(new ColumnSynchronization(TargetDatabase, tc).GetDropItems(DatabaseObject));
                    }
                }

                _createdColumns = new List <string>();
                _alteredColumns = new List <string>();

                foreach (var fc in DatabaseObject.Columns)
                {
                    var tc = targetTable.Columns.FirstOrDefault(c => string.Compare(c.ColumnName, fc.ColumnName, ignoreCase) == 0);
                    if (tc == null)
                    {
                        items.AddRange(new ColumnSynchronization(TargetDatabase, fc).GetAddAlterItems(null, ignoreCase, false));
                        _createdColumns.Add(fc.ColumnName);
                    }
                    else
                    {
                        var alteredItems = new ColumnSynchronization(TargetDatabase, fc).GetSynchronizationItems(tc, ignoreCase, condensed);
                        if (alteredItems.Any())
                        {
                            items.AddRange(alteredItems);
                            _alteredColumns.Add(fc.ColumnName);
                        }
                    }
                }
            }

            if (!recreate)
            {
                items.AddRange(getKeyConstraintUpdateItems(targetTable, ignoreCase, condensed));
            }

            if (!condensed)
            {
                items.AddRange(getIndexCreateUpdateItems(targetTable, ignoreCase));
                items.AddRange(getDefaultConstraintCreateUpdateItems(targetTable, ignoreCase));
                items.AddRange(getTriggerUpdateItems(targetTable, ignoreCase));
                foreach (var column in DatabaseObject.Columns)
                {
                    items.AddRange(ExtendedPropertySynchronization.GetExtendedProperties(TargetDatabase, column, targetTable == null ? null : targetTable.Columns.FirstOrDefault(c => c.ColumnName == column.ColumnName)));
                }
            }
            TargetDatabase.DataSource.CheckUnnecessaryItems(items);
            return(items);
        }