mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-26 23:57:35 +08:00
Merge pull request #264 from meysamhadeli/refactor/remove_migration_in_message_processor
refactor: Remove migration from message processor
This commit is contained in:
commit
ee2936fa86
@ -1,6 +1,5 @@
|
|||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using BuildingBlocks.Core.Model;
|
using BuildingBlocks.Core.Model;
|
||||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
|
||||||
using BuildingBlocks.Web;
|
using BuildingBlocks.Web;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
@ -114,9 +113,6 @@ public static class Extensions
|
|||||||
{
|
{
|
||||||
using var scope = serviceProvider.CreateScope();
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
|
||||||
var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
|
|
||||||
await persistMessageContext.Database.MigrateAsync();
|
|
||||||
|
|
||||||
var context = scope.ServiceProvider.GetRequiredService<TContext>();
|
var context = scope.ServiceProvider.GetRequiredService<TContext>();
|
||||||
await context.Database.MigrateAsync();
|
await context.Database.MigrateAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +0,0 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Design;
|
|
||||||
|
|
||||||
namespace BuildingBlocks.PersistMessageProcessor.Data;
|
|
||||||
|
|
||||||
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<PersistMessageDbContext>
|
|
||||||
{
|
|
||||||
public PersistMessageDbContext CreateDbContext(string[] args)
|
|
||||||
{
|
|
||||||
var builder = new DbContextOptionsBuilder<PersistMessageDbContext>();
|
|
||||||
|
|
||||||
builder.UseNpgsql("Server=localhost;Port=5432;Database=persist_message;User Id=postgres;Password=postgres;Include Error Detail=true")
|
|
||||||
.UseSnakeCaseNamingConvention();
|
|
||||||
return new PersistMessageDbContext(builder.Options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,77 +0,0 @@
|
|||||||
// <auto-generated />
|
|
||||||
using System;
|
|
||||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
||||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace BuildingBlocks.PersistMessageProcessor.Data.Migrations
|
|
||||||
{
|
|
||||||
[DbContext(typeof(PersistMessageDbContext))]
|
|
||||||
[Migration("20230508215131_initial")]
|
|
||||||
partial class initial
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
#pragma warning disable 612, 618
|
|
||||||
modelBuilder
|
|
||||||
.HasAnnotation("ProductVersion", "7.0.2")
|
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
|
||||||
|
|
||||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
|
||||||
|
|
||||||
modelBuilder.Entity("BuildingBlocks.PersistMessageProcessor.PersistMessage", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("Id")
|
|
||||||
.HasColumnType("uuid")
|
|
||||||
.HasColumnName("id");
|
|
||||||
|
|
||||||
b.Property<DateTime>("Created")
|
|
||||||
.HasColumnType("timestamp with time zone")
|
|
||||||
.HasColumnName("created");
|
|
||||||
|
|
||||||
b.Property<string>("Data")
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasColumnName("data");
|
|
||||||
|
|
||||||
b.Property<string>("DataType")
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasColumnName("data_type");
|
|
||||||
|
|
||||||
b.Property<string>("DeliveryType")
|
|
||||||
.IsRequired()
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasDefaultValue("Outbox")
|
|
||||||
.HasColumnName("delivery_type");
|
|
||||||
|
|
||||||
b.Property<string>("MessageStatus")
|
|
||||||
.IsRequired()
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasDefaultValue("InProgress")
|
|
||||||
.HasColumnName("message_status");
|
|
||||||
|
|
||||||
b.Property<int>("RetryCount")
|
|
||||||
.HasColumnType("integer")
|
|
||||||
.HasColumnName("retry_count");
|
|
||||||
|
|
||||||
b.Property<long>("Version")
|
|
||||||
.IsConcurrencyToken()
|
|
||||||
.HasColumnType("bigint")
|
|
||||||
.HasColumnName("version");
|
|
||||||
|
|
||||||
b.HasKey("Id")
|
|
||||||
.HasName("pk_persist_message");
|
|
||||||
|
|
||||||
b.ToTable("persist_message", (string)null);
|
|
||||||
});
|
|
||||||
#pragma warning restore 612, 618
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
using Microsoft.EntityFrameworkCore.Migrations;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace BuildingBlocks.PersistMessageProcessor.Data.Migrations
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public partial class initial : Migration
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.CreateTable(
|
|
||||||
name: "persist_message",
|
|
||||||
columns: table => new
|
|
||||||
{
|
|
||||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
||||||
datatype = table.Column<string>(name: "data_type", type: "text", nullable: true),
|
|
||||||
data = table.Column<string>(type: "text", nullable: true),
|
|
||||||
created = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
|
||||||
retrycount = table.Column<int>(name: "retry_count", type: "integer", nullable: false),
|
|
||||||
messagestatus = table.Column<string>(name: "message_status", type: "text", nullable: false, defaultValue: "InProgress"),
|
|
||||||
deliverytype = table.Column<string>(name: "delivery_type", type: "text", nullable: false, defaultValue: "Outbox"),
|
|
||||||
version = table.Column<long>(type: "bigint", nullable: false)
|
|
||||||
},
|
|
||||||
constraints: table =>
|
|
||||||
{
|
|
||||||
table.PrimaryKey("pk_persist_message", x => x.id);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Down(MigrationBuilder migrationBuilder)
|
|
||||||
{
|
|
||||||
migrationBuilder.DropTable(
|
|
||||||
name: "persist_message");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,74 +0,0 @@
|
|||||||
// <auto-generated />
|
|
||||||
using System;
|
|
||||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
||||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
namespace BuildingBlocks.PersistMessageProcessor.Data.Migrations
|
|
||||||
{
|
|
||||||
[DbContext(typeof(PersistMessageDbContext))]
|
|
||||||
partial class PersistMessageDbContextModelSnapshot : ModelSnapshot
|
|
||||||
{
|
|
||||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
#pragma warning disable 612, 618
|
|
||||||
modelBuilder
|
|
||||||
.HasAnnotation("ProductVersion", "7.0.2")
|
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
|
||||||
|
|
||||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
|
||||||
|
|
||||||
modelBuilder.Entity("BuildingBlocks.PersistMessageProcessor.PersistMessage", b =>
|
|
||||||
{
|
|
||||||
b.Property<Guid>("Id")
|
|
||||||
.HasColumnType("uuid")
|
|
||||||
.HasColumnName("id");
|
|
||||||
|
|
||||||
b.Property<DateTime>("Created")
|
|
||||||
.HasColumnType("timestamp with time zone")
|
|
||||||
.HasColumnName("created");
|
|
||||||
|
|
||||||
b.Property<string>("Data")
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasColumnName("data");
|
|
||||||
|
|
||||||
b.Property<string>("DataType")
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasColumnName("data_type");
|
|
||||||
|
|
||||||
b.Property<string>("DeliveryType")
|
|
||||||
.IsRequired()
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasDefaultValue("Outbox")
|
|
||||||
.HasColumnName("delivery_type");
|
|
||||||
|
|
||||||
b.Property<string>("MessageStatus")
|
|
||||||
.IsRequired()
|
|
||||||
.ValueGeneratedOnAdd()
|
|
||||||
.HasColumnType("text")
|
|
||||||
.HasDefaultValue("InProgress")
|
|
||||||
.HasColumnName("message_status");
|
|
||||||
|
|
||||||
b.Property<int>("RetryCount")
|
|
||||||
.HasColumnType("integer")
|
|
||||||
.HasColumnName("retry_count");
|
|
||||||
|
|
||||||
b.Property<long>("Version")
|
|
||||||
.IsConcurrencyToken()
|
|
||||||
.HasColumnType("bigint")
|
|
||||||
.HasColumnName("version");
|
|
||||||
|
|
||||||
b.HasKey("Id")
|
|
||||||
.HasName("pk_persist_message");
|
|
||||||
|
|
||||||
b.ToTable("persist_message", (string)null);
|
|
||||||
});
|
|
||||||
#pragma warning restore 612, 618
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -57,6 +57,29 @@ public class PersistMessageDbContext : DbContext, IPersistMessageDbContext
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void CreatePersistMessageTable()
|
||||||
|
{
|
||||||
|
if (Database.GetPendingMigrations().Any())
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Cannot create table if there are pending migrations.");
|
||||||
|
}
|
||||||
|
|
||||||
|
string createTableSql = @"
|
||||||
|
create table if not exists persist_message (
|
||||||
|
id uuid not null,
|
||||||
|
data_type text,
|
||||||
|
data text,
|
||||||
|
created timestamp with time zone not null,
|
||||||
|
retry_count integer not null,
|
||||||
|
message_status text not null default 'InProgress'::text,
|
||||||
|
delivery_type text not null default 'Outbox'::text,
|
||||||
|
version bigint not null,
|
||||||
|
constraint pk_persist_message primary key (id)
|
||||||
|
)";
|
||||||
|
|
||||||
|
Database.ExecuteSqlRaw(createTableSql);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnBeforeSaving()
|
private void OnBeforeSaving()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@ -1,2 +0,0 @@
|
|||||||
dotnet ef migrations add initial --context PersistMessageDbContext -o "PersistMessageProcessor\Data\Migrations"
|
|
||||||
dotnet ef database update --context PersistMessageDbContext
|
|
||||||
@ -30,7 +30,15 @@ public static class Extensions
|
|||||||
.UseSnakeCaseNamingConvention();
|
.UseSnakeCaseNamingConvention();
|
||||||
});
|
});
|
||||||
|
|
||||||
services.AddScoped<IPersistMessageDbContext>(provider => provider.GetService<PersistMessageDbContext>());
|
services.AddScoped<IPersistMessageDbContext>(provider =>
|
||||||
|
{
|
||||||
|
var persistMessageDbContext = provider.GetRequiredService<PersistMessageDbContext>();
|
||||||
|
|
||||||
|
persistMessageDbContext.Database.EnsureCreated();
|
||||||
|
persistMessageDbContext.CreatePersistMessageTable();
|
||||||
|
|
||||||
|
return persistMessageDbContext;
|
||||||
|
});
|
||||||
|
|
||||||
services.AddScoped<IPersistMessageProcessor, PersistMessageProcessor>();
|
services.AddScoped<IPersistMessageProcessor, PersistMessageProcessor>();
|
||||||
|
|
||||||
@ -41,19 +49,4 @@ public static class Extensions
|
|||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IApplicationBuilder UseMigrationPersistMessage<TContext>(this IApplicationBuilder app,
|
|
||||||
IWebHostEnvironment env)
|
|
||||||
where TContext : DbContext, IPersistMessageDbContext
|
|
||||||
{
|
|
||||||
using var scope = app.ApplicationServices.CreateScope();
|
|
||||||
|
|
||||||
var persistMessageContext = scope.ServiceProvider.GetRequiredService<PersistMessageDbContext>();
|
|
||||||
persistMessageContext.Database.Migrate();
|
|
||||||
|
|
||||||
var context = scope.ServiceProvider.GetRequiredService<TContext>();
|
|
||||||
context.Database.Migrate();
|
|
||||||
|
|
||||||
return app;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,8 +24,6 @@ using Serilog;
|
|||||||
|
|
||||||
namespace Booking.Extensions.Infrastructure;
|
namespace Booking.Extensions.Infrastructure;
|
||||||
|
|
||||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
|
||||||
|
|
||||||
public static class InfrastructureExtensions
|
public static class InfrastructureExtensions
|
||||||
{
|
{
|
||||||
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
|
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
|
||||||
@ -97,7 +95,6 @@ public static class InfrastructureExtensions
|
|||||||
});
|
});
|
||||||
app.UseCorrelationId();
|
app.UseCorrelationId();
|
||||||
app.UseHttpMetrics();
|
app.UseHttpMetrics();
|
||||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
|
||||||
app.UseCustomHealthCheck();
|
app.UseCustomHealthCheck();
|
||||||
app.MapMetrics();
|
app.MapMetrics();
|
||||||
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));
|
app.MapGet("/", x => x.Response.WriteAsync(appOptions.Name));
|
||||||
|
|||||||
@ -102,7 +102,6 @@ public static class InfrastructureExtensions
|
|||||||
});
|
});
|
||||||
app.UseCorrelationId();
|
app.UseCorrelationId();
|
||||||
app.UseHttpMetrics();
|
app.UseHttpMetrics();
|
||||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
|
||||||
app.UseMigration<FlightDbContext>(env);
|
app.UseMigration<FlightDbContext>(env);
|
||||||
app.MapMetrics();
|
app.MapMetrics();
|
||||||
app.UseCustomHealthCheck();
|
app.UseCustomHealthCheck();
|
||||||
|
|||||||
@ -99,7 +99,6 @@ public static class InfrastructureExtensions
|
|||||||
{
|
{
|
||||||
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
|
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
|
||||||
});
|
});
|
||||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
|
||||||
app.UseMigration<IdentityContext>(env);
|
app.UseMigration<IdentityContext>(env);
|
||||||
app.UseCorrelationId();
|
app.UseCorrelationId();
|
||||||
app.UseHttpMetrics();
|
app.UseHttpMetrics();
|
||||||
|
|||||||
@ -26,8 +26,6 @@ using Serilog;
|
|||||||
|
|
||||||
namespace Passenger.Extensions.Infrastructure;
|
namespace Passenger.Extensions.Infrastructure;
|
||||||
|
|
||||||
using BuildingBlocks.PersistMessageProcessor.Data;
|
|
||||||
|
|
||||||
public static class InfrastructureExtensions
|
public static class InfrastructureExtensions
|
||||||
{
|
{
|
||||||
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
|
public static WebApplicationBuilder AddInfrastructure(this WebApplicationBuilder builder)
|
||||||
@ -94,7 +92,6 @@ public static class InfrastructureExtensions
|
|||||||
{
|
{
|
||||||
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
|
options.EnrichDiagnosticContext = LogEnrichHelper.EnrichFromRequest;
|
||||||
});
|
});
|
||||||
app.UseMigrationPersistMessage<PersistMessageDbContext>(env);
|
|
||||||
app.UseMigration<PassengerDbContext>(env);
|
app.UseMigration<PassengerDbContext>(env);
|
||||||
app.UseCorrelationId();
|
app.UseCorrelationId();
|
||||||
app.UseHttpMetrics();
|
app.UseHttpMetrics();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user