/// <summary> /// Builds commands for the given <see cref="RenameIndexOperation" /> /// by making calls on the given <see cref="MigrationCommandListBuilder" />. /// </summary> /// <param name="operation"> The operation. </param> /// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param> /// <param name="builder"> The command builder to use to build the commands. </param> protected override void Generate( RenameIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); if (operation.NewName != null) { if (_connectionInfo.ServerVersion.SupportsRenameIndex) { builder.Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" RENAME INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" TO ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.NewName)) .AppendLine(";"); EndStatement(builder); } else { var index = FindEntityTypes(model, operation.Schema, operation.Table) ?.SelectMany(e => e.GetDeclaredIndexes()) .FirstOrDefault(i => i.GetName() == operation.NewName); if (index == null) { throw new InvalidOperationException( $"Could not find the model index: {Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)}.{Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.NewName)}. Upgrade to Mysql 5.7+ or split the 'RenameIndex' call into 'DropIndex' and 'CreateIndex'"); } Generate(new DropIndexOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name }, model, builder); var createIndexOperation = new CreateIndexOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.NewName, Columns = index.Properties.Select(p => p.GetColumnName()).ToArray(), IsUnique = index.IsUnique, Filter = index.GetFilter() }; createIndexOperation.AddAnnotations(_migrationsAnnotations.For(index)); createIndexOperation.AddAnnotations(operation.GetAnnotations()); Generate(createIndexOperation, model, builder); } } }
protected virtual void CreateIndexes( [CanBeNull] IProperty property, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(builder, nameof(builder)); if (property == null) { return; } foreach (var index in property.GetContainingIndexes()) { var operation = new CreateIndexOperation { IsUnique = index.IsUnique, Name = Annotations.For(index).Name, Schema = Annotations.For(index.DeclaringEntityType).Schema, Table = Annotations.For(index.DeclaringEntityType).TableName, Columns = index.Properties.Select(p => Annotations.For(p).ColumnName).ToArray() }; operation.AddAnnotations(_migrationsAnnotations.For(index)); Generate(operation, index.DeclaringEntityType.Model, builder, terminate: false); builder.AppendLine(SqlGenerationHelper.StatementTerminator); } }
/// <summary> /// Generates SQL to create the given indexes. /// </summary> /// <param name="indexes"> The indexes to create. </param> /// <param name="builder"> The command builder to use to build the commands. </param> protected virtual void CreateIndexes( [NotNull] IEnumerable <IIndex> indexes, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(indexes, nameof(indexes)); Check.NotNull(builder, nameof(builder)); foreach (var index in indexes) { var operation = new CreateIndexOperation { IsUnique = index.IsUnique, Name = index.GetName(), Schema = index.DeclaringEntityType.GetSchema(), Table = index.DeclaringEntityType.GetTableName(), Columns = index.Properties.Select(p => p.GetColumnName()) .ToArray(), Filter = index.GetFilter() }; operation.AddAnnotations(_migrationsAnnotations.For(index)); Generate(operation, index.DeclaringEntityType.Model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); } }
/// <summary> /// Creates a new <see cref="CreateIndexOperation" /> from the specified index. /// </summary> /// <param name="index">The index.</param> /// <returns>The operation.</returns> public static CreateIndexOperation CreateFrom(ITableIndex index) { Check.NotNull(index, nameof(index)); var operation = new CreateIndexOperation { IsUnique = index.IsUnique, Name = index.Name, Schema = index.Table.Schema, Table = index.Table.Name, Columns = index.Columns.Select(p => p.Name).ToArray(), Filter = index.Filter }; operation.AddAnnotations(index.GetAnnotations()); return(operation); }
/// <summary> /// Builds commands for the given <see cref="RenameIndexOperation"/> by making calls on the /// given <see cref="MigrationCommandListBuilder"/>. /// </summary> /// <param name="operation">The operation.</param> /// <param name="model"> /// The target model which may be <c>null</c> if the operations exist without a model. /// </param> /// <param name="builder">The command builder to use to build the commands.</param> protected override void Generate(RenameIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { var index = FindEntityTypes(model, operation.Schema, operation.Table) ?.SelectMany(t => t.GetDeclaredIndexes()).Where(i => i.Relational().Name == operation.NewName) .FirstOrDefault(); if (index == null) { throw new NotSupportedException( SqliteStrings.InvalidMigrationOperation(operation.GetType().ShortDisplayName())); } var dropOperation = new DropIndexOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name }; dropOperation.AddAnnotations(_migrationsAnnotations.ForRemove(index)); var createOperation = new CreateIndexOperation { IsUnique = index.IsUnique, Name = operation.NewName, Schema = operation.Schema, Table = operation.Table, Columns = index.Properties.Select(p => p.Relational().ColumnName).ToArray(), Filter = index.Relational().Filter }; createOperation.AddAnnotations(_migrationsAnnotations.For(index)); Generate(dropOperation, model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); Generate(createOperation, model, builder); }
/// <summary> /// Builds commands for the given <see cref="RenameIndexOperation" /> /// by making calls on the given <see cref="MigrationCommandListBuilder" />. /// </summary> /// <param name="operation"> The operation. </param> /// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param> /// <param name="builder"> The command builder to use to build the commands. </param> protected override void Generate(RenameIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { var index = model.GetRelationalModel().FindTable(operation.Table, operation.Schema) ?.Indexes.FirstOrDefault(i => i.Name == operation.NewName); if (index == null) { throw new NotSupportedException( SqliteStrings.InvalidMigrationOperation(operation.GetType().ShortDisplayName())); } var dropOperation = new DropIndexOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name }; dropOperation.AddAnnotations(index.GetAnnotations()); var createOperation = new CreateIndexOperation { IsUnique = index.IsUnique, Name = operation.NewName, Schema = operation.Schema, Table = operation.Table, Columns = index.Columns.Select(p => p.Name).ToArray(), Filter = index.Filter }; createOperation.AddAnnotations(index.GetAnnotations()); Generate(dropOperation, model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); Generate(createOperation, model, builder); }