Skip to content

Commit

Permalink
feat(postgres): Add postges db option to main
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoliz committed Jun 16, 2024
1 parent 66b8ae4 commit b398e77
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
17 changes: 16 additions & 1 deletion YoFi.AspNet/Main/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using YoFi.Core.SampleData;
using YoFi.Services;
using System.Reflection;
using Microsoft.CodeAnalysis.Elfie.Diagnostics;

#if __DEMO_OPEN_ACCESS__
using Microsoft.AspNetCore.Authorization;
Expand All @@ -50,8 +51,22 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var databaseAssemblyName = typeof(ApplicationDbContext).Assembly.GetName().Name!;
var connectionString =
Configuration.GetConnectionString(databaseAssemblyName) ??
Configuration.GetConnectionString("DefaultConnection") ??
throw new KeyNotFoundException("No connection string found. Please check config.");

#if POSTGRES
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(connectionString));
logme.Enqueue("Using Postgres");
#else
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
options.UseSqlServer(connectionString));
logme.Enqueue("Using SQL Server");
#endif

services.AddDatabaseDeveloperPageExceptionFilter();

services.AddIdentity<ApplicationUser, IdentityRole>()
Expand Down
15 changes: 11 additions & 4 deletions YoFi.AspNet/YoFi.AspNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<TargetFramework>net8.0</TargetFramework>
<UserSecretsId>aspnet-OfxWeb.Asp-BDDDDEB0-488C-4CEF-A390-529A80D08E0A</UserSecretsId>
<EnforceCodeStyleInBuild>false</EnforceCodeStyleInBuild>
<DefineConstants>$(MyDefineConstants)</DefineConstants>
<DefineConstants Condition=" '$(Database)' == 'Postgres' ">$(DefineConstants);POSTGRES</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(MyDefineConstants);TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
Expand Down Expand Up @@ -54,10 +54,17 @@

<ItemGroup>
<ProjectReference Include="..\YoFi.Core\YoFi.Core.csproj" />
<ProjectReference Include="..\YoFi.Data\YoFi.Data.csproj" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition=" '$(Database)' == 'Postgres' ">
<ProjectReference Include="..\YoFi.Data.Postgres\YoFi.Data.Postgres.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(Database)' != 'Postgres' ">
<ProjectReference Include="..\YoFi.Data\YoFi.Data.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Seeders\" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion YoFi.AspNet/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-OfxWeb.Asp-BDDDDEB0-488C-4CEF-A390-529A80D08E0A;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-OfxWeb.Asp-BDDDDEB0-488C-4CEF-A390-529A80D08E0A;Trusted_Connection=True;MultipleActiveResultSets=true",
"YoFi.Data.Postgres": "Host=localhost;Port=5501;Database=db;Username=postgres;Password=yofi_development"
},
"Logging": {
"IncludeScopes": false,
Expand Down
29 changes: 29 additions & 0 deletions YoFi.Data.Postgres/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.4'
x-name: pg_yofi_development
services:
db:
image: postgres:15.4-alpine
container_name: postgres_yofi
restart: always
ports:
- '5501:5432'
environment:
- POSTGRES_PASSWORD=yofi_development
- POSTGRES_DB=db
- POSTGRES_USER=postgres
- PGUSER=postgres
# volumes:
# - pg_lwa_pgdata:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL" , "pg_isready" ]
interval: 1s
timeout: 5s
retries: 10
networks:
- pg_yofi_development

#volumes:
# pg_lwa_pgdata:

networks:
pg_yofi_development:

0 comments on commit b398e77

Please sign in to comment.