Skip to content

Commit

Permalink
Move DB migrations into separate manager apps (#2)
Browse files Browse the repository at this point in the history
* Factor out catalog db management into separate app

* Add health check for DB migrations

* Update to latest preview.3 build

* Move ordering DB migrations into separate app

* Update global.json
  • Loading branch information
DamianEdwards authored Feb 3, 2024
1 parent 7f7a473 commit 9bdbaf7
Show file tree
Hide file tree
Showing 66 changed files with 371 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AspnetVersion>8.0.1</AspnetVersion>
<MicrosoftExtensionsVersion>8.1.0</MicrosoftExtensionsVersion>
<EfVersion>8.0.1</EfVersion>
<AspireVersion>8.0.0-preview.3.24078.5</AspireVersion>
<AspireVersion>8.0.0-preview.3.24081.13</AspireVersion>
<GrpcVersion>2.59.0</GrpcVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
24 changes: 24 additions & 0 deletions eShop.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Keycloak", "Keycloak", "{08
src\Keycloak\data\import\eshop-realm.json = src\Keycloak\data\import\eshop-realm.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Catalog.Data", "src\Catalog.Data\Catalog.Data.csproj", "{BFF57633-BF05-4D32-826C-212AE7858539}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Catalog.Data.Manager", "src\Catalog.Data.Manager\Catalog.Data.Manager.csproj", "{E1AAD2C8-97A7-404E-9DF7-89A719101631}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.Data", "src\Ordering.Data\Ordering.Data.csproj", "{D68FAD25-C09D-434C-A7AE-8F78DB2D923D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ordering.Data.Manager", "src\Ordering.Data.Manager\Ordering.Data.Manager.csproj", "{4FCA8863-B3C7-47D6-B031-37A0384B40AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -58,6 +66,22 @@ Global
{2F3143A9-1565-43BF-8BF2-18B13F604CE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F3143A9-1565-43BF-8BF2-18B13F604CE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F3143A9-1565-43BF-8BF2-18B13F604CE9}.Release|Any CPU.Build.0 = Release|Any CPU
{BFF57633-BF05-4D32-826C-212AE7858539}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFF57633-BF05-4D32-826C-212AE7858539}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFF57633-BF05-4D32-826C-212AE7858539}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFF57633-BF05-4D32-826C-212AE7858539}.Release|Any CPU.Build.0 = Release|Any CPU
{E1AAD2C8-97A7-404E-9DF7-89A719101631}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1AAD2C8-97A7-404E-9DF7-89A719101631}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1AAD2C8-97A7-404E-9DF7-89A719101631}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1AAD2C8-97A7-404E-9DF7-89A719101631}.Release|Any CPU.Build.0 = Release|Any CPU
{D68FAD25-C09D-434C-A7AE-8F78DB2D923D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D68FAD25-C09D-434C-A7AE-8F78DB2D923D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D68FAD25-C09D-434C-A7AE-8F78DB2D923D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D68FAD25-C09D-434C-A7AE-8F78DB2D923D}.Release|Any CPU.Build.0 = Release|Any CPU
{4FCA8863-B3C7-47D6-B031-37A0384B40AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4FCA8863-B3C7-47D6-B031-37A0384B40AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4FCA8863-B3C7-47D6-B031-37A0384B40AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4FCA8863-B3C7-47D6-B031-37A0384B40AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.200-preview",
"version": "8.0.200-preview.23624.5",
"rollForward": "latestPatch",
"allowPrerelease": true
}
Expand Down
6 changes: 3 additions & 3 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<configuration>
<packageSources>
<clear />
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
<!-- <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" /> -->
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<packageSourceMapping>
<packageSource key="dotnet8">
<!-- <packageSource key="dotnet8">
<package pattern="*" />
</packageSource>
</packageSource> -->
<packageSource key="dotnet-tools">
<package pattern="*" />
</packageSource>
Expand Down
10 changes: 5 additions & 5 deletions src/Catalog.API/Apis/CatalogApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using eShop.Catalog.API;
using eShop.Catalog.API.Data;
using eShop.Catalog.API.Model;
using eShop.Catalog.Data;

namespace Microsoft.AspNetCore.Builder;

Expand Down Expand Up @@ -180,17 +180,17 @@ public static async Task<Ok<PaginatedItems<CatalogItem>>> GetItemsByBrandId(
var pageSize = paginationRequest.PageSize;
var pageIndex = paginationRequest.PageIndex;

var root = (IQueryable<CatalogItem>)services.DbContext.CatalogItems;
var query = (IQueryable<CatalogItem>)services.DbContext.CatalogItems;

if (brandId is not null)
{
root = root.Where(ci => ci.CatalogBrandId == brandId);
query = query.Where(ci => ci.CatalogBrandId == brandId);
}

var totalItems = await root
var totalItems = await query
.LongCountAsync();

var itemsOnPage = await root
var itemsOnPage = await query
.Skip(pageSize * pageIndex)
.Take(pageSize)
.AsNoTracking()
Expand Down
19 changes: 1 addition & 18 deletions src/Catalog.API/Catalog.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,8 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Shared\ActivityExtensions.cs" Link="Extensions\ActivityExtensions.cs" />
<Compile Include="..\Shared\MigrateDbContextExtensions.cs" Link="Extensions\MigrateDbContextExtensions.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Catalog.Data\Catalog.Data.csproj" />
<ProjectReference Include="..\eShop.ServiceDefaults\eShop.ServiceDefaults.csproj" />
</ItemGroup>


<ItemGroup>
<InternalsVisibleTo Include="Catalog.FunctionalTests" />
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions src/Catalog.API/CatalogOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class CatalogOptions

public string PicBasePathFormat { get; set; } = "items/{0}/pic/";

public bool UseCustomizationData { get; set; }

public string GetPictureUrl(int catalogItemId)
{
// PERF: Not ideal
Expand Down
5 changes: 1 addition & 4 deletions src/Catalog.API/Extensions/HostingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using eShop.Catalog.API;
using eShop.Catalog.API.Data;
using eShop.Catalog.Data;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Hosting;
Expand All @@ -10,9 +10,6 @@ public static void AddApplicationServices(this IHostApplicationBuilder builder)
{
builder.AddNpgsqlDbContext<CatalogDbContext>("CatalogDB");

// TODO: Move this to a CatalogDbManager project
builder.Services.AddMigration<CatalogDbContext, CatalogContextSeed>();

builder.Services.Configure<CatalogOptions>(builder.Configuration.GetSection(nameof(CatalogOptions)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Catalog.API/Model/CatalogServices.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Options;
using eShop.Catalog.API.Data;
using eShop.Catalog.Data;

namespace eShop.Catalog.API.Model;

Expand Down
2 changes: 1 addition & 1 deletion src/Catalog.API/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"CatalogDB": "Host=localhost;Database=CatalogDB;Username=postgres"
},
"CatalogOptions": {
"PicBasePathFormat": "items/{0}/pic/"

}
}
2 changes: 1 addition & 1 deletion src/Catalog.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"SubscriptionClientName": "Catalog"
},
"CatalogOptions": {
"UseCustomizationData": false
"PicBasePathFormat": "items/{0}/pic/"
}
}
28 changes: 28 additions & 0 deletions src/Catalog.Data.Manager/Catalog.Data.Manager.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>eShop.Catalog.Data.Manager</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Catalog.Data\Catalog.Data.csproj" />
<ProjectReference Include="..\eShop.ServiceDefaults\eShop.ServiceDefaults.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Compile Include="..\Shared\ActivityExtensions.cs" Link="Extensions\ActivityExtensions.cs" />
<Compile Include="..\Shared\MigrateDbContextExtensions.cs" Link="Extensions\MigrateDbContextExtensions.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Npgsql;

namespace eShop.Catalog.API.Data;
namespace eShop.Catalog.Data.Manager;

public partial class CatalogContextSeed(IWebHostEnvironment env, IOptions<CatalogOptions> settings, ILogger<CatalogContextSeed> logger)
public partial class CatalogContextSeed(IWebHostEnvironment env, ILogger<CatalogContextSeed> logger)
: IDbSeeder<CatalogDbContext>
{
public async Task SeedAsync(CatalogDbContext context)
{
var useCustomizationData = settings.Value.UseCustomizationData;
var contentRootPath = env.ContentRootPath;
var picturePath = env.WebRootPath;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#nullable disable

namespace eShop.Catalog.API.Data.Migrations
namespace eShop.Catalog.Data.Manager.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable disable

namespace eShop.Catalog.API.Data.Migrations
namespace eShop.Catalog.Data.Manager.Migrations
{
/// <inheritdoc />
public partial class RemoveHiLoAndIndexCatalogName : Migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using eShop.Catalog.API.Data;
using eShop.Catalog.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace eShop.Catalog.API.Data.Migrations
namespace eShop.Catalog.Data.Manager.Migrations
{
[DbContext(typeof(CatalogDbContext))]
partial class CatalogContextModelSnapshot : ModelSnapshot
Expand All @@ -17,12 +17,12 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.0-rtm.23512.13")
.HasAnnotation("ProductVersion", "8.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("eShop.Catalog.API.Model.CatalogBrand", b =>
modelBuilder.Entity("eShop.Catalog.Data.CatalogBrand", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand All @@ -40,7 +40,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("CatalogBrand", (string)null);
});

modelBuilder.Entity("eShop.Catalog.API.Model.CatalogItem", b =>
modelBuilder.Entity("eShop.Catalog.Data.CatalogItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand Down Expand Up @@ -91,7 +91,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("Catalog", (string)null);
});

modelBuilder.Entity("eShop.Catalog.API.Model.CatalogType", b =>
modelBuilder.Entity("eShop.Catalog.Data.CatalogType", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
Expand All @@ -109,15 +109,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("CatalogType", (string)null);
});

modelBuilder.Entity("eShop.Catalog.API.Model.CatalogItem", b =>
modelBuilder.Entity("eShop.Catalog.Data.CatalogItem", b =>
{
b.HasOne("eShop.Catalog.API.Model.CatalogBrand", "CatalogBrand")
b.HasOne("eShop.Catalog.Data.CatalogBrand", "CatalogBrand")
.WithMany()
.HasForeignKey("CatalogBrandId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.HasOne("eShop.Catalog.API.Model.CatalogType", "CatalogType")
b.HasOne("eShop.Catalog.Data.CatalogType", "CatalogType")
.WithMany()
.HasForeignKey("CatalogTypeId")
.OnDelete(DeleteBehavior.Cascade)
Expand Down
19 changes: 19 additions & 0 deletions src/Catalog.Data.Manager/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using eShop.Catalog.Data;
using eShop.Catalog.Data.Manager;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddNpgsqlDbContext<CatalogDbContext>("CatalogDB", null,
optionsBuilder => optionsBuilder.UseNpgsql(npgsqlBuilder =>
npgsqlBuilder.MigrationsAssembly(typeof(Program).Assembly.GetName().Name)));

builder.Services.AddMigration<CatalogDbContext, CatalogContextSeed>();

var app = builder.Build();

app.MapDefaultEndpoints();

app.Run();
Loading

0 comments on commit 9bdbaf7

Please sign in to comment.