From f6a434c440642ea9931a0f41b7def03e7bc86c61 Mon Sep 17 00:00:00 2001 From: Jonathan Bourdon Date: Sat, 6 Jul 2024 23:12:39 -0400 Subject: [PATCH] Changing to database schema to ensure separations of concerns instead of connection string --- src/Web/Contents/Data/ContentDbContext.cs | 2 + ...8_ChangingContentDefaultSchema.Designer.cs | 59 ++++++++++++++++++ ...0707030928_ChangingContentDefaultSchema.cs | 31 ++++++++++ .../ContentDbContextModelSnapshot.cs | 3 +- src/Web/Messages/Data/MessagingDbContext.cs | 2 + ...seSubjectId_InsteadOfContentId.Designer.cs | 2 +- ...7023204_UseSubjectId_InsteadOfContentId.cs | 2 +- ...ChangingMessagingDefaultSchema.Designer.cs | 60 +++++++++++++++++++ ...07030834_ChangingMessagingDefaultSchema.cs | 42 +++++++++++++ .../MessagingDbContextModelSnapshot.cs | 9 +-- src/Web/Program.cs | 7 ++- src/Web/appsettings.Development.json | 4 +- 12 files changed, 211 insertions(+), 12 deletions(-) create mode 100644 src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.Designer.cs create mode 100644 src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.cs rename src/Web/{ => Messages}/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs (97%) rename src/Web/{ => Messages}/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs (91%) create mode 100644 src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.Designer.cs create mode 100644 src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.cs diff --git a/src/Web/Contents/Data/ContentDbContext.cs b/src/Web/Contents/Data/ContentDbContext.cs index f525cbf..bf6eaf5 100644 --- a/src/Web/Contents/Data/ContentDbContext.cs +++ b/src/Web/Contents/Data/ContentDbContext.cs @@ -8,6 +8,8 @@ public class ContentDbContext( { protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.HasDefaultSchema("Content"); + modelBuilder .Entity() .Property(c => c.CreatedAt) diff --git a/src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.Designer.cs b/src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.Designer.cs new file mode 100644 index 0000000..2555444 --- /dev/null +++ b/src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.Designer.cs @@ -0,0 +1,59 @@ +// +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 + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("CURRENT_TIMESTAMP"); + + b.Property("CreatedBy") + .HasColumnType("uniqueidentifier"); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .HasColumnType("nvarchar(max)"); + + b.Property("Uri") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Contents", "Content"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.cs b/src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.cs new file mode 100644 index 0000000..df1bb9f --- /dev/null +++ b/src/Web/Contents/Migrations/20240707030928_ChangingContentDefaultSchema.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Hutopy.Web.Contents.Migrations +{ + /// + public partial class ChangingContentDefaultSchema : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.EnsureSchema( + name: "Content"); + + migrationBuilder.RenameTable( + name: "Contents", + newName: "Contents", + newSchema: "Content"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameTable( + name: "Contents", + schema: "Content", + newName: "Contents"); + } + } +} diff --git a/src/Web/Contents/Migrations/ContentDbContextModelSnapshot.cs b/src/Web/Contents/Migrations/ContentDbContextModelSnapshot.cs index 1892c37..4f2d601 100644 --- a/src/Web/Contents/Migrations/ContentDbContextModelSnapshot.cs +++ b/src/Web/Contents/Migrations/ContentDbContextModelSnapshot.cs @@ -17,6 +17,7 @@ namespace Hutopy.Web.Contents.Migrations { #pragma warning disable 612, 618 modelBuilder + .HasDefaultSchema("Content") .HasAnnotation("ProductVersion", "8.0.3") .HasAnnotation("Relational:MaxIdentifierLength", 128); @@ -47,7 +48,7 @@ namespace Hutopy.Web.Contents.Migrations b.HasKey("Id"); - b.ToTable("Contents"); + b.ToTable("Contents", "Content"); }); #pragma warning restore 612, 618 } diff --git a/src/Web/Messages/Data/MessagingDbContext.cs b/src/Web/Messages/Data/MessagingDbContext.cs index 28bb977..e343a2b 100644 --- a/src/Web/Messages/Data/MessagingDbContext.cs +++ b/src/Web/Messages/Data/MessagingDbContext.cs @@ -8,6 +8,8 @@ public class MessagingDbContext( { protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.HasDefaultSchema("Messaging"); + modelBuilder .Entity() .Property(c => c.CreatedAt) diff --git a/src/Web/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs b/src/Web/Messages/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs similarity index 97% rename from src/Web/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs rename to src/Web/Messages/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs index da741d4..86ed4f1 100644 --- a/src/Web/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs +++ b/src/Web/Messages/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; #nullable disable -namespace Hutopy.Web.Migrations +namespace Hutopy.Web.Messages.Migrations { [DbContext(typeof(ContentDbContext))] [Migration("20240707023204_UseSubjectId_InsteadOfContentId")] diff --git a/src/Web/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs b/src/Web/Messages/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs similarity index 91% rename from src/Web/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs rename to src/Web/Messages/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs index 02c3670..760efd1 100644 --- a/src/Web/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs +++ b/src/Web/Messages/Migrations/20240707023204_UseSubjectId_InsteadOfContentId.cs @@ -2,7 +2,7 @@ #nullable disable -namespace Hutopy.Web.Migrations +namespace Hutopy.Web.Messages.Migrations { /// public partial class UseSubjectId_InsteadOfContentId : Migration diff --git a/src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.Designer.cs b/src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.Designer.cs new file mode 100644 index 0000000..dacf0b2 --- /dev/null +++ b/src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.Designer.cs @@ -0,0 +1,60 @@ +// +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 + { + /// + 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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("datetime2") + .HasDefaultValueSql("CURRENT_TIMESTAMP"); + + b.Property("CreatedBy") + .HasColumnType("uniqueidentifier"); + + b.Property("ParentId") + .HasColumnType("uniqueidentifier"); + + b.Property("SubjectId") + .HasColumnType("uniqueidentifier"); + + b.Property("Value") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Messages", "Messaging"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.cs b/src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.cs new file mode 100644 index 0000000..c788df7 --- /dev/null +++ b/src/Web/Messages/Migrations/20240707030834_ChangingMessagingDefaultSchema.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Hutopy.Web.Messages.Migrations +{ + /// + public partial class ChangingMessagingDefaultSchema : Migration + { + /// + 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"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameTable( + name: "Messages", + schema: "Messaging", + newName: "Messages"); + + migrationBuilder.RenameColumn( + name: "SubjectId", + table: "Messages", + newName: "ContentId"); + } + } +} diff --git a/src/Web/Messages/Migrations/MessagingDbContextModelSnapshot.cs b/src/Web/Messages/Migrations/MessagingDbContextModelSnapshot.cs index be9a4b9..dcaa429 100644 --- a/src/Web/Messages/Migrations/MessagingDbContextModelSnapshot.cs +++ b/src/Web/Messages/Migrations/MessagingDbContextModelSnapshot.cs @@ -17,6 +17,7 @@ namespace Hutopy.Web.Messages.Migrations { #pragma warning disable 612, 618 modelBuilder + .HasDefaultSchema("Messaging") .HasAnnotation("ProductVersion", "8.0.3") .HasAnnotation("Relational:MaxIdentifierLength", 128); @@ -28,9 +29,6 @@ namespace Hutopy.Web.Messages.Migrations .ValueGeneratedOnAdd() .HasColumnType("uniqueidentifier"); - b.Property("ContentId") - .HasColumnType("uniqueidentifier"); - b.Property("CreatedAt") .ValueGeneratedOnAdd() .HasColumnType("datetime2") @@ -42,13 +40,16 @@ namespace Hutopy.Web.Messages.Migrations b.Property("ParentId") .HasColumnType("uniqueidentifier"); + b.Property("SubjectId") + .HasColumnType("uniqueidentifier"); + b.Property("Value") .IsRequired() .HasColumnType("nvarchar(max)"); b.HasKey("Id"); - b.ToTable("Messages"); + b.ToTable("Messages", "Messaging"); }); #pragma warning restore 612, 618 } diff --git a/src/Web/Program.cs b/src/Web/Program.cs index c7967fa..3aad5cb 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -80,14 +80,17 @@ builder.Services.AddOpenApiDocument((configure, sp) => builder.Services.AddFastEndpoints(); +var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") + ?? throw new InvalidOperationException("Missing ConnectionStrings:DefaultConnection"); + builder.Services.AddDbContext((_, options) => { - options.UseSqlServer(builder.Configuration.GetConnectionString("CommentStore")); + options.UseSqlServer(connectionString); }); builder.Services.AddDbContext((_, options) => { - options.UseSqlServer(builder.Configuration.GetConnectionString("ContentStore")); + options.UseSqlServer(connectionString); }); builder.Services.Configure(builder.Configuration.GetRequiredSection(JwtOptions.SectionName)); diff --git a/src/Web/appsettings.Development.json b/src/Web/appsettings.Development.json index f394dcb..d249e8e 100644 --- a/src/Web/appsettings.Development.json +++ b/src/Web/appsettings.Development.json @@ -8,9 +8,7 @@ } }, "ConnectionStrings": { - "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" + "DefaultConnection": "Server=localhost,1433;Initial Catalog=Hutopy;User Id=sa;Password=P@ssword123!;MultipleActiveResultSets=true;TrustServerCertificate=True;MultiSubnetFailover=True" }, "Authentication": { "Jwt": {