forked from DevBetterCom/DevBetterWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabling Local Development on Macs with Apple Silicon (arm64) 🍏 (DevB…
…etterCom#1274) * Add docker compose with a sql edge container * Updates razor markup so that the new Local environment includes and excludes client side resources the same as Development * Remove commented code in the Dockerfile * Test migrations only run for the 'Local' environment: - Extract the actual migration apply to a separate service to test the environment logic. - Add unit test for the MigrationService extension * Remove docker ignore from csproj configuration section * Code refactor: Clean empty lines * Removes Local environment in favor of docker environment variable file - Now migrations are run for "Development" environment, but only if running from a container - Removes appsettings.Local.json - Removes Local environment from razor markdown - Updates unit tests * Adds README instructions Signed-off-by: davidchaparro <[email protected]> * Put 'Run with docker' section after the 'EF Migrations Commands' section Signed-off-by: davidchaparro <[email protected]> * Fixes null reference warning Signed-off-by: davidchaparro <[email protected]> --------- Signed-off-by: davidchaparro <[email protected]>
- Loading branch information
Showing
10 changed files
with
243 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/.idea | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
LOCAL_SQL_PASSWORD=L0c4l_S3cr3t_P4a5sw0rD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base | ||
WORKDIR /app | ||
EXPOSE 80 | ||
EXPOSE 443 | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build | ||
ARG BUILD_CONFIGURATION=Debug | ||
COPY *.sln . | ||
COPY . . | ||
RUN dotnet restore | ||
WORKDIR "/src/DevBetterWeb.Web" | ||
RUN dotnet build "DevBetterWeb.Web.csproj" -c $BUILD_CONFIGURATION -o /app/build | ||
|
||
FROM build AS publish | ||
ARG BUILD_CONFIGURATION=Debug | ||
RUN dotnet publish "DevBetterWeb.Web.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
# Optional: Set this here if not setting it from docker-compose.yml | ||
ENV ASPNETCORE_ENVIRONMENT=Local | ||
ENTRYPOINT ["dotnet", "DevBetterWeb.Web.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
services: | ||
web: | ||
image: dev-better-web | ||
container_name: dev-better-web | ||
depends_on: | ||
- database | ||
ports: | ||
- 80:80 | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
networks: | ||
- my-network | ||
environment: | ||
ASPNETCORE_ENVIRONMENT: Development | ||
ConnectionStrings:DefaultConnection: Server=database,1433;MultipleActiveResultSets=true;User Id=sa;Password=${LOCAL_SQL_PASSWORD};Encrypt=false | ||
|
||
database: | ||
image: mcr.microsoft.com/azure-sql-edge | ||
cap_add: | ||
- SYS_PTRACE | ||
environment: | ||
- ACCEPT_EULA=1 | ||
- MSSQL_SA_PASSWORD=${LOCAL_SQL_PASSWORD} | ||
ports: | ||
- 1433:1433 | ||
container_name: ms-sql | ||
command: | ||
- "/bin/sh" | ||
- "-c" | ||
- "/opt/mssql/bin/launchpadd -usens=false -enableOutboundAccess=true -usesameuser=true -sqlGroup root -- -reparentOrphanedDescendants=true -useDefaultLaunchers=false & /app/asdepackage/AsdePackage & /opt/mssql/bin/sqlservr" | ||
privileged: true | ||
networks: | ||
- my-network | ||
|
||
networks: | ||
my-network: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DevBetterWeb.Web; | ||
|
||
public interface ILocalMigrationService<TContext> where TContext : DbContext | ||
{ | ||
Task ApplyDatabaseMigrationsAsync(); | ||
} | ||
|
||
public class MigrationService<TContext> : ILocalMigrationService<TContext> | ||
where TContext : DbContext | ||
{ | ||
private readonly ILogger<MigrationService<TContext>> _logger; | ||
|
||
private readonly TContext _dbContext; | ||
|
||
public MigrationService( | ||
TContext context, | ||
ILoggerFactory loggerFactory) | ||
{ | ||
_logger = loggerFactory.CreateLogger<MigrationService<TContext>>(); | ||
_dbContext = context; | ||
} | ||
|
||
public async Task ApplyDatabaseMigrationsAsync() | ||
{ | ||
if (await NoPendingMigrationsAsync(_dbContext)) | ||
{ | ||
_logger.LogInformation($"No pending migrations to apply for context: {typeof(TContext).Name}"); | ||
return; | ||
} | ||
|
||
_logger.LogInformation($"Applying pending migrations for context: {typeof(TContext).Name}..."); | ||
await _dbContext.Database.MigrateAsync(); | ||
_logger.LogInformation($"Migrations applied successfully for context: {typeof(TContext).Name}"); | ||
} | ||
|
||
private static async Task<bool> NoPendingMigrationsAsync(TContext dbContext) => | ||
!(await dbContext.Database.GetPendingMigrationsAsync()).Any(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace DevBetterWeb.Web; | ||
|
||
public static class MigrationServiceExtensions { | ||
|
||
public static async Task ApplyLocalMigrationAsync<TContext>( | ||
this ILocalMigrationService<TContext> migrationService, | ||
string environmentName, | ||
bool runningIncontainer) | ||
where TContext : DbContext | ||
{ | ||
if (environmentName is not "Development" | ||
|| !runningIncontainer) | ||
{ | ||
return; | ||
} | ||
|
||
await migrationService.ApplyDatabaseMigrationsAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/DevBetterWeb.UnitTests/Web/Extensions/MigrationServiceExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Threading.Tasks; | ||
using DevBetterWeb.Web; | ||
using Microsoft.EntityFrameworkCore; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace DevBetterWeb.UnitTests.Web.Extensions; | ||
|
||
public class MigrationServiceExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData("Development", true, 1)] | ||
[InlineData("Development", false, 0)] | ||
[InlineData("Prod", true, 0)] | ||
public async Task OnlyMigratesWhenEnvironmentIsDevelopment( | ||
string environment, | ||
bool runningInContainer, | ||
int expectedRuns) | ||
{ | ||
var migrationService = Substitute.For<ILocalMigrationService<DbContext>>(); | ||
await migrationService.ApplyLocalMigrationAsync(environment, runningInContainer); | ||
|
||
await migrationService.Received(expectedRuns).ApplyDatabaseMigrationsAsync(); | ||
} | ||
} |