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

Upgrades Piggy to net7.0, updates all .nupkgs to latest, and removes custom Npgsql to Serilog logging bridge... #26

Merged
14 changes: 7 additions & 7 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ function Publish-Gzips($version)
{
$rids = @("linux-x64", "win-x64", "osx-x64")
foreach ($rid in $rids) {
& dotnet publish src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -f net5.0 -r $rid /p:VersionPrefix=$version /p:PublishSingleFile=true /p:SelfContained=true /p:PublishTrimmed=true
& dotnet publish src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -f net7.0 -r $rid /p:VersionPrefix=$version /p:PublishSingleFile=true /p:SelfContained=true /p:PublishTrimmed=true
if($LASTEXITCODE -ne 0) { exit 4 }

# Make sure the archive contains a reasonable root filename
mv ./src/Datalust.Piggy/bin/Release/net5.0/$rid/publish/ ./src/Datalust.Piggy/bin/Release/net5.0/$rid/piggy-$version-$rid/
mv ./src/Datalust.Piggy/bin/Release/net7.0/$rid/publish/ ./src/Datalust.Piggy/bin/Release/net7.0/$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/net5.0/$rid/piggy-$version-$rid/
& ./build/7-zip/7za.exe a -ttar piggy-$version-$rid.tar ./src/Datalust.Piggy/bin/Release/net7.0/$rid/piggy-$version-$rid/
if($LASTEXITCODE -ne 0) { exit 5 }

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

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

# Back to the original directory name
mv ./src/Datalust.Piggy/bin/Release/net5.0/$rid/piggy-$version-$rid/ ./src/Datalust.Piggy/bin/Release/net5.0/$rid/publish/
mv ./src/Datalust.Piggy/bin/Release/net7.0/$rid/piggy-$version-$rid/ ./src/Datalust.Piggy/bin/Release/net7.0/$rid/publish/
}
}

Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: 2.0.{build}
version: 3.0.{build}
skip_tags: true
image: Visual Studio 2019
image: Visual Studio 2022
build_script:
- ps: ./Build.ps1
test: off
Expand Down
3 changes: 2 additions & 1 deletion piggy.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=cstr/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Datalust/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Skynet/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
28 changes: 28 additions & 0 deletions src/Datalust.Piggy/Database/ConnectionStringParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Datalust.Piggy.Database
{
public static class ConnectionStringParser
{
const StringSplitOptions Options = StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries;

// e.g. Host=localhost;Username=postgres;Password=password-value;Database=database-name
public static Dictionary<string, string> Parse(string? connectionString)
{
var parts = new Dictionary<string, string>();

if (connectionString == null) return parts;

connectionString.Split(';', Options)
.Where(s => s.Contains('='))
.Select(s => s.Split('=', 2, Options))
.Where(a => a.Length == 2)
.ToList()
.ForEach(a => parts.TryAdd(a[0].Trim(), a[1].Trim()));

return parts;
}
}
}
18 changes: 13 additions & 5 deletions src/Datalust.Piggy/Database/DatabaseConnector.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Linq;
using Npgsql;
using Npgsql.Logging;
using Serilog;
using Serilog.Extensions.Logging;

namespace Datalust.Piggy.Database
{
Expand All @@ -12,7 +13,7 @@ public static class DatabaseConnector
{
static DatabaseConnector()
{
NpgsqlLogManager.Provider = new SerilogLoggingProvider();
NpgsqlLoggingConfiguration.InitializeLogging(new SerilogLoggerFactory());
}

/// <summary>
Expand Down Expand Up @@ -50,8 +51,15 @@ public static NpgsqlConnection Connect(string connectionString)
if (connectionString == null) throw new ArgumentNullException(nameof(connectionString));

var conn = new NpgsqlConnection(connectionString);

Log.Information("Connecting to database {Database} on {Host}", conn.Database, conn.Host);

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);

try
{
Expand Down Expand Up @@ -87,4 +95,4 @@ static bool TryCreate(string host, string database, string username, string pass
}
}
}
}
}
30 changes: 0 additions & 30 deletions src/Datalust.Piggy/Database/SerilogLogger.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/Datalust.Piggy/Database/SerilogLoggingProvider.cs

This file was deleted.

27 changes: 18 additions & 9 deletions src/Datalust.Piggy/Datalust.Piggy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>A friendly PostgreSQL script runner in the spirit of DbUp.</Description>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<AssemblyName>piggy</AssemblyName>
<ApplicationIcon>..\..\asset\Piggy-Icon-128px.ico</ApplicationIcon>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
Expand All @@ -16,6 +16,8 @@
<PackageIcon>Piggy-Icon-128px.png</PackageIcon>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<TrimMode>partial</TrimMode>
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
</PropertyGroup>

<ItemGroup>
Expand All @@ -29,13 +31,20 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="dapper" Version="2.0.90" />
<PackageReference Include="npgsql" Version="5.0.7" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Autofac" Version="6.2.0" />
<PackageReference Include="serilog.sinks.seq" Version="5.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="dapper" Version="2.0.123" />
<PackageReference Include="npgsql" Version="7.0.2" />
<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>

</Project>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>Datalust.Piggy.Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
21 changes: 15 additions & 6 deletions src/Datalust.Piggy/PiggyModule.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
using System.Reflection;
using System;
using Autofac;
using Datalust.Piggy.Cli;
using Datalust.Piggy.Cli.Commands;

namespace Datalust.Piggy
{
class PiggyModule : Autofac.Module
class PiggyModule : Module
{
public static readonly Type[] RegisteredCommands =
{
typeof(BaselineCommand),
typeof(HelpCommand),
typeof(LogCommand),
typeof(PendingCommand),
typeof(UpdateCommand),
typeof(VersionCommand)
};

protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CommandLineHost>();
builder.RegisterAssemblyTypes(typeof(Program).GetTypeInfo().Assembly)
.As<Command>()
.WithMetadataFrom<CommandAttribute>();
builder.RegisterTypes(RegisteredCommands).As<Command>().WithMetadataFrom<CommandAttribute>();
}
}
}
}
75 changes: 75 additions & 0 deletions test/Datalust.Piggy.Tests/Database/ConnectionStringParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Datalust.Piggy.Database;
using Xunit;

namespace Datalust.Piggy.Tests.Database
{
public class ConnectionStringParserTests
{
[Fact]
public void ParsesGoodConnectionStringCorrectly1()
{
const string connectionString = "Host=localhost";
var parts = ConnectionStringParser.Parse(connectionString);
Assert.Equal("localhost", parts["Host"]);
}

[Fact]
public void ParsesGoodConnectionStringCorrectly2()
{
const string connectionString = "Host=localhost;Username=hunter2;Password=KenSentMe;Database=Skynet";
var parts = ConnectionStringParser.Parse(connectionString);
Assert.Equal("localhost", parts["Host"]);
Assert.Equal("hunter2", parts["Username"]);
Assert.Equal("KenSentMe", parts["Password"]);
Assert.Equal("Skynet", parts["Database"]);
}

[Fact]
public void ParserHandlesDuplicateConnectionStringKeysCorrectly()
{
const string connectionString = "Host=localhost;Host=127.0.0.1";
var parts = ConnectionStringParser.Parse(connectionString);
Assert.Equal("localhost", parts["Host"]);
}

[Fact]
public void ParserHandlesDuplicateButDifferentCaseConnectionStringKeysCorrectly()
{
const string connectionString = "Host=localhost;host=127.0.0.1";
var parts = ConnectionStringParser.Parse(connectionString);
Assert.Equal("localhost", parts["Host"]);
Assert.Equal("127.0.0.1", parts["host"]);
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData(";")]
[InlineData("=;")]
[InlineData("==;")]
[InlineData("=;;")]
[InlineData("=;;=")]
[InlineData("=;=;=")]
[InlineData("Host")]
[InlineData("Host=")]
[InlineData(";Host=")]
[InlineData("Host=;")]
[InlineData("localhost")]
[InlineData("=localhost")]
[InlineData(";=localhost")]
[InlineData("=localhost;")]
[InlineData("Host=;;")]
[InlineData("Host=;=;")]
[InlineData("Host=;Username=;")]
[InlineData("Host=;=hunter2;")]
[InlineData("=localhost;Username=;")]
[InlineData("=localhost;=hunter2;")]
[InlineData(";Username=;=KenSentMe;DatabaseSkynet")]
public void DoesNotParseBadConnectionStrings(string? connectionString)
{
var parts = ConnectionStringParser.Parse(connectionString);
Assert.Empty(parts);
}
}
}
8 changes: 4 additions & 4 deletions test/Datalust.Piggy.Tests/Datalust.Piggy.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<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">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
20 changes: 20 additions & 0 deletions test/Datalust.Piggy.Tests/PiggyModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Linq;
using Datalust.Piggy.Cli;
using Xunit;

namespace Datalust.Piggy.Tests;

public class PiggyModuleTests
{
[Fact]
public void AllCommandsScannedAndFoundAreExplicitlyRegisteredInPiggyModule()
{
var expectedCommands = typeof(PiggyModule).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Command)));
var actualCommands = PiggyModule.RegisteredCommands;

foreach (var expected in expectedCommands)
{
Assert.Contains(expected, actualCommands);
}
}
}