Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ServiceControl.Persistence.EFCore.PostgreSql.Migrations
{
/// <inheritdoc />
public partial class AddMissingIndexes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "ix_failed_messages_status_last_modified",
table: "failed_messages");

migrationBuilder.DropIndex(
name: "ix_failed_message_groups_type_group_id",
table: "failed_message_groups");

migrationBuilder.AlterColumn<string>(
name: "message_type",
table: "failed_messages",
type: "character varying(450)",
maxLength: 450,
nullable: true,
oldClrType: typeof(string),
oldType: "text",
oldNullable: true);

migrationBuilder.CreateIndex(
name: "ix_failed_messages_status_last_modified",
table: "failed_messages",
columns: new[] { "status", "last_modified" })
.Annotation("Npgsql:IndexInclude", new[] { "first_time_of_failure", "last_time_of_failure" });

migrationBuilder.CreateIndex(
name: "ix_failed_messages_status_last_time_of_failure",
table: "failed_messages",
columns: new[] { "status", "last_time_of_failure" });

// Normally this would be potentially slow and necessitate executing outside of the
// migration transaction, but no customers consume this persister yet so this can
// be treated as an initial migration
migrationBuilder.CreateIndex(
name: "ix_failed_messages_status_message_type_unique_message_id",
table: "failed_messages",
columns: new[] { "status", "message_type", "unique_message_id" });

migrationBuilder.CreateIndex(
name: "ix_failed_message_groups_type_group_id",
table: "failed_message_groups",
columns: new[] { "type", "group_id" })
.Annotation("Npgsql:IndexInclude", new[] { "title" });
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "ix_failed_messages_status_last_modified",
table: "failed_messages");

migrationBuilder.DropIndex(
name: "ix_failed_messages_status_last_time_of_failure",
table: "failed_messages");

migrationBuilder.DropIndex(
name: "ix_failed_messages_status_message_type_unique_message_id",
table: "failed_messages");

migrationBuilder.DropIndex(
name: "ix_failed_message_groups_type_group_id",
table: "failed_message_groups");

migrationBuilder.AlterColumn<string>(
name: "message_type",
table: "failed_messages",
type: "text",
nullable: true,
oldClrType: typeof(string),
oldType: "character varying(450)",
oldMaxLength: 450,
oldNullable: true);

migrationBuilder.CreateIndex(
name: "ix_failed_messages_status_last_modified",
table: "failed_messages",
columns: new[] { "status", "last_modified" });

migrationBuilder.CreateIndex(
name: "ix_failed_message_groups_type_group_id",
table: "failed_message_groups",
columns: new[] { "type", "group_id" });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnName("message_id");

b.Property<string>("MessageType")
.HasColumnType("text")
.HasMaxLength(450)
.HasColumnType("character varying(450)")
.HasColumnName("message_type");

b.Property<int>("NumberOfProcessingAttempts")
Expand Down Expand Up @@ -440,6 +441,14 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasIndex("Status", "LastModified")
.HasDatabaseName("ix_failed_messages_status_last_modified");

NpgsqlIndexBuilderExtensions.IncludeProperties(b.HasIndex("Status", "LastModified"), new[] { "FirstTimeOfFailure", "LastTimeOfFailure" });

b.HasIndex("Status", "LastTimeOfFailure")
.HasDatabaseName("ix_failed_messages_status_last_time_of_failure");

b.HasIndex("Status", "MessageType", "UniqueMessageId")
.HasDatabaseName("ix_failed_messages_status_message_type_unique_message_id");

b.ToTable("failed_messages", (string)null);
});

Expand Down Expand Up @@ -474,6 +483,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasIndex("Type", "GroupId")
.HasDatabaseName("ix_failed_message_groups_type_group_id");

NpgsqlIndexBuilderExtensions.IncludeProperties(b.HasIndex("Type", "GroupId"), new[] { "Title" });

b.ToTable("failed_message_groups", (string)null);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<FailedMessageEntity>()
.HasIndex(e => e.StatusChangedAt)
.HasFilter($"status IN ({(int)FailedMessageStatus.Resolved}, {(int)FailedMessageStatus.Archived})");

// Widen the group-aggregate indexes with covering INCLUDE columns so the
// /api/recoverability/groups/ aggregate is index-only (no key lookups / sequential scan with a
// residual status predicate). The IncludeProperties API is
// provider-specific, so the widening is applied here rather than in the shared configuration.
modelBuilder.Entity<FailedMessageEntity>()
.HasIndex(e => new { e.Status, e.LastModified })
.IncludeProperties(nameof(FailedMessageEntity.FirstTimeOfFailure), nameof(FailedMessageEntity.LastTimeOfFailure));

modelBuilder.Entity<FailedMessageGroupEntity>()
.HasIndex(e => new { e.Type, e.GroupId })
.IncludeProperties(nameof(FailedMessageGroupEntity.Title));
}

public override bool IsDuplicateKeyException(DbUpdateException exception)
Expand Down
Loading
Loading