Пример #1
0
        protected override IEnumerable <MigrationOperation> Add(TableMapping target, DiffContext diffContext)
        {
            var schema = _cassandraOptionsExtension.DefaultKeyspace;
            var result = new List <MigrationOperation>();
            var type   = target.GetRootType();

            if (!type.IsUserDefinedType())
            {
                var entityType           = target.EntityTypes[0];
                var createTableOperation = new CreateTableOperation
                {
                    Schema  = schema,
                    Name    = target.Name,
                    Comment = target.GetComment()
                };
                createTableOperation.AddAnnotations(MigrationsAnnotations.For(entityType));
                createTableOperation.Columns.AddRange(GetSortedProperties(target).SelectMany(p => Add(p, diffContext, inline: true)).Cast <AddColumnOperation>());
                var primaryKey = target.EntityTypes[0].FindPrimaryKey();
                if (primaryKey != null)
                {
                    createTableOperation.PrimaryKey = Add(primaryKey, diffContext).Cast <AddPrimaryKeyOperation>().Single();
                }

                createTableOperation.UniqueConstraints.AddRange(
                    target.GetKeys().Where(k => !k.IsPrimaryKey()).SelectMany(k => Add(k, diffContext))
                    .Cast <AddUniqueConstraintOperation>());
                createTableOperation.CheckConstraints.AddRange(
                    target.GetCheckConstraints().SelectMany(c => Add(c, diffContext))
                    .Cast <CreateCheckConstraintOperation>());

                foreach (var targetEntityType in target.EntityTypes)
                {
                    diffContext.AddCreate(targetEntityType, createTableOperation);
                }

                result.Add(createTableOperation);
                foreach (var operation in target.GetIndexes().SelectMany(i => Add(i, diffContext)))
                {
                    result.Add(operation);
                }

                return(result.ToArray());
            }

            var createUserDefinedOperation = new CreateUserDefinedTypeOperation
            {
                Schema = schema,
                Name   = target.Name,
            };

            createUserDefinedOperation.Columns.AddRange(type.GetProperties().SelectMany(p => Add(p, diffContext, inline: true)).Cast <AddColumnOperation>());
            result.Add(createUserDefinedOperation);
            foreach (var operation in target.GetIndexes().SelectMany(i => Add(i, diffContext)))
            {
                result.Add(operation);
            }

            return(result.ToArray());
        }
Пример #2
0
        protected override IEnumerable <MigrationOperation> Diff(TableMapping source,
                                                                 TableMapping target, DiffContext diffContext)
        {
            if (source.Schema != target.Schema ||
                source.Name != target.Name)
            {
                // 如果是创建分表操作
                var entityType = target.EntityTypes[0];
                if (entityType.ClrType.IsDefined <ShardableAttribute>())
                {
                    // 不使用 Add 方法创建表操作,会添加重复的索引导致异常
                    var createTableOperation = new CreateTableOperation
                    {
                        Schema  = target.Schema,
                        Name    = target.Name,
                        Comment = target.GetComment()
                    };
                    createTableOperation.AddAnnotations(MigrationsAnnotations.For(entityType));

                    createTableOperation.Columns.AddRange
                        (GetSortedProperties(target)
                        .SelectMany(p => Add(p, diffContext, inline: true))
                        .Cast <AddColumnOperation>());

                    var primaryKey = entityType.FindPrimaryKey();
                    if (primaryKey != null)
                    {
                        createTableOperation.PrimaryKey = Add(primaryKey, diffContext).Cast <AddPrimaryKeyOperation>().Single();
                    }

                    createTableOperation.UniqueConstraints.AddRange(
                        target.GetKeys().Where(k => !k.IsPrimaryKey()).SelectMany(k => Add(k, diffContext))
                        .Cast <AddUniqueConstraintOperation>());

                    createTableOperation.CheckConstraints.AddRange(
                        target.GetCheckConstraints().SelectMany(c => Add(c, diffContext))
                        .Cast <CreateCheckConstraintOperation>());

                    foreach (var targetEntityType in target.EntityTypes)
                    {
                        diffContext.AddCreate(targetEntityType, createTableOperation);
                    }

                    yield return(createTableOperation);

                    yield break; // 跳出迭代,防止出现重复的添加主键、索引等相关操作
                }
                else
                {
                    yield return(new RenameTableOperation
                    {
                        Schema = source.Schema,
                        Name = source.Name,
                        NewSchema = target.Schema,
                        NewName = target.Name
                    });
                }
            }

            // Validation should ensure that all the relevant annotations for the collocated entity types are the same
            var sourceMigrationsAnnotations = MigrationsAnnotations.For(source.EntityTypes[0]).ToList();
            var targetMigrationsAnnotations = MigrationsAnnotations.For(target.EntityTypes[0]).ToList();

            if (source.GetComment() != target.GetComment() ||
                HasDifferences(sourceMigrationsAnnotations, targetMigrationsAnnotations))
            {
                var alterTableOperation = new AlterTableOperation
                {
                    Name     = target.Name,
                    Schema   = target.Schema,
                    Comment  = target.GetComment(),
                    OldTable = { Comment = source.GetComment() }
                };

                alterTableOperation.AddAnnotations(targetMigrationsAnnotations);
                alterTableOperation.OldTable.AddAnnotations(sourceMigrationsAnnotations);

                yield return(alterTableOperation);
            }

            var operations = Diff(source.GetProperties(), target.GetProperties(), diffContext)
                             .Concat(Diff(source.GetKeys(), target.GetKeys(), diffContext))
                             .Concat(Diff(source.GetIndexes(), target.GetIndexes(), diffContext))
                             .Concat(Diff(source.GetCheckConstraints(), target.GetCheckConstraints(), diffContext));

            foreach (var operation in operations)
            {
                yield return(operation);
            }

            DiffData(source, target, diffContext);
        }