Example #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());
        }
        protected override IEnumerable <MigrationOperation> Diff(TableMapping source, TableMapping target, DiffContext diffContext)
        {
            var clrComment  = source?.EntityTypes.Select(e => e.FindAnnotation("ClrComment")).FirstOrDefault(e => e != null)?.Value.ToString();
            var clrComment2 = target?.EntityTypes.Select(e => e.FindAnnotation("ClrComment")).FirstOrDefault(e => e != null)?.Value.ToString();
            var operations  = base.Diff(source, target, diffContext);

            if (clrComment == clrComment2)
            {
                return(operations);
            }
            var tableName = target?.EntityTypes.Select(e => e.FindAnnotation("Relational:TableName")).FirstOrDefault(e => e != null)?.Value.ToString();

            if (tableName == null)
            {
                tableName = source?.EntityTypes.Select(e => e.FindAnnotation("Relational:TableName")).FirstOrDefault(e => e != null)?.Value.ToString();
            }
            if (tableName == null)
            {
                foreach (var t in target.EntityTypes.Concat(new[] { target.GetRootType() }))
                {
                    Console.WriteLine($"{t.Name}");
                    foreach (var annotation in t.GetAnnotations())
                    {
                        Console.WriteLine($"{annotation.Name} => {annotation.Value}");
                    }
                }
                return(operations);
            }
            var schema = target?.EntityTypes.Select(e => e.FindAnnotation("Relational:Schema")).FirstOrDefault(e => e != null)?.Value.ToString() ?? "dbo";
            var diff   = operations.ToList();

            diff.Add(new CommentOperation()
            {
                Table   = tableName,
                Schema  = schema,
                Comment = clrComment2,
            });
            return(diff);
        }
        protected override IEnumerable <MigrationOperation> Remove(TableMapping source, DiffContext diffContext)
        {
            var type = source.GetRootType();
            MigrationOperation operation;

            if (!type.IsUserDefinedType())
            {
                var dropOperation = new DropTableOperation {
                    Schema = source.Schema, Name = source.Name
                };
                diffContext.AddDrop(source, dropOperation);
                operation = dropOperation;
            }
            else
            {
                operation = new DropUserDefinedTypeOperation {
                    Schema = source.Schema, Name = source.Name
                };
            }

            operation.AddAnnotations(MigrationsAnnotations.ForRemove(source.EntityTypes[0]));
            yield return(operation);
        }
Example #4
0
 private IEnumerable <IProperty> GetSortedProperties(TableMapping target)
 => GetSortedProperties(target.GetRootType()).Distinct((x, y) => x.GetColumnName() == y.GetColumnName());