Ejemplo n.º 1
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 <see langword="null" /> 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());

            Generate(dropOperation, model, builder, terminate: false);
            builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);

            Generate(CreateIndexOperation.For(index), model, builder);
        }
        protected virtual void DropIndexes(
            [CanBeNull] IProperty property,
            [NotNull] MigrationCommandListBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            if (property == null)
            {
                return;
            }

            foreach (var index in property.GetContainingIndexes())
            {
                var operation = new DropIndexOperation
                {
                    Schema = Annotations.For(index.DeclaringEntityType).Schema,
                    Table  = Annotations.For(index.DeclaringEntityType).TableName,
                    Name   = Annotations.For(index).Name
                };
                operation.AddAnnotations(_migrationsAnnotations.ForRemove(index));

                Generate(operation, index.DeclaringEntityType.Model, builder, terminate: false);
                builder.AppendLine(SqlGenerationHelper.StatementTerminator);
            }
        }
        protected virtual void DropIndexes(
            [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 DropIndexOperation
                {
                    Schema = index.DeclaringEntityType.Relational().Schema,
                    Table  = index.DeclaringEntityType.Relational().TableName,
                    Name   = index.Relational().Name
                };
                operation.AddAnnotations(_migrationsAnnotations.ForRemove(index));

                Generate(operation, index.DeclaringEntityType.Model, builder, terminate: false);
                builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Generates SQL to drop the given indexes.
        /// </summary>
        /// <param name="indexes"> The indexes to drop. </param>
        /// <param name="builder"> The command builder to use to build the commands. </param>
        protected virtual void DropIndexes(
            [NotNull] IEnumerable <ITableIndex> indexes,
            [NotNull] MigrationCommandListBuilder builder)
        {
            Check.NotNull(indexes, nameof(indexes));
            Check.NotNull(builder, nameof(builder));

            foreach (var index in indexes)
            {
                var table     = index.Table;
                var operation = new DropIndexOperation
                {
                    Schema = table.Schema,
                    Table  = table.Name,
                    Name   = index.Name
                };
                operation.AddAnnotations(index.GetAnnotations());

                Generate(operation, table.Model.Model, builder, terminate: false);
                builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
            }
        }
Ejemplo n.º 5
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);
        }