Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate the payment database #74

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion src/EventHub.Admin.HttpApi.Host/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"Default": "Host=localhost;Database=EventHub;Username=root;Password=root;Port=5432"
"Default": "Host=localhost;Database=EventHub;Username=root;Password=root;Port=5432",
"Payment": "Server=(LocalDb)\\MSSQLLocalDB;Database=EventHubPayment;Trusted_Connection=True"
},
"Redis": {
"Configuration": "localhost"
Expand Down
3 changes: 2 additions & 1 deletion src/EventHub.BackgroundServices/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"Default": "Host=localhost;Database=EventHub;Username=root;Password=root;Port=5432"
"Default": "Host=localhost;Database=EventHub;Username=root;Password=root;Port=5432",
"Payment": "Server=(LocalDb)\\MSSQLLocalDB;Database=EventHubPayment;Trusted_Connection=True"
},
"Redis": {
"Configuration": "127.0.0.1"
Expand Down
3 changes: 2 additions & 1 deletion src/EventHub.DbMigrator/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"Default": "Host=localhost;Database=EventHub;Username=root;Password=root;Port=5432"
"Default": "Host=localhost;Database=EventHub;Username=root;Password=root;Port=5432",
"Payment": "Server=(LocalDb)\\MSSQLLocalDB;Database=EventHubPayment;Trusted_Connection=True"
},
"IdentityServer": {
"Clients": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using EventHub.Data;
using EventHub.EntityFrameworkCore.Payment;
using Volo.Abp.DependencyInjection;

namespace EventHub.EntityFrameworkCore
Expand Down Expand Up @@ -30,6 +31,11 @@ await _serviceProvider
.GetRequiredService<EventHubDbContext>()
.Database
.MigrateAsync();

await _serviceProvider
.GetRequiredService<EventHubPaymentDbContext>()
.Database
.MigrateAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using EventHub.Organizations;
using EventHub.Organizations.Memberships;
using Microsoft.EntityFrameworkCore;
using Payment.EntityFrameworkCore;
using System;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
Expand Down Expand Up @@ -69,7 +68,6 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.ConfigureIdentity();
builder.ConfigureIdentityServer();
builder.ConfigureBlobStoring();
builder.ConfigurePayment();
builder.ConfigureEventHub();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.PostgreSql;
using System;
using EventHub.EntityFrameworkCore.Payment;
using Payment.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.SqlServer;

namespace EventHub.EntityFrameworkCore
{
Expand All @@ -24,7 +26,8 @@ namespace EventHub.EntityFrameworkCore
typeof(AbpBackgroundJobsEntityFrameworkCoreModule),
typeof(AbpAuditLoggingEntityFrameworkCoreModule),
typeof(BlobStoringDatabaseEntityFrameworkCoreModule),
typeof(PaymentEntityFrameworkCoreModule)
typeof(PaymentEntityFrameworkCoreModule),
typeof(AbpEntityFrameworkCoreSqlServerModule)
)]
public class EventHubEntityFrameworkCoreModule : AbpModule
{
Expand All @@ -43,10 +46,21 @@ public override void ConfigureServices(ServiceConfigurationContext context)
{
options.AddDefaultRepositories();
});

/* Registering the payment dbcontext and replacing the module's dbcontext */
context.Services.AddAbpDbContext<EventHubPaymentDbContext>();

Configure<AbpDbContextOptions>(options =>
{
options.UseNpgsql();

/* While the default db provider is PostgreSQl (because of `options.UseNpgsql()` above),
* We are configuring to use SQL Server for the payment database
*/
options.Configure<EventHubPaymentDbContext>(opts =>
{
opts.UseSqlServer();
});
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Payment;

namespace EventHub.EntityFrameworkCore.Payment
{
/* This class is needed for EF Core console commands
* (like Add-Migration and Update-Database commands) */
public class EventHubPaymentDbContextFactory : IDesignTimeDbContextFactory<EventHubPaymentDbContext>
{
public EventHubPaymentDbContext CreateDbContext(string[] args)
{
EventHubEfCoreEntityExtensionMappings.Configure();

var configuration = BuildConfiguration();

var builder = new DbContextOptionsBuilder<EventHubPaymentDbContext>()
.UseSqlServer(configuration.GetConnectionString(PaymentDbProperties.ConnectionStringName));

return new EventHubPaymentDbContext(builder.Options);
}

private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../EventHub.DbMigrator/"))
.AddJsonFile("appsettings.json", optional: false);

return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore;
using Payment;
using Payment.EntityFrameworkCore;
using Payment.PaymentRequests;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;

namespace EventHub.EntityFrameworkCore.Payment
{
/*
* This class replaces the IPaymentDbContext (defined by the Payment module)
* using the [ReplaceDbContext] attribute and implementing the IPaymentDbContext interface.
* It also declares the [ConnectionStringName] attribute to use the Payment connection string name
* instead of the Default in appsettings.json files.
* Finally, it calls modelBuilder.ConfigurePayment() extension method of the Payment module
* to configure the database mappings.
*/
[ReplaceDbContext(typeof(IPaymentDbContext))]
[ConnectionStringName(PaymentDbProperties.ConnectionStringName)]
public class EventHubPaymentDbContext : AbpDbContext<EventHubPaymentDbContext>, IPaymentDbContext
{
public DbSet<PaymentRequest> PaymentRequests { get; set; }

public EventHubPaymentDbContext(
DbContextOptions<EventHubPaymentDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ConfigurePayment();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Volo.Abp.BlobStoring.Database.EntityFrameworkCore" Version="5.0.0-rc.1" />
<ProjectReference Include="..\EventHub.Domain\EventHub.Domain.csproj" />
<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="5.0.0-rc.1" />
<PackageReference Include="Volo.Abp.EntityFrameworkCore.SqlServer" Version="5.0.0-rc.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-rc.*" />
<PackageReference Include="Volo.Abp.PermissionManagement.EntityFrameworkCore" Version="5.0.0-rc.1" />
<PackageReference Include="Volo.Abp.SettingManagement.EntityFrameworkCore" Version="5.0.0-rc.1" />
Expand Down
Loading