Exemplo n.º 1
0
        protected virtual void Generate([NotNull] RenameIndexOperation operation, [NotNull] IndentedStringBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            builder.AppendLine(".RenameIndex(");

            using (builder.Indent())
            {
                builder
                .Append("name: ")
                .Append(_code.Literal(operation.Name));

                if (operation.Schema != null)
                {
                    builder
                    .AppendLine(",")
                    .Append("schema: ")
                    .Append(_code.Literal(operation.Schema));
                }

                builder
                .AppendLine(",")
                .Append("table: ")
                .Append(_code.Literal(operation.Table))
                .AppendLine(",")
                .Append("newName: ")
                .Append(_code.Literal(operation.NewName))
                .Append(")");

                Annotations(operation.GetAnnotations(), builder);
            }
        }
Exemplo n.º 2
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);
                }
            }
        }