Skip to content

Commit e7e1c74

Browse files
authored
Merge pull request #22 from datalust/dev
Release
2 parents e4b6f04 + 3bee99c commit e7e1c74

File tree

10 files changed

+569
-214
lines changed

10 files changed

+569
-214
lines changed

Build.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function Publish-Gzips($version)
4040
{
4141
$rids = @("ubuntu.14.04-x64", "ubuntu.16.04-x64", "rhel.7-x64", "osx.10.12-x64")
4242
foreach ($rid in $rids) {
43-
& dotnet publish src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -f netcoreapp2.0 -r $rid /p:VersionPrefix=$version /p:ShowLinkerSizeComparison=true
43+
& dotnet publish src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -f netcoreapp2.0 -r $rid /p:VersionPrefix=$version
4444
if($LASTEXITCODE -ne 0) { exit 4 }
4545

4646
# Make sure the archive contains a reasonable root filename
@@ -61,7 +61,7 @@ function Publish-Gzips($version)
6161

6262
function Publish-Msi($version)
6363
{
64-
& dotnet publish src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -f netcoreapp2.0 -r win10-x64 /p:VersionPrefix=$version /p:ShowLinkerSizeComparison=true
64+
& dotnet publish src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -f netcoreapp2.0 -r win10-x64 /p:VersionPrefix=$version
6565
if($LASTEXITCODE -ne 0) { exit 7 }
6666

6767
& msbuild ./setup/Datalust.Piggy.Setup/Datalust.Piggy.Setup.wixproj /t:Build /p:Configuration=Release /p:Platform=x64 /p:Version=$version /p:BuildProjectReferences=false
@@ -70,6 +70,12 @@ function Publish-Msi($version)
7070
mv ./setup/Datalust.Piggy.Setup/bin/Release/piggy.msi ./artifacts/piggy-$version.msi
7171
}
7272

73+
function Publish-Nupkgs($version)
74+
{
75+
& dotnet pack src/Datalust.Piggy/Datalust.Piggy.csproj -c Release -o $PSScriptRoot/artifacts /p:VersionPrefix=$version /p:OutputType=Library
76+
if($LASTEXITCODE -ne 0) { exit 9 }
77+
}
78+
7379
Push-Location $PSScriptRoot
7480

7581
$version = @{ $true = $env:APPVEYOR_BUILD_VERSION; $false = "99.99.99" }[$env:APPVEYOR_BUILD_VERSION -ne $NULL];
@@ -83,5 +89,6 @@ Execute-Tests
8389
Create-ArtifactDir
8490
Publish-Gzips($version)
8591
Publish-Msi($version)
92+
Publish-Nupkgs($version)
8693

8794
Pop-Location

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ Available commands are:
101101
Type `piggy help <command>` for detailed help
102102
```
103103

104+
### C&sharp; API
105+
106+
For development and test automation purposes, the core script runner is also packaged as a C&sharp; API and published to NuGet as _Datalust.Piggy_.
107+
108+
```csharp
109+
// dotnet add package Datalust.Piggy
110+
var connectionString = // Npgsql connection string
111+
using (var connection = DatabaseConnector.Connect(connectionString, createIfMissing: true)
112+
{
113+
UpdateSession.ApplyChangeScripts(connection, "./db", new Dictionary<string, string>());
114+
}
115+
```

appveyor.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
version: 1.0.{build}
22
skip_tags: true
3-
image: Visual Studio 2017
3+
image: Visual Studio 2019
44
build_script:
55
- ps: ./Build.ps1
66
test: off
77
artifacts:
88
- path: artifacts/piggy-*.msi
99
- path: artifacts/piggy-*.tar.gz
10+
- path: artifacts/Datalust.Piggy.*.nupkg
1011
deploy:
12+
- provider: NuGet
13+
api_key:
14+
secure: ErGkGoLMaZtkFgFcBIFjNn3twXKxEhO27F7vQGlx0m7ouCV3XdgGdSNERZOmdLLw
15+
skip_symbols: true
16+
on:
17+
branch: master
1118
- provider: GitHub
1219
auth_token:
1320
secure: Bo3ypKpKFxinjR9ShkNekNvkob2iklHJU+UlYyfHtcFFIAa58SV2TkEd0xWxz633

piggy.sln

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.12
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29020.237
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FC0A256C-CC1F-4ECE-AF09-707248D10DC1}"
77
EndProject
@@ -11,7 +11,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{2EA56595-519
1111
appveyor.yml = appveyor.yml
1212
Build.ps1 = Build.ps1
1313
LICENSE = LICENSE
14-
nuget.config = nuget.config
1514
README.md = README.md
1615
EndProjectSection
1716
EndProject

piggy.sln.DotSettings

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<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">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=cstr/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Datalust/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Postgre/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

setup/Datalust.Piggy.Setup/Piggy.wxs

Lines changed: 447 additions & 163 deletions
Large diffs are not rendered by default.

src/Datalust.Piggy/Cli/Commands/UpdateCommand.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Datalust.Piggy.Cli.Features;
3+
using Datalust.Piggy.Database;
34
using Datalust.Piggy.Update;
45
using Npgsql;
56
using Serilog;
@@ -35,9 +36,10 @@ protected override int Run()
3536

3637
try
3738
{
38-
UpdateSession.ApplyChangeScripts(
39-
_databaseFeature.Host, _databaseFeature.Database, _usernamePasswordFeature.Username, _usernamePasswordFeature.Password,
40-
_createIfMissing, _scriptRootFeature.ScriptRoot, _defineVariablesFeature.Variables);
39+
using (var connection = DatabaseConnector.Connect(_databaseFeature.Host, _databaseFeature.Database, _usernamePasswordFeature.Username, _usernamePasswordFeature.Password, _createIfMissing))
40+
{
41+
UpdateSession.ApplyChangeScripts(connection, _scriptRootFeature.ScriptRoot, _defineVariablesFeature.Variables);
42+
}
4143

4244
return 0;
4345
}

src/Datalust.Piggy/Database/DatabaseConnector.cs

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,73 @@
1+
using System;
12
using Npgsql;
23
using Npgsql.Logging;
34
using Serilog;
45

56
namespace Datalust.Piggy.Database
67
{
7-
static class DatabaseConnector
8+
/// <summary>
9+
/// Assists with making a PostgreSQL database connection.
10+
/// </summary>
11+
public static class DatabaseConnector
812
{
913
static DatabaseConnector()
1014
{
1115
NpgsqlLogManager.Provider = new SerilogLoggingProvider();
1216
}
1317

18+
/// <summary>
19+
/// Connect to the specified database.
20+
/// </summary>
21+
/// <param name="host">The PostgreSQL host to connect to.</param>
22+
/// <param name="database">The database to update.</param>
23+
/// <param name="username">The username to authenticate with.</param>
24+
/// <param name="password">The password to authenticate with.</param>
25+
/// <param name="createIfMissing">If <c>true</c>, Piggy will attempt to create the database if it doesn't exist. The
26+
/// database must already exist, otherwise.</param>
27+
/// <returns>An open database connection.</returns>
1428
public static NpgsqlConnection Connect(string host, string database, string username, string password, bool createIfMissing)
1529
{
16-
Log.Information("Connecting to database {Database} on {Host}", database, host);
17-
18-
var cstr = $"Host={host};Username={username};Password={password};Database={database}";
19-
NpgsqlConnection conn = null;
2030
try
2131
{
22-
conn = new NpgsqlConnection(cstr);
23-
conn.Open();
24-
25-
Log.Information("Connected");
26-
27-
return conn;
32+
return Connect($"Host={host};Username={username};Password={password};Database={database}");
2833
}
2934
catch (PostgresException px) when (px.SqlState == "3D000")
3035
{
31-
conn?.Dispose();
32-
3336
if (createIfMissing && TryCreate(host, database, username, password))
3437
return Connect(host, database, username, password, false);
3538

3639
throw;
3740
}
3841
}
3942

43+
/// <summary>
44+
/// Connect to the specified database.
45+
/// </summary>
46+
/// <param name="connectionString">A connection string identifying the database.</param>
47+
/// <returns>An open database connection.</returns>
48+
public static NpgsqlConnection Connect(string connectionString)
49+
{
50+
if (connectionString == null) throw new ArgumentNullException(nameof(connectionString));
51+
52+
var conn = new NpgsqlConnection(connectionString);
53+
54+
Log.Information("Connecting to database {Database} on {Host}", conn.Database, conn.Host);
55+
56+
try
57+
{
58+
conn.Open();
59+
}
60+
catch
61+
{
62+
conn.Dispose();
63+
throw;
64+
}
65+
66+
Log.Information("Connected");
67+
68+
return conn;
69+
}
70+
4071
static bool TryCreate(string host, string database, string username, string password)
4172
{
4273
Log.Information("Database does not exist; attempting to create it");

src/Datalust.Piggy/Datalust.Piggy.csproj

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<Description>A friendly PostgreSQL script runner in the spirit of DbUp.</Description>
45
<OutputType>Exe</OutputType>
56
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
67
<AssemblyName>piggy</AssemblyName>
78
<ApplicationIcon>..\..\asset\Piggy-Icon-128px.ico</ApplicationIcon>
89
<RuntimeIdentifiers>win10-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;rhel.7-x64;osx.10.12-x64</RuntimeIdentifiers>
910
<GenerateAssemblyInformationalVersionAttribute>True</GenerateAssemblyInformationalVersionAttribute>
11+
<Authors>Datalust and Contributors</Authors>
12+
<PackageId>Datalust.Piggy</PackageId>
13+
<PackageTags>postgresql</PackageTags>
14+
<PackageIconUrl>https://raw.githubusercontent.com/datalust/piggy/dev/asset/Piggy-Icon-128px.png</PackageIconUrl>
15+
<PackageProjectUrl>https://github.com/datalust/piggy</PackageProjectUrl>
16+
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1017
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
11-
<TreatSpecificWarningsAsErrors />
1218
</PropertyGroup>
1319

1420
<ItemGroup>
@@ -28,15 +34,14 @@
2834
</ItemGroup>
2935

3036
<ItemGroup>
31-
<PackageReference Include="dapper" Version="1.50.2" />
32-
<PackageReference Include="npgsql" Version="3.2.4.1" />
33-
<PackageReference Include="Serilog" Version="2.5.0" />
34-
<PackageReference Include="Serilog.Sinks.Console" Version="3.0.1" />
35-
<PackageReference Include="Autofac" Version="4.0.0" />
36-
<PackageReference Include="serilog.sinks.seq" Version="3.3.2" />
37+
<PackageReference Include="dapper" Version="1.60.6" />
38+
<PackageReference Include="npgsql" Version="4.0.7" />
39+
<PackageReference Include="Serilog" Version="2.8.0" />
40+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
41+
<PackageReference Include="Autofac" Version="4.9.2" />
42+
<PackageReference Include="serilog.sinks.seq" Version="4.0.0" />
3743
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
3844
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
39-
<PackageReference Include="ILLink.Tasks" Version="0.1.4-preview-981901" />
4045
</ItemGroup>
4146

4247
</Project>

src/Datalust.Piggy/Update/UpdateSession.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.IO;
33
using System.Text;
4-
using Datalust.Piggy.Database;
54
using Datalust.Piggy.Filesystem;
65
using Datalust.Piggy.History;
76
using Datalust.Piggy.Status;
@@ -11,32 +10,37 @@
1110

1211
namespace Datalust.Piggy.Update
1312
{
14-
static class UpdateSession
13+
/// <summary>
14+
/// Applies updates to a target database.
15+
/// </summary>
16+
public static class UpdateSession
1517
{
16-
public static void ApplyChangeScripts(string host, string database, string username, string password,
17-
bool createIfMissing, string scriptRoot, IReadOnlyDictionary<string, string> variables)
18+
/// <summary>
19+
/// Apply change scripts from a folder hierarchy.
20+
/// </summary>
21+
/// <param name="connection">The database connection to use.</param>
22+
/// <param name="scriptRoot">A root filesystem folder under which change scripts are stored.</param>
23+
/// <param name="variables">A set of variables to replace within change scripts.</param>
24+
public static void ApplyChangeScripts(NpgsqlConnection connection, string scriptRoot, IReadOnlyDictionary<string, string> variables)
1825
{
19-
using (var connection = DatabaseConnector.Connect(host, database, username, password, createIfMissing))
20-
{
21-
var scripts = DatabaseStatus.GetPendingScripts(connection, scriptRoot);
22-
23-
Log.Information("Found {Count} new script files to apply", scripts.Length);
26+
var scripts = DatabaseStatus.GetPendingScripts(connection, scriptRoot);
2427

25-
if (scripts.Length != 0)
26-
{
27-
Log.Information("Ensuring the change log table exists");
28-
using (var command = new NpgsqlCommand(AppliedChangeScriptLog.ChangesTableCreateScript, connection))
29-
command.ExecuteNonQuery();
30-
}
28+
Log.Information("Found {Count} new script files to apply", scripts.Length);
3129

32-
foreach (var script in scripts)
33-
{
34-
Log.Information("Applying {FullPath} as {ScriptFile}", script.FullPath, script.RelativeName);
35-
ApplyChangeScript(connection, script, variables);
36-
}
30+
if (scripts.Length != 0)
31+
{
32+
Log.Information("Ensuring the change log table exists");
33+
using (var command = new NpgsqlCommand(AppliedChangeScriptLog.ChangesTableCreateScript, connection))
34+
command.ExecuteNonQuery();
35+
}
3736

38-
Log.Information("Done");
37+
foreach (var script in scripts)
38+
{
39+
Log.Information("Applying {FullPath} as {ScriptFile}", script.FullPath, script.RelativeName);
40+
ApplyChangeScript(connection, script, variables);
3941
}
42+
43+
Log.Information("Done");
4044
}
4145

4246
static void ApplyChangeScript(NpgsqlConnection connection, ChangeScriptFile script, IReadOnlyDictionary<string, string> variables)

0 commit comments

Comments
 (0)