ADatabaseMigrator 0.2.2
dotnet add package ADatabaseMigrator --version 0.2.2
NuGet\Install-Package ADatabaseMigrator -Version 0.2.2
<PackageReference Include="ADatabaseMigrator" Version="0.2.2" />
paket add ADatabaseMigrator --version 0.2.2
#r "nuget: ADatabaseMigrator, 0.2.2"
// Install ADatabaseMigrator as a Cake Addin #addin nuget:?package=ADatabaseMigrator&version=0.2.2 // Install ADatabaseMigrator as a Cake Tool #tool nuget:?package=ADatabaseMigrator&version=0.2.2
ADatabaseMigrator - An Appeasing Database Migrator
A small and flexible package you can use to run database migration scripts.
Getting started
- Create a class project to contain your migrations
- Place your migrations in a file structure like this:
\Scripts\Migrations\001.00.00\001_MyFirstMigration.sql \Scripts\Migrations\001.00.00\002_MySecondMigration.sql \Scripts\RunAlways\001_RunLog_.sql \Scripts\RunIfChanged\001_MyFirstView_.sql
- Add this
ItemGroup
to thecsproj
file to configure the files in the script folder become embedded resources in the assembly<ItemGroup> <EmbeddedResource Include="Scripts\**\*.sql" /> </ItemGroup>
- Add a
nuget
reference toADatabaseMigrator
- Create and invoke a migrator like so:
Some things to note about the configuration embedded resource script loader configuration here:var migrator = new Migrator( scriptLoader: new EmbeddedResourceScriptLoader(new MD5ScriptHasher(), config => config .UsingAssemblyFromType<ScriptLoaderTests>() .AddNamespaces<VersionFromPathVersionLoader>(MigrationScriptRunType.RunOnce, "Scripts.Migrations") .AddNamespaces<VersionFromAssemblyVersionLoader>(MigrationScriptRunType.RunIfChanged, "Scripts.RunIfChanged") .AddNamespaces<VersionFromAssemblyVersionLoader>(MigrationScriptRunType.RunAlways, "Scripts.RunAlways")), journalManager: new MigrationScriptJournalManager(connection), scriptRunner: new MigrationScriptRunner(connection)); await migrator.Migrate();
MigrationScriptRunType
:ADatabaseMigrator
comes predefined with some run types:MigrationScriptRunType.RunOnce
: Means this script is meant to run once. This can be used for changes to the database schema, like adding columns.MigrationScriptRunType.RunIfChanged
: Means this script is mean to run every time the content of the file is changed (this is what the script hasher is used for). This can be used for views or stored procedures.MigrationScriptRunType.RunAlways
: Means this script is mean to run every time. This could be used for keeping a log of every time the migrator is run for instance.
UsingAssemblyFromType<ScriptLoaderTests>()
specifies an assembly from which we can configure subsequent namespaces to fetch migrations from. We can call this method multiple times if we have migrations from multiple assemblies to load.AddNamespaces<VersionFromPathVersionLoader>(MigrationScriptRunType.RunOnce, "Scripts.Migrations")
, specifies that we want to load scripts from inside the folderScripts\Migrations
, we want them to be run only once and we want to extract the version number from the the path.AddNamespaces<VersionFromAssemblyVersionLoader>(MigrationScriptRunType.RunIfChanged, "Scripts.RunIfChanged")
specifies we want to load scripts from inside the folderScripts\RunIfChanged
, we want them to be run every time they are changed (when the hash of the file changes) and we use version number from the previously specified assembly.AddNamespaces<VersionFromAssemblyVersionLoader>(MigrationScriptRunType.RunAlways, "Scripts.RunAlways")
specifies that we want to load scripts from inside the folderScripts\RunAlways
, we want them to be run every time we run the migrator and we use version number from the previously specified assembly.- Note that the order of the
AddNamespaces
invocations specifies the execution order, so in our example the Migrations are executed first, then RunIfChanged and last RunAlways. Within a namespace the migrations found are executed first in order of version, then in order of embedded resource name. This means that if you want your scripts to run in a specific order, you have full control over this by adding namespaces in the order you want.
Packages and Contents
ADatabaseMigrator
Contains the Migrator
class which you instantiate to run your migrations. The class needs 3 things in order to be instantiated:
- A script loader, that can find the script files you want to run.
- A journal manager, that can load already executed scripts from the database.
- A script runner that can execute scripts.
You can write your own script loaders, journal managers and scripts runners, but the package comes with some predefined classes to use or extend:
EmbeddedResourceScriptLoader
is a script loader that loads scripts from embedded resources in one or more assemblies. This class also requires a script hasher in order to create a unique hash for the loaded scripts, theMD5ScriptHasher
is provided as a default script hasher.MigrationScriptJournalManager
that loads executed scripts from a table namedSchemaVersionJournal
.MigrationScriptRunner
that can execute scripts given a connection (and an optional transaction if you want the whole migration to be executed in a provided transaction).
ADatabaseFixture.SqlServer
Contains SqlServerMigrationScriptRunner
which you can use as a script runner instead of MigrationScriptRunner
if you need batch support (like if your scripts contains GO
statements for instance).
Logging
Simple logging of executed scripts or script failures can be implemented like this (replace Console.WriteLine
with your own choice of logging mechanism):
var migrator = new Migrator(/*...*/)
migrator.ScriptMigrationSucceeded += (_, args) => Console.WriteLine($"Executed script '{args.Script.Name}'");
migrator.ScriptMigrationFailed += (_, args) => Console.WriteLine($"Failed to execute script '{args.Script.Name}': {args.Exception}");
await migrator.Migrate();
Compatibility with GalacticWasteManagement
ADatabaseMigrator
is inspired by, and can be somewhat made compatible with Galactic-Waste-Management, which has been deprecated.
GalacticWasteManagement and ADatabaseMigrator solves slightly different problems. While GalacticWasteManagement can be used as a development tool as well as a deployment tool, has an array of built in migration types and modes, ADatabaseMigrator is a simpler and more focused tool. ADatabaseMigrator can execute migration scripts, and that's it. ADatabaseMigrator however is built to simplify configuration and extension to allow you to build your own migration orchestration by configuration, extending or replacing parts and making it easy to introduce custom run types if you feel the need for it.
Product | Versions 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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ADatabaseMigrator:
Package | Downloads |
---|---|
ADatabaseMigrator.SqlServer
ADatabaseMigrator.SqlServer a plugin to ADatabaseMigrator that supports parsing batched statements for Sql Server |
GitHub repositories
This package is not used by any popular GitHub repositories.