Skip to content

Commit

Permalink
Merge pull request #34 from nblumhardt/net-8.0
Browse files Browse the repository at this point in the history
Update to .NET 8 and new Npgsql version
  • Loading branch information
nblumhardt authored May 14, 2024
2 parents 6c04a1d + 0430fd9 commit 90f474e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 113 deletions.
30 changes: 16 additions & 14 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
$ErrorActionPreference = 'Stop'

$tfm = "net8.0"

Write-Output "build: Build started"
Push-Location $PSScriptRoot

Expand Down Expand Up @@ -76,44 +78,44 @@ Write-Output "build: Version is $version"
Write-Output "build: Creating artifacts directory at .\$artifacts"
New-Item -ItemType "directory" -Name $artifacts

Write-Output "build: Packing library into .nupkg"
if ($suffix) {
& dotnet pack $project -c Release -o $PSScriptRoot/$artifacts /p:OutputType=Library --version-suffix=$suffix
} else {
& dotnet pack $project -c Release -o $PSScriptRoot/$artifacts /p:OutputType=Library
}
if($LASTEXITCODE -ne 0) { exit 11 }

$rids = @("win-x64", "linux-x64", "osx-x64", "osx-arm64")
foreach ($rid in $rids) {
Write-Output "build: Building a $rid build of version $version"

if ($suffix) {
& dotnet publish $project -c Release -f net7.0 -r $rid /p:PublishSingleFile=true /p:SelfContained=true /p:PublishTrimmed=true --version-suffix=$suffix
& dotnet publish $project -c Release -f $tfm -r $rid /p:PublishSingleFile=true /p:SelfContained=true /p:PublishTrimmed=true --version-suffix=$suffix
} else {
& dotnet publish $project -c Release -f net7.0 -r $rid /p:PublishSingleFile=true /p:SelfContained=true /p:PublishTrimmed=true
& dotnet publish $project -c Release -f $tfm -r $rid /p:PublishSingleFile=true /p:SelfContained=true /p:PublishTrimmed=true
}
if($LASTEXITCODE -ne 0) { exit 7 }

# Make sure the archive contains a reasonable root filename.
Move-Item ./src/Datalust.Piggy/bin/Release/net7.0/$rid/publish/ ./src/Datalust.Piggy/bin/Release/net7.0/$rid/piggy-$version-$rid/
Move-Item ./src/Datalust.Piggy/bin/Release/$tfm/$rid/publish/ ./src/Datalust.Piggy/bin/Release/$tfm/$rid/piggy-$version-$rid/

if ($rid -ne "win-x64") {
& ./build/7-zip/7za.exe a -ttar piggy-$version-$rid.tar ./src/Datalust.Piggy/bin/Release/net7.0/$rid/piggy-$version-$rid/
& ./build/7-zip/7za.exe a -ttar piggy-$version-$rid.tar ./src/Datalust.Piggy/bin/Release/$tfm/$rid/piggy-$version-$rid/
if($LASTEXITCODE -ne 0) { exit 8 }

& ./build/7-zip/7za.exe a -tgzip ./$artifacts/piggy-$version-$rid.tar.gz piggy-$version-$rid.tar
if($LASTEXITCODE -ne 0) { exit 9 }

Remove-Item piggy-$version-$rid.tar
} else {
& ./build/7-zip/7za.exe a -tzip ./$artifacts/piggy-$version-$rid.zip ./src/Datalust.Piggy/bin/Release/net7.0/$rid/piggy-$version-$rid/
& ./build/7-zip/7za.exe a -tzip ./$artifacts/piggy-$version-$rid.zip ./src/Datalust.Piggy/bin/Release/$tfm/$rid/piggy-$version-$rid/
if($LASTEXITCODE -ne 0) { exit 10 }
}

# Move back to the original directory name.
Move-Item ./src/Datalust.Piggy/bin/Release/net7.0/$rid/piggy-$version-$rid/ ./src/Datalust.Piggy/bin/Release/net7.0/$rid/publish/
Move-Item ./src/Datalust.Piggy/bin/Release/$tfm/$rid/piggy-$version-$rid/ ./src/Datalust.Piggy/bin/Release/$tfm/$rid/publish/
}

Write-Output "build: Packing library into .nupkg"
if ($suffix) {
& dotnet pack $project -c Release -o $PSScriptRoot/$artifacts /p:OutputType=Library --version-suffix=$suffix
} else {
& dotnet pack $project -c Release -o $PSScriptRoot/$artifacts /p:OutputType=Library
}
if($LASTEXITCODE -ne 0) { exit 11 }

Pop-Location
Write-Output "build: completed successfully"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![Piggy](https://raw.githubusercontent.com/datalust/piggy/master/asset/Piggy-400px.png)

A friendly PostgreSQL script runner in the spirit of [DbUp](https://github.com/DbUp/DbUp).

[sln](sln)
[![Build status](https://ci.appveyor.com/api/projects/status/889gkdpvjbjuhkfg?svg=true)](https://ci.appveyor.com/project/datalust/piggy)

### What is Piggy?
Expand Down Expand Up @@ -108,7 +108,7 @@ For development and test automation purposes, the core script runner is also pac
```csharp
// dotnet add package Datalust.Piggy
var connectionString = // Npgsql connection string
using (var connection = DatabaseConnector.Connect(connectionString, createIfMissing: true)
using (var connection = DatabaseConnector.Connect(connectionString))
{
UpdateSession.ApplyChangeScripts(connection, "./db", new Dictionary<string, string>());
}
Expand Down
2 changes: 1 addition & 1 deletion global.json
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"
}
}
138 changes: 68 additions & 70 deletions src/Datalust.Piggy/Database/DatabaseConnector.cs
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;
}
}
}
}
}
29 changes: 11 additions & 18 deletions src/Datalust.Piggy/Datalust.Piggy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>A friendly PostgreSQL script runner in the spirit of DbUp.</Description>
<OutputType>exe</OutputType>
<VersionPrefix>2.0.0</VersionPrefix>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>piggy</AssemblyName>
<ApplicationIcon>..\..\asset\Piggy-Icon-128px.ico</ApplicationIcon>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
Expand All @@ -17,36 +17,29 @@
<PackageIcon>Piggy-Icon-128px.png</PackageIcon>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<TrimMode>partial</TrimMode>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
<LangVersion>latest</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\asset\Piggy-Icon-128px.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup>
<Content Include="Attribution\*.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<InternalsVisibleTo Include="Datalust.Piggy.Tests" />
<None Include="..\..\README.md" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="dapper" Version="2.0.123" />
<PackageReference Include="dapper" Version="2.1.35" />
<PackageReference Include="npgsql" Version="8.0.3" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Autofac" Version="7.0.1" />
<PackageReference Include="serilog.sinks.seq" Version="5.2.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Datalust.Piggy.Tests</_Parameter1>
</AssemblyAttribute>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Autofac" Version="8.0.0" />
<PackageReference Include="serilog.sinks.seq" Version="7.0.1" />
</ItemGroup>

</Project>
13 changes: 5 additions & 8 deletions test/Datalust.Piggy.Tests/Datalust.Piggy.Tests.csproj
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>

0 comments on commit 90f474e

Please sign in to comment.