Changing to database schema to ensure separations of concerns instead of connection string

This commit is contained in:
Jonathan Bourdon
2024-07-06 23:12:39 -04:00
parent ca94dba08d
commit f6a434c440
12 changed files with 211 additions and 12 deletions

View File

@@ -8,6 +8,8 @@ public class ContentDbContext(
{ {
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.HasDefaultSchema("Content");
modelBuilder modelBuilder
.Entity<Content>() .Entity<Content>()
.Property(c => c.CreatedAt) .Property(c => c.CreatedAt)

View File

@@ -0,0 +1,59 @@
// <auto-generated />
using System;
using Hutopy.Web.Contents.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Hutopy.Web.Contents.Migrations
{
[DbContext(typeof(ContentDbContext))]
[Migration("20240707030928_ChangingContentDefaultSchema")]
partial class ChangingContentDefaultSchema
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("Content")
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Hutopy.Web.Contents.Data.Content", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid>("CreatedBy")
.HasColumnType("uniqueidentifier");
b.Property<string>("Description")
.HasColumnType("nvarchar(max)");
b.Property<string>("Title")
.HasColumnType("nvarchar(max)");
b.Property<string>("Uri")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Contents", "Content");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Hutopy.Web.Contents.Migrations
{
/// <inheritdoc />
public partial class ChangingContentDefaultSchema : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "Content");
migrationBuilder.RenameTable(
name: "Contents",
newName: "Contents",
newSchema: "Content");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameTable(
name: "Contents",
schema: "Content",
newName: "Contents");
}
}
}

View File

@@ -17,6 +17,7 @@ namespace Hutopy.Web.Contents.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasDefaultSchema("Content")
.HasAnnotation("ProductVersion", "8.0.3") .HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128); .HasAnnotation("Relational:MaxIdentifierLength", 128);
@@ -47,7 +48,7 @@ namespace Hutopy.Web.Contents.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Contents"); b.ToTable("Contents", "Content");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@@ -8,6 +8,8 @@ public class MessagingDbContext(
{ {
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.HasDefaultSchema("Messaging");
modelBuilder modelBuilder
.Entity<Message>() .Entity<Message>()
.Property(c => c.CreatedAt) .Property(c => c.CreatedAt)

View File

@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable #nullable disable
namespace Hutopy.Web.Migrations namespace Hutopy.Web.Messages.Migrations
{ {
[DbContext(typeof(ContentDbContext))] [DbContext(typeof(ContentDbContext))]
[Migration("20240707023204_UseSubjectId_InsteadOfContentId")] [Migration("20240707023204_UseSubjectId_InsteadOfContentId")]

View File

@@ -2,7 +2,7 @@
#nullable disable #nullable disable
namespace Hutopy.Web.Migrations namespace Hutopy.Web.Messages.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class UseSubjectId_InsteadOfContentId : Migration public partial class UseSubjectId_InsteadOfContentId : Migration

View File

@@ -0,0 +1,60 @@
// <auto-generated />
using System;
using Hutopy.Web.Messages.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Hutopy.Web.Messages.Migrations
{
[DbContext(typeof(MessagingDbContext))]
[Migration("20240707030834_ChangingMessagingDefaultSchema")]
partial class ChangingMessagingDefaultSchema
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("Messaging")
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Hutopy.Web.Messages.Data.Message", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("datetime2")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid>("CreatedBy")
.HasColumnType("uniqueidentifier");
b.Property<Guid>("ParentId")
.HasColumnType("uniqueidentifier");
b.Property<Guid>("SubjectId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Messages", "Messaging");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Hutopy.Web.Messages.Migrations
{
/// <inheritdoc />
public partial class ChangingMessagingDefaultSchema : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "Messaging");
migrationBuilder.RenameTable(
name: "Messages",
newName: "Messages",
newSchema: "Messaging");
migrationBuilder.RenameColumn(
name: "ContentId",
schema: "Messaging",
table: "Messages",
newName: "SubjectId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameTable(
name: "Messages",
schema: "Messaging",
newName: "Messages");
migrationBuilder.RenameColumn(
name: "SubjectId",
table: "Messages",
newName: "ContentId");
}
}
}

View File

@@ -17,6 +17,7 @@ namespace Hutopy.Web.Messages.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasDefaultSchema("Messaging")
.HasAnnotation("ProductVersion", "8.0.3") .HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128); .HasAnnotation("Relational:MaxIdentifierLength", 128);
@@ -28,9 +29,6 @@ namespace Hutopy.Web.Messages.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<Guid>("ContentId")
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreatedAt") b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("datetime2") .HasColumnType("datetime2")
@@ -42,13 +40,16 @@ namespace Hutopy.Web.Messages.Migrations
b.Property<Guid>("ParentId") b.Property<Guid>("ParentId")
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<Guid>("SubjectId")
.HasColumnType("uniqueidentifier");
b.Property<string>("Value") b.Property<string>("Value")
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
b.ToTable("Messages"); b.ToTable("Messages", "Messaging");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@@ -80,14 +80,17 @@ builder.Services.AddOpenApiDocument((configure, sp) =>
builder.Services.AddFastEndpoints(); builder.Services.AddFastEndpoints();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Missing ConnectionStrings:DefaultConnection");
builder.Services.AddDbContext<MessagingDbContext>((_, options) => builder.Services.AddDbContext<MessagingDbContext>((_, options) =>
{ {
options.UseSqlServer(builder.Configuration.GetConnectionString("CommentStore")); options.UseSqlServer(connectionString);
}); });
builder.Services.AddDbContext<ContentDbContext>((_, options) => builder.Services.AddDbContext<ContentDbContext>((_, options) =>
{ {
options.UseSqlServer(builder.Configuration.GetConnectionString("ContentStore")); options.UseSqlServer(connectionString);
}); });
builder.Services.Configure<JwtOptions>(builder.Configuration.GetRequiredSection(JwtOptions.SectionName)); builder.Services.Configure<JwtOptions>(builder.Configuration.GetRequiredSection(JwtOptions.SectionName));

View File

@@ -8,9 +8,7 @@
} }
}, },
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True", "DefaultConnection": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True"
"CommentStore": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True",
"ContentStore": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True"
}, },
"Authentication": { "Authentication": {
"Jwt": { "Jwt": {