Badgie.Migrator 1.3.355

dotnet tool install --global Badgie.Migrator --version 1.3.355
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local Badgie.Migrator --version 1.3.355
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Badgie.Migrator&version=1.3.355
nuke :add-package Badgie.Migrator --version 1.3.355

Badgie Migrator

A SQL migration tool originally built for Badgie

What are database migrations?

As you develop new versions of a server application which you want to deploy with no downtime, you'll need to write snippets of SQL that change the schema from the current version to the next, so they can be run on all the systems (production, staging, development...) in the same way. If these migrations are the only schema-changing operations on the database, this gives you a good guarantee that all the schemas remain in sync.

How are migrations used?

Usually there's a build step that runs migrations. This in general should happen before running the application or running tests. It is a convetion that we first alter the database in a backwards compatible way and then we deploy the new version of the application that takes advantage of the new version

Are migrations safe?

In order to make migrations safe there are a few preconditions to know about.

  1. each migration should have a guard, i.e. an IF statement that only runs the migration if the schema is in the right state. For example you might want to only add a certain table if the table is not present.
  2. each migration should therefore be idempotent: if you run it twice, it should only change the schema once
  3. migrations should be immutable
  4. migrations should either add or delete database entities, and avoid altering existing ones. Adding a column is fine, altering it can be problematic, renaming it is likely not a good idea at all. Be aware that usually migrations are run before a new deployment of code so a column rename would probably break the existing version momentarily while the code is deployed.
  5. in general data migrations (migrations that add, update or delete data) are complex and risky business and we recommend avoiding them in favor of writing code that seeds the database appropriately as it's run.

Why Badgie Migrator?

Badgie migrator offers significant advantages over common ORM migrators (i.e. ruby-on-rails or Entity Framework)!

  1. Easy, trivial deployability. The tool is just a small cross platform app which you can point to your migrations folder and it just works. It does not require the application to be present and it's completely decoupled from the platform you use. It just works.
  2. Migrations are written in SQL, because we believe in simplicity. Databases use SQL, use SQL with databases and not a made up "domain language" where you have to go through hoops just to effectively run the SQL you need.
  3. The state of migrations is kept safe in a database table. The table is easy to understand and "hack" if you ever need to -- not that this is ever required.
  4. Migrations are ever only run once, even if idempotent. Safety in depth.
  5. The tool catches migration mutations and stops them (unless you force it not to!) because we want to be able to run migrations on a "blank" db and recreate the current state reliably. If we mutate migrations we change the "past" and therefore the schema will not be the same between current systems (pre mutation) and newer systems (post mutation).
  6. We support cases in which you have multiple migration folders, and/or multiple db connections, so you can use this little tool agains big systems

Installation

Install the migrator tool either as a global tool:

dotnet tool install -g Badgie.Migrator

...or as a CliToolReference in your project:

<ItemGroup>
    <DotNetCliToolPackageReference Include="Badgie.Migrator"/>
</ItemGroup>

Usage

Once the tool is installed you can simply call it like:

dotnet-badgie-migrator <connection string> [drive:][path][filename pattern] [-f] [-i] [-d] [-n] [-V] [--no-stack-trace]
  -f runs mutated migrations
  -i if needed, installs the db table needed to store state
  -d:(SqlServer|Postgres|MySql) specifies whether to run against SQL Server, PostgreSQL or MySql
  -n avoids wrapping each execution in a transaction 
  -V verbose mode for debugging
  --no-stack-trace omits the (mostly useless) stack traces

Alternatively, if you have many databases to run migrations against you can pass a json configuration file with many configurations:

dotnet-badgie-migrator -json=<configuration filename.json>

Here is a sample file to use as a template:

[
  {
    "ConnectionString": "Connection 1",
    "Force": true,
    "Install": true,
    "SqlType": "SqlServer",
    "Path": "Path 1",
    "UseTransaction": true
  },                      
  {
    "ConnectionString": "Connection 2",
    "Force": false,
    "Install": false,
    "SqlType": "Postgres",
    "Path": "Path 2",
    "UseTransaction": false
  }
]

Building

dotnet pack -c Release

Creates the DotNet CLI Tool package in App/bin/Relase/badgie-migrator.{version}.nupkg

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
1.3.355 1,123 1/31/2024
1.2.354 86 1/31/2024
1.2.349 4,428 4/30/2023
1.2.348 381 4/21/2023
1.2.347 7,412 2/4/2023
1.1.0.346 320 2/4/2023
1.1.0.345 15,551 12/29/2022
1.1.0.344 322 12/29/2022
1.0.1.339 119,734 3/9/2022
1.0.1.338 466 3/9/2022
1.0.0.337 443 3/9/2022
1.0.0.336 582 3/8/2022
1.0.0.335 445 3/8/2022
1.0.0.334 109,284 6/5/2021
0.2.0.318 205,307 5/27/2020
0.2.0.316 24,873 3/20/2020
0.2.0.315 11,315 1/16/2020
0.2.0.314 534 1/16/2020
0.2.0.313 610 1/16/2020
0.2.0.312 593 1/16/2020
0.2.0.311 1,013 12/21/2019
0.2.0.310 535 12/20/2019
0.2.0.309 551 12/20/2019
0.2.0.308 568 12/19/2019
0.2.0.260 2,601 6/18/2019
0.2.0.256 591 6/14/2019
0.2.0.253 632 6/14/2019
0.2.0.250 598 6/13/2019
0.2.0.249 595 6/13/2019
0.2.0.248 611 6/13/2019
0.2.0.247 554 6/13/2019
0.1.0.246 532 6/13/2019
0.1.0.245 575 6/13/2019
0.1.0.244 585 6/13/2019
0.1.0 640 6/2/2019

Minor: Add verbose mode and optionally remove stack traces