TcKs.PgDbMigration 6.0.1

dotnet add package TcKs.PgDbMigration --version 6.0.1                
NuGet\Install-Package TcKs.PgDbMigration -Version 6.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TcKs.PgDbMigration" Version="6.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TcKs.PgDbMigration --version 6.0.1                
#r "nuget: TcKs.PgDbMigration, 6.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TcKs.PgDbMigration as a Cake Addin
#addin nuget:?package=TcKs.PgDbMigration&version=6.0.1

// Install TcKs.PgDbMigration as a Cake Tool
#tool nuget:?package=TcKs.PgDbMigration&version=6.0.1                

PgDbMigration

The opinionated .NET library for PostgreSql database migrations.

If you want easy-to-use db migration tool, you are on the right spot!

Build status

Main branch pipeline status

Installation from NuGet

dotnet add package TcKs.PgDbMigration

Usage & philosphy

The database migrations are batches of sql statements, which should be executed in correct order one by one.

In PgDbMigration is correct order ensured by MigrationOrderNumber. This order number has two parts:

  • date-time part - the date (and time optionaly) when the migration was created
  • number part - the order of migration in given date-time part

The three step migration can be written this way:

// Program.cs
return TcKs.PgDbMigration.Runners.ProgramRunner.Run();
// <<< This oneliner will runn all the migratoins.

/* >>> in other files >>> */

// the base class for all migration done in this day
public abstract record BaseMigration : Migration {
  protected BaseMigration(params int[] number)
    //         year, month, day
    : base(new(2023, 09,    26), number) { }
  }
}

// The first migration of the batch.
public sealed record M1 : BaseMigration {
  public M1() : base(1) {
    // The title is optional. But it is good practice to say,
    // what the purpose of migratoin is.
    Title = "Sample migration creating necessary extensions.";
  }

  // This method is doing the work.
  public override void UpgradeDatabase(NpgsqlTransaction transaction) {
    _ = transaction.ExecuteScalar(@"CREATE EXTENSION IF NOT EXISTS ""uuid-ossp"";");
    _ = transaction.ExecuteScalar(@"CREATE EXTENSION IF NOT EXISTS ""citext"";");
    _ = transaction.ExecuteScalar("CREATE SCHEMA IF NOT EXISTS admin;");
  }
}

// The second migration of the batch.
public sealed record M2 : BaseMigration {
  public M2() : base(2) {
    Title = "Sample migration creating admins.admin table.";
  }

  public override void UpgradeDatabase(NpgsqlTransaction transaction) {
    _ = transaction.ExecuteScalar(@"
        CREATE TABLE admin.admins (
          id uuid NOT NULL PRIMARY KEY DEFAULT uuid_generate_v4(),
          created_at timestamp with time zone NOT NULL DEFAULT now(),
          updated_at timestamp with time zone NOT NULL DEFAULT now(),
          disabled_at timestamp with time zone NULL,
          email citext NOT NULL UNIQUE,
          display_name text NOT NULL
        );
    ");
  }
}

// The third migration of the batch.
public sealed record M3 : BaseMigration {
  public M3() : base(3) {
    Title = "Sample insertion into admins.admin table.";
  }

  public override void UpgradeDatabase(NpgsqlTransaction transaction) {
    _ = transaction.ExecuteScalar(@"
        INSERT INTO admin.admins
            (email, display_name)
            VALUES
            ('alice@example.com', 'Alice Awesome'),
            ('bruno@example.com', 'Bruno Best');
    ");
  }
}


Support

Feel free to open new issue if you have question or you found a bug.

Contributing

Contributors are vere welcome!

Open new merge-request and describe what purpose of the contribution is.

Especially contribution do documentation and examples are super welcome.

Authors and acknowledgment

  • Jakub (TcKs) Müller - founder

License

This project is licencsed under MIT open source license.

Project status

This project is under development. The main branch contains tested and functional version of the code base.

I am very excited to extend and improve this library.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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
6.0.1 155 9/27/2023

Initial release.