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);
            }
        }
Beispiel #3
0
        /// <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);
                }
            }
        }
Beispiel #4
0
        /// <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);
        }