protected override IEnumerable <MigrationOperation> Add(IProperty target, DiffContext diffContext, bool inline = false) { var schema = _cassandraOptionsExtension.DefaultKeyspace; var targetEntityType = target.DeclaringEntityType.GetRootType(); var et = targetEntityType.Model.FindEntityType(target.ClrType); Type clrType; IEntityType userDefinedType = null; if (et == null || !et.IsUserDefinedType()) { var typeMapping = TypeMappingSource.GetMapping(target); clrType = typeMapping.Converter?.ProviderClrType ?? (typeMapping.ClrType).UnwrapNullableType(); } else { clrType = target.ClrType; userDefinedType = et; } var operation = new AddColumnOperation { Schema = schema, Table = targetEntityType.GetTableName(), Name = target.GetColumnName() }; Initialize( operation, target, clrType, target.IsColumnNullable(), MigrationsAnnotations.For(target), inline, userDefinedType); yield return(operation); }
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()); }
private IEnumerable <MigrationOperation> DiffAnnotations( IModel source, IModel target) { var sourceMigrationsAnnotations = source == null ? null : MigrationsAnnotations.For(source).ToList(); var targetMigrationsAnnotations = target == null ? null : MigrationsAnnotations.For(target).ToList(); if (source == null) { if (targetMigrationsAnnotations?.Count > 0) { var alterDatabaseOperation = new AlterDatabaseOperation(); alterDatabaseOperation.AddAnnotations(targetMigrationsAnnotations); yield return(alterDatabaseOperation); } yield break; } if (target == null) { sourceMigrationsAnnotations = MigrationsAnnotations.ForRemove(source).ToList(); if (sourceMigrationsAnnotations.Count > 0) { var alterDatabaseOperation = new AlterDatabaseOperation(); alterDatabaseOperation.OldDatabase.AddAnnotations(MigrationsAnnotations.ForRemove(source)); yield return(alterDatabaseOperation); } yield break; } if (HasDifferences(sourceMigrationsAnnotations, targetMigrationsAnnotations)) { var alterDatabaseOperation = new AlterDatabaseOperation(); alterDatabaseOperation.AddAnnotations(targetMigrationsAnnotations); alterDatabaseOperation.OldDatabase.AddAnnotations(sourceMigrationsAnnotations); yield return(alterDatabaseOperation); } }
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); }
protected override IEnumerable <MigrationOperation> Add(IColumn target, DiffContext diffContext, bool inline = false) { var _property = target.PropertyMappings.ToArray().FirstOrDefault().Property; if (_property.FindTypeMapping() is RelationalTypeMapping storeType) { var valueGenerationStrategy = MySQLValueGenerationStrategyCompatibility.GetValueGenerationStrategy(MigrationsAnnotations.ForRemove(target).ToArray()); // Ensure that null will be set for the columns default value, if CURRENT_TIMESTAMP has been required, // or when the store type of the column does not support default values at all. inline = inline || (storeType.StoreTypeNameBase == "datetime" || storeType.StoreTypeNameBase == "timestamp") && (valueGenerationStrategy == MySQLValueGenerationStrategy.IdentityColumn || valueGenerationStrategy == MySQLValueGenerationStrategy.ComputedColumn) || storeType.StoreTypeNameBase.Contains("text") || storeType.StoreTypeNameBase.Contains("blob") || storeType.StoreTypeNameBase == "geometry" || storeType.StoreTypeNameBase == "json"; } return(base.Add(target, diffContext, inline)); }
protected override IEnumerable <MigrationOperation> Add(IProperty target, DiffContext diffContext, bool inline = false) { if (target.FindTypeMapping() is RelationalTypeMapping storeType) { var valueGenerationStrategy = MySqlValueGenerationStrategyCompatibility.GetValueGenerationStrategy(MigrationsAnnotations.For(target).ToArray()); // Ensure that null will be set for the columns default value, if CURRENT_TIMESTAMP has been required. inline = inline || (storeType.StoreTypeNameBase == "datetime" || storeType.StoreTypeNameBase == "timestamp") && (valueGenerationStrategy == MySqlValueGenerationStrategy.IdentityColumn || valueGenerationStrategy == MySqlValueGenerationStrategy.ComputedColumn); } return(base.Add(target, diffContext, inline)); }
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); }