-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from nblumhardt/net-8.0
Update to .NET 8 and new Npgsql version
- Loading branch information
Showing
6 changed files
with
103 additions
and
113 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"sdk": { | ||
"allowPrerelease": false, | ||
"version": "7.0.203", | ||
"version": "8.0.204", | ||
"rollForward": "latestFeature" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,98 +1,96 @@ | ||
using System; | ||
using System.Linq; | ||
using Npgsql; | ||
using Serilog; | ||
using Serilog.Extensions.Logging; | ||
|
||
namespace Datalust.Piggy.Database | ||
namespace Datalust.Piggy.Database; | ||
|
||
/// <summary> | ||
/// Assists with making a PostgreSQL database connection. | ||
/// </summary> | ||
public static class DatabaseConnector | ||
{ | ||
static DatabaseConnector() | ||
{ | ||
NpgsqlLoggingConfiguration.InitializeLogging(new SerilogLoggerFactory()); | ||
} | ||
|
||
/// <summary> | ||
/// Assists with making a PostgreSQL database connection. | ||
/// Connect to the specified database. | ||
/// </summary> | ||
public static class DatabaseConnector | ||
/// <param name="host">The PostgreSQL host to connect to.</param> | ||
/// <param name="database">The database to update.</param> | ||
/// <param name="username">The username to authenticate with.</param> | ||
/// <param name="password">The password to authenticate with.</param> | ||
/// <param name="createIfMissing">If <c>true</c>, Piggy will attempt to create the database if it doesn't exist. The | ||
/// database must already exist, otherwise.</param> | ||
/// <returns>An open database connection.</returns> | ||
public static NpgsqlConnection Connect(string host, string database, string username, string password, bool createIfMissing) | ||
{ | ||
static DatabaseConnector() | ||
try | ||
{ | ||
NpgsqlLoggingConfiguration.InitializeLogging(new SerilogLoggerFactory()); | ||
return Connect($"Host={host};Username={username};Password={password};Database={database}"); | ||
} | ||
|
||
/// <summary> | ||
/// Connect to the specified database. | ||
/// </summary> | ||
/// <param name="host">The PostgreSQL host to connect to.</param> | ||
/// <param name="database">The database to update.</param> | ||
/// <param name="username">The username to authenticate with.</param> | ||
/// <param name="password">The password to authenticate with.</param> | ||
/// <param name="createIfMissing">If <c>true</c>, Piggy will attempt to create the database if it doesn't exist. The | ||
/// database must already exist, otherwise.</param> | ||
/// <returns>An open database connection.</returns> | ||
public static NpgsqlConnection Connect(string host, string database, string username, string password, bool createIfMissing) | ||
catch (PostgresException px) when (px.SqlState == "3D000") | ||
{ | ||
try | ||
{ | ||
return Connect($"Host={host};Username={username};Password={password};Database={database}"); | ||
} | ||
catch (PostgresException px) when (px.SqlState == "3D000") | ||
{ | ||
if (createIfMissing && TryCreate(host, database, username, password)) | ||
return Connect(host, database, username, password, false); | ||
if (createIfMissing && TryCreate(host, database, username, password)) | ||
return Connect(host, database, username, password, false); | ||
|
||
throw; | ||
} | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Connect to the specified database. | ||
/// </summary> | ||
/// <param name="connectionString">A connection string identifying the database.</param> | ||
/// <returns>An open database connection.</returns> | ||
public static NpgsqlConnection Connect(string connectionString) | ||
{ | ||
if (connectionString == null) throw new ArgumentNullException(nameof(connectionString)); | ||
/// <summary> | ||
/// Connect to the specified database. | ||
/// </summary> | ||
/// <param name="connectionString">A connection string identifying the database.</param> | ||
/// <returns>An open database connection.</returns> | ||
public static NpgsqlConnection Connect(string connectionString) | ||
{ | ||
if (connectionString == null) throw new ArgumentNullException(nameof(connectionString)); | ||
|
||
var conn = new NpgsqlConnection(connectionString); | ||
var conn = new NpgsqlConnection(connectionString); | ||
|
||
var host = conn.Host; | ||
if (conn.Host == null && !string.IsNullOrWhiteSpace(conn.ConnectionString)) | ||
{ | ||
var parts = ConnectionStringParser.Parse(conn.ConnectionString); | ||
if (parts.TryGetValue("Host", out var value)) host = value; | ||
} | ||
var host = conn.Host; | ||
if (conn.Host == null && !string.IsNullOrWhiteSpace(conn.ConnectionString)) | ||
{ | ||
var parts = ConnectionStringParser.Parse(conn.ConnectionString); | ||
if (parts.TryGetValue("Host", out var value)) host = value; | ||
} | ||
|
||
Log.Information("Connecting to database {Database} on {Host}", conn.Database, host); | ||
Log.Information("Connecting to database {Database} on {Host}", conn.Database, host); | ||
|
||
try | ||
{ | ||
conn.Open(); | ||
} | ||
catch | ||
{ | ||
conn.Dispose(); | ||
throw; | ||
} | ||
try | ||
{ | ||
conn.Open(); | ||
} | ||
catch | ||
{ | ||
conn.Dispose(); | ||
throw; | ||
} | ||
|
||
Log.Information("Connected"); | ||
Log.Information("Connected"); | ||
|
||
return conn; | ||
} | ||
return conn; | ||
} | ||
|
||
static bool TryCreate(string host, string database, string username, string password) | ||
static bool TryCreate(string host, string database, string username, string password) | ||
{ | ||
Log.Information("Database does not exist; attempting to create it"); | ||
|
||
var postgresCstr = $"Host={host};Username={username};Password={password};Database=postgres"; | ||
using (var postgresConn = new NpgsqlConnection(postgresCstr)) | ||
{ | ||
Log.Information("Database does not exist; attempting to create it"); | ||
postgresConn.Open(); | ||
|
||
var postgresCstr = $"Host={host};Username={username};Password={password};Database=postgres"; | ||
using (var postgresConn = new NpgsqlConnection(postgresCstr)) | ||
Log.Information("Creating database {Database} on {Host} with owner {Owner} and default options", database, host, username); | ||
using (var createCommand = new NpgsqlCommand($"CREATE DATABASE {database} WITH OWNER = {username} ENCODING = 'UTF8' CONNECTION LIMIT = -1;", postgresConn)) | ||
{ | ||
postgresConn.Open(); | ||
|
||
Log.Information("Creating database {Database} on {Host} with owner {Owner} and default options", database, host, username); | ||
using (var createCommand = new NpgsqlCommand($"CREATE DATABASE {database} WITH OWNER = {username} ENCODING = 'UTF8' CONNECTION LIMIT = -1;", postgresConn)) | ||
{ | ||
createCommand.ExecuteNonQuery(); | ||
Log.Information("Database created successfully"); | ||
return true; | ||
} | ||
createCommand.ExecuteNonQuery(); | ||
Log.Information("Database created successfully"); | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<ProjectReference Include="..\..\src\Datalust.Piggy\Datalust.Piggy.csproj" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageReference Include="xunit" Version="2.8.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Datalust.Piggy\Datalust.Piggy.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |