Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postgres support #235

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# LinkDotNet.Blog
# LinkDotNet.Blog
[![.NET](https://github.com/linkdotnet/Blog/actions/workflows/dotnet.yml/badge.svg?branch=master)](https://github.com/linkdotnet/Blog/actions/workflows/dotnet.yml)
[![CodeQL](https://github.com/linkdotnet/Blog/actions/workflows/codeql.yml/badge.svg)](https://github.com/linkdotnet/Blog/actions/workflows/codeql.yml)

Expand Down Expand Up @@ -97,12 +97,13 @@ The appsettings.json file has a lot of options to customize the content of the b
| PatreonName | string | Enables the "Become a patreon" button that redirects to patreon.com. Only pass the user name (public profile) as user name. |

## Storage Provider
Currently, there are 5 Storage-Provider:
Currently, there are 6 Storage-Provider:
* InMemory - Basically a list holding your data (per request). If the User hits a hard reload, the data is gone.
* RavenDb - As the name suggests for RavenDb. RavenDb automatically creates all the documents, if a database name is provided.
* Sqlite - Based on EF Core, it can be easily adapted for other Sql Dialects. The tables are automatically created.
* SqlServer - Based on EF Core, it can be easily adapted for other Sql Dialects. The tables are automatically created.
* MySql - Based on EF Core - also supports MariaDB.
* Postgresql - Based on EF Core.

The default (when you clone the repository) is the `InMemory` option. That means every time you restart the service, all posts and related objects are gone.

Expand Down Expand Up @@ -243,3 +244,10 @@ For MySql use the following:
"PersistenceProvider": "MySql"
"ConnectionString": "Server=YOURSERVER;User ID=YOURUSERID;Password=YOURPASSWORD;Database=YOURDATABASE"
```

For Postgres use the following:

```
"PersistenceProvider": "Postgres"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has to be Postgresql

"ConnectionString": "Host=YOURSERVER;Database=YOURDATABASE;Username=YOURUSERNAME;Password=YOURPASSWORD"
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.5.23280.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
<PackageReference Include="RavenDB.Client" Version="5.4.105" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed class PersistenceProvider : Enumeration<PersistenceProvider>
public static readonly PersistenceProvider RavenDb = new(nameof(RavenDb));
public static readonly PersistenceProvider InMemory = new(nameof(InMemory));
public static readonly PersistenceProvider MySql = new(nameof(MySql));
public static readonly PersistenceProvider Postgresql = new(nameof(Postgresql));

private PersistenceProvider(string key)
: base(key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ public static void UseMySqlAsStorageProvider(this IServiceCollection services)
});
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
}

public static void UsePostgresqlAsStorageProvider(this IServiceCollection services)
{
services.AssertNotAlreadyRegistered(typeof(IRepository<>));

services.AddPooledDbContextFactory<BlogDbContext>(
(s, builder) =>
{
var configuration = s.GetRequiredService<AppConfiguration>();
var connectionString = configuration.ConnectionString;
builder.UseNpgsql(connectionString)
#if DEBUG
.EnableDetailedErrors()
#endif
;
});
linkdotnet marked this conversation as resolved.
Show resolved Hide resolved

services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static void AddStorageProvider(this IServiceCollection services, IConfigu
services.UseMySqlAsStorageProvider();
services.RegisterCachedRepository<Infrastructure.Persistence.Sql.Repository<BlogPost>>();
}
else if (persistenceProvider == PersistenceProvider.Postgresql)
{
services.UsePostgresqlAsStorageProvider();
services.RegisterCachedRepository<Infrastructure.Persistence.Sql.Repository<BlogPost>>();
}
}

private static void RegisterCachedRepository<TRepo>(this IServiceCollection services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class StorageProviderRegistrationExtensionsTests
new object[] { new Action<IServiceCollection>(services => services.UseInMemoryAsStorageProvider()) },
new object[] { new Action<IServiceCollection>(services => services.UseRavenDbAsStorageProvider()) },
new object[] { new Action<IServiceCollection>(services => services.UseMySqlAsStorageProvider()) },
new object[] { new Action<IServiceCollection>(services => services.UsePostgresqlAsStorageProvider()) },
};

[Theory]
Expand Down