Skip to content

Commit

Permalink
Updated project and test configuration to run migrations automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed May 17, 2021
1 parent fa065c6 commit 54fdc6b
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 34 deletions.
7 changes: 5 additions & 2 deletions Core.Marten/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ private static void SetStoreOptions(StoreOptions options, Config config,
{
options.Connection(config.ConnectionString);
options.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
options.Events.DatabaseSchemaName = config.WriteModelSchema;
options.DatabaseSchemaName = config.ReadModelSchema;

var schemaName = Environment.GetEnvironmentVariable("SchemaName");
options.Events.DatabaseSchemaName = schemaName ?? config.WriteModelSchema;
options.DatabaseSchemaName = schemaName ?? config.ReadModelSchema;

options.UseDefaultSerialization(nonPublicMembersStorage: NonPublicMembersStorage.NonPublicSetters,
enumStorage: EnumStorage.AsString);
options.PLV8Enabled = false;
Expand Down
2 changes: 2 additions & 0 deletions Core.Testing/ApiFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public abstract class ApiFixture: IAsyncLifetime

protected ApiFixture()
{
Environment.SetEnvironmentVariable("SchemaName", GetType().Name.ToLower());

Sut = CreateTestContext();
}

Expand Down
2 changes: 1 addition & 1 deletion Core.Testing/TestWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static IWebHostBuilder Create(Dictionary<string, string> configuration, A
configureServices ??= _ => { };

return new WebHostBuilder()
.UseEnvironment("Tests")
.UseEnvironment("Development")
.UseContentRoot(projectDir)
.UseConfiguration(new ConfigurationBuilder()
.SetBasePath(projectDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public CreateMeetingTests(CreateMeetingFixture fixture)
}

[Fact]
[Trait("Category", "Exercise")]
public async Task CreateCommand_ShouldReturn_CreatedStatus_With_MeetingId()
{
var commandResponse = fixture.CommandResponse;
Expand All @@ -58,7 +57,6 @@ public async Task CreateCommand_ShouldReturn_CreatedStatus_With_MeetingId()
}

[Fact]
[Trait("Category", "Exercise")]
public void CreateCommand_ShouldPublish_MeetingCreateEvent()
{
// assert MeetingCreated event was produced to external bus
Expand All @@ -70,7 +68,6 @@ public void CreateCommand_ShouldPublish_MeetingCreateEvent()
}

[Fact]
[Trait("Category", "Exercise")]
public async Task CreateCommand_ShouldUpdateReadModel()
{
// prepare query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public ScheduleMeetingTests(ScheduleMeetingFixture fixture)
}

[Fact]
[Trait("Category", "Exercise")]
public async Task CreateMeeting_ShouldReturn_CreatedStatus_With_MeetingId()
{
var commandResponse = fixture.CreateMeetingCommandResponse.EnsureSuccessStatusCode();
Expand All @@ -64,7 +63,6 @@ public async Task CreateMeeting_ShouldReturn_CreatedStatus_With_MeetingId()
}

[Fact]
[Trait("Category", "Exercise")]
public async Task ScheduleMeeting_ShouldSucceed()
{
var commandResponse = fixture.ScheduleMeetingCommandResponse.EnsureSuccessStatusCode();
Expand All @@ -75,7 +73,6 @@ public async Task ScheduleMeeting_ShouldSucceed()
}

[Fact]
[Trait("Category", "Exercise")]
public async Task ScheduleMeeting_ShouldUpdateReadModel()
{
//send query
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using Core.Testing;
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Warehouse.Products.GettingProductDetails;
using Warehouse.Products.GettingProducts;
using Warehouse.Products.RegisteringProduct;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Castle.Core.Configuration;
using Core.WebApi.Middlewares.ExceptionHandling;
using Core.WebApi.Middlewares.ExceptionHandling;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
Expand All @@ -18,8 +17,6 @@ public static IWebHostBuilder Configure(IWebHostBuilder webHostBuilder, string s
.ConfigureServices(services =>
{
services.AddRouting()
.AddAuthorization()
.AddCors()
.AddWarehouseServices()
.AddTransient<DbContextOptions<WarehouseDBContext>>(s =>
{
Expand All @@ -33,15 +30,13 @@ public static IWebHostBuilder Configure(IWebHostBuilder webHostBuilder, string s
})
.Configure(app =>
{
app.UseHttpsRedirection()
.UseMiddleware(typeof(ExceptionHandlingMiddleware))
app.UseMiddleware(typeof(ExceptionHandlingMiddleware))
.UseRouting()
.UseAuthorization()
.UseEndpoints(endpoints => { endpoints.UseWarehouseEndpoints(); });
.UseEndpoints(endpoints => { endpoints.UseWarehouseEndpoints(); })
.ConfigureWarehouse();

// Kids, do not try this at home!
var database = app.ApplicationServices.GetRequiredService<WarehouseDBContext>().Database;
database.Migrate();
database.ExecuteSqlRaw("TRUNCATE TABLE \"Product\"");
});

Expand Down
9 changes: 3 additions & 6 deletions Sample/Warehouse/Warehouse.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
.ConfigureServices(services =>
{
services.AddRouting()
.AddCors()
.AddAuthorization()
.AddWarehouseServices();
})
.Configure(app =>
{
app.UseHttpsRedirection()
.UseMiddleware(typeof(ExceptionHandlingMiddleware))
app.UseMiddleware(typeof(ExceptionHandlingMiddleware))
.UseRouting()
.UseAuthorization()
.UseEndpoints(endpoints =>
{
endpoints.UseWarehouseEndpoints();
});
})
.ConfigureWarehouse();
});
})
.Build();
Expand Down
16 changes: 15 additions & 1 deletion Sample/Warehouse/Warehouse/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Routing;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Warehouse.Products;
Expand All @@ -16,5 +18,17 @@ public static IServiceCollection AddWarehouseServices(this IServiceCollection se

public static IEndpointRouteBuilder UseWarehouseEndpoints(this IEndpointRouteBuilder endpoints)
=> endpoints.UseProductsEndpoints();

public static IApplicationBuilder ConfigureWarehouse(this IApplicationBuilder app)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

if (environment == "Development")
{
app.ApplicationServices.GetRequiredService<WarehouseDBContext>().Database.Migrate();
}

return app;
}
}
}
11 changes: 8 additions & 3 deletions Sample/Warehouse/Warehouse/Core/Extensions/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Headers;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;

namespace Warehouse.Core.Extensions
{
Expand Down Expand Up @@ -79,8 +80,12 @@ public static async Task<T> FromBody<T>(this HttpContext context)
public static Task OK<T>(this HttpContext context, T result)
=> context.ReturnJSON(result);

public static Task Created<T>(this HttpContext context, T result)
=> context.ReturnJSON(result, HttpStatusCode.Created);
public static Task Created<T>(this HttpContext context, T id, string? location = null)
{
context.Response.Headers[HeaderNames.Location] = location ?? $"{context.Request.Path}{id}";

return context.ReturnJSON(id, HttpStatusCode.Created);
}

public static void NotFound(this HttpContext context)
=> context.Response.StatusCode = (int)HttpStatusCode.NotFound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static T AssertNotNull<T>(this T? value, string? paramName = null)
}

public static string AssertNotEmpty(this string? value, string? paramName = null)
=> string.IsNullOrWhiteSpace(value) ? value! : throw new ArgumentOutOfRangeException(paramName);
=> !string.IsNullOrWhiteSpace(value) ? value : throw new ArgumentOutOfRangeException(paramName);

public static T AssertNotEmpty<T>(this T value, string? paramName = null)
where T : struct
Expand Down
3 changes: 1 addition & 2 deletions Sample/Warehouse/Warehouse/Products/GettingProducts/Route.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Warehouse.Core.Extensions;
Expand Down
1 change: 0 additions & 1 deletion Sample/Warehouse/Warehouse/Products/Product.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Text.Json.Serialization;
using Warehouse.Products.Primitives;

namespace Warehouse.Products
Expand Down

0 comments on commit 54fdc6b

Please sign in to comment.