Datalust.Piggy
2.0.0
dotnet add package Datalust.Piggy --version 2.0.0
NuGet\Install-Package Datalust.Piggy -Version 2.0.0
<PackageReference Include="Datalust.Piggy" Version="2.0.0" />
paket add Datalust.Piggy --version 2.0.0
#r "nuget: Datalust.Piggy, 2.0.0"
// Install Datalust.Piggy as a Cake Addin #addin nuget:?package=Datalust.Piggy&version=2.0.0 // Install Datalust.Piggy as a Cake Tool #tool nuget:?package=Datalust.Piggy&version=2.0.0
A friendly PostgreSQL script runner in the spirit of DbUp. sln
What is Piggy?
Piggy is a simple command-line tool for managing schema and data changes to PostgreSQL databases. Piggy looks for .sql
files in a directory and applies them to the database in order, using transactions and a change log table to ensure each script runs only once per database.
Installation
Piggy is available as a .NET tool package, as standalone binaries, and as a C♯ library that can be used directly in .NET applications.
dotnet tool
Piggy is distributed as a .NET tool package called Datalust.Piggy.Cli, which can be installed using:
dotnet tool install --global Datalust.Piggy.Cli
The executable is called piggy
. Test that the installation was successful with:
piggy --version
Executable binaries
Pre-built, native, self-contained Piggy binaries are available for Windows, macOS and Linux from the releases page. If your platform of choice isn't listed, please raise an issue here so that we can add it.
C# library
For development and test automation purposes, the core script runner is also packaged as a C♯ API and published to NuGet as Datalust.Piggy.
// dotnet add package Datalust.Piggy
var connectionString = // Npgsql connection string
using (var connection = DatabaseConnector.Connect(connectionString))
{
UpdateSession.ApplyChangeScripts(connection, "./db", new Dictionary<string, string>());
}
Workflow
To manage database updates with Piggy, write SQL scripts to make changes, rather than applying changes directly.
Organize change scripts on the file system under a script root directory. Name files so that they sort lexicographically in the order in which they need to be executed:
001-create-schema.sql
002-create-users-table.sql
003-...
If the scripts are arranged in subdirectories, these must be ordered by name as well:
v1/
001-create-schema.sql
002-create-users-table.sql
003-...
v2/
001-rename-users-table.sql
Piggy enumerates .sql
files and generates names like /v1/001-create-schema.sql
using each script's filename relative to the script root. These relative names are checked against the change log table to determine which of them need to be run.
To bring a database up to date, run piggy up
, providing the script root, host, database name, username and password:
piggy up -s <scripts> -h <host> -d <database> -u <username> -p <password>
If the database does not exist, Piggy will create it using sensible defaults. To opt out of this behavior, add the --no-create
flag.
Over time, as your application grows, create new scripts to move the database forwards - don't edit the existing ones, since they've already been applied and will be ignored by Piggy.
Piggy can be used to update from any previous schema version to the current one: scripts that have already been run on a database are ignored, so only necessary scripts are applied.
For more detailed usage information, run piggy help up
; to see all available commands run piggy help
.
Transactions
Piggy wraps each script in a transaction that also covers the change log table update. A few DDL statements can't run within a transaction in PostgreSQL - in these cases, add:
-- PIGGY NO TRANSACTION
as the first line of the script.
Variable substitution
Piggy uses $var$
syntax for replaced variables:
create table $schema$.users (name varchar(140) not null);
insert into $schema$.users (name) values ('$admin$');
Values are inserted using pure text substitution: no escaping or other processing is applied. If no value is supplied for a variable that appears in a script, Piggy will leave the script unchanged (undefined variables will not be replaced with the empty string).
Variables are supplied on the command-line with the -v
flag:
piggy up ... -v schema=myapp -v admin=myuser
Change log
The change log is stored in the target database in piggy.changes
. The piggy log
command is used to view applied change scripts.
Help
Run piggy help
to see all available commands, and piggy help <command>
for detailed command help.
> piggy help
Usage: piggy <command> [<args>]
Available commands are:
baseline Add scripts to the change log without running them
help Show information about available commands
log List change scripts that have been applied to a database
pending Determine which scripts will be run in an update
up Bring a database up to date
--version Print the current executable version
Type `piggy help <command>` for detailed help
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Autofac (>= 8.0.0)
- dapper (>= 2.1.35)
- npgsql (>= 8.0.3)
- Serilog (>= 3.1.1)
- Serilog.Extensions.Logging (>= 8.0.0)
- Serilog.Sinks.Console (>= 5.0.1)
- serilog.sinks.seq (>= 7.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 172 | 6/17/2024 |
2.0.0-dev-00102 | 101 | 5/22/2024 |
2.0.0-dev-00100 | 101 | 5/22/2024 |
2.0.0-dev-00097 | 87 | 5/22/2024 |
2.0.0-dev-00094 | 98 | 5/22/2024 |
2.0.0-dev-00093 | 94 | 5/22/2024 |
2.0.0-dev-00090 | 104 | 5/14/2024 |
2.0.0-dev-00083 | 122 | 4/27/2023 |
2.0.0-dev-00081 | 117 | 4/27/2023 |
2.0.0-dev-00079 | 506 | 4/26/2023 |
2.0.0-dev-00077 | 118 | 4/26/2023 |
1.0.46 | 3,316 | 7/9/2019 |