Exemplo n.º 1
0
        public static bool FkeyHasChanged(SchemaChanges changes, SubObjectState <FkeyType, FkeyType.State> current)
        {
            if (current == null)
            {
                return(true);
            }

            var desired = changes.Desired.Get(current);

            // The foreign key doesn't exist in the desired schema at all
            if (desired == null)
            {
                return(true);
            }

            // The destination table has changed
            if (desired.State.ToTable != current.State.ToTable)
            {
                return(true);
            }

            // Either the from or to table might need to be dropped entirely to reorder fields
            if (FieldType.IsFieldReorderPossiblyNeeded(changes, current.ParentIdentifier) ||
                FieldType.IsFieldReorderPossiblyNeeded(changes, current.State.ToTable))
            {
                return(true);
            }

            // It's changed from cascading to not or vice versa
            if (desired.State.IsCascadeDelete != current.State.IsCascadeDelete)
            {
                return(true);
            }

            // The sequence of field name pairs don't match
            if (!Enumerable.SequenceEqual(desired.State.Joins, current.State.Joins, FieldPair.GetComparer(changes.DbDriver)))
            {
                return(true);
            }

            // Any of the fields in either table are changing
            if (current.State.Joins.Any(field =>
                                        FieldType.FieldHasChanged(changes, current.ParentIdentifier, FieldType.Identifier(field.FromFieldName)) ||
                                        FieldType.FieldHasChanged(changes, current.State.ToTable, FieldType.Identifier(field.ToFieldName))))
            {
                return(true);
            }

            // The unique index on the destination table that corresponds to the destination fields is changing
            var ukey = UniqueIndexType.ChildrenFrom(changes.Current, current.State.ToTable)
                       .SingleOrDefault(key => new HashSet <Identifier>(key.State.IndexState.Fields).SetEquals(from field in current.State.Joins
                                                                                                               select FieldType.Identifier(field.ToFieldName)));

            if (ukey != null && UniqueIndexType.IndexHasChanged(changes, ukey))
            {
                return(true);
            }

            return(false);
        }