Skip to content

Setup & Integration ​

Weasel provides database CLI commands through the JasperFx command line framework. These commands let you apply migrations, validate schema state, generate patch files, and dump DDL from the command line.

If you are here from Marten, Polecat, Fisher, Wolverine or CritterWatch: these are the db-* commands those tools list under dotnet run -- help. Each of them registers its databases with Weasel, so the commands below work the same way against a document store, Wolverine's envelope storage, a database-backed Wolverine transport, or an EF Core DbContext that Wolverine manages. See Choosing a Schema Management Approach for where these commands fit against runtime migration and startup setup.

TIP

The command line framework itself is documented with JasperFx: setup, writing your own commands, arguments and flags, environment checks and describe. This section covers the db-* commands Weasel adds.

Native AOT

Since JasperFx 2.101.0 the command line runs from a Native AOT binary, as long as every assembly that contributes commands is built with JasperFx.SourceGenerator. Weasel.Core and Weasel.EntityFrameworkCore are, from Weasel 9.50.0 on, so db-apply, db-assert, db-patch, db-dump, db-list and db-ef-migration all appear in help and run from the native binary. See Native AOT in the JasperFx documentation for what your application needs.

Enabling the CLI ​

To enable the Weasel CLI commands, make two changes to your Program.cs:

cs
var builder = WebApplication.CreateBuilder(args);

// 1. Add this call to enable JasperFx extensions
builder.Host.ApplyJasperFxExtensions();

// ... configure services as usual ...

var app = builder.Build();

// 2. Replace app.Run() with this:
return await app.RunJasperFxCommands(args);

snippet source | anchor

RunJasperFxCommands(args) will intercept any recognized CLI commands and run them instead of starting the web host. If no CLI command is detected, the application starts normally.

Available Commands ​

Run dotnet run -- help to see all available commands:

CommandDescription
db-applyApplies all outstanding changes to the database(s)
db-assertAsserts that the existing database(s) match the current configuration
db-patchExports a SQL patch and rollback file for pending changes
db-dumpDumps the entire DDL for the configured database(s)
db-listLists all configured databases

The resources command is not a Weasel command, but every Weasel database is also exposed through it. See The resources command below.

Common Flags ​

All database commands support the following flags:

FlagDescription
--database / -dFilter to a specific database by partial URI match
--conn / -cOverride the connection string
--logRecord command output to a file
--verboseEnable verbose output
--log-levelOverride the log level
--environmentOverride the hosting environment name

db-apply and db-assert also take --parallel <n> to walk a fleet of databases concurrently. See db-apply.

Database Discovery ​

Weasel discovers databases through dependency injection. Register your databases as IDatabase or IDatabaseSource implementations in the DI container, and the CLI commands will find them automatically.

The Critter Stack libraries do this registration for you:

LibraryWhat it registers
MartenThe document store's database, every tenant database under database-per-tenant, and each ancillary store
PolecatThe same, for SQL Server
FisherThe same, for SQLite files
WolverineIts envelope storage, the queue tables of a database-backed transport, and any EF Core DbContext it manages
CritterWatchThe console's own store and durability schema

db-list shows what was found. When an application has more than one, pass the SubjectUri it prints to --database / -d to target one of them, for example -d marten://store/ or -d wolverine://messages/main.

The resources command ​

Weasel's db-* commands act on Weasel databases only. The JasperFx stateful resource model is wider: a database schema, a Rabbit MQ exchange, an Azure Service Bus topic, or an SQS queue are all an IStatefulResource, and the resources command acts on every one the application has registered.

bash
dotnet run -- resources list      # every stateful resource in the application
dotnet run -- resources check     # verify each one, non-zero exit on any failure
dotnet run -- resources setup     # create or migrate each one
dotnet run -- resources clear     # wipe persisted state, keep the structure
dotnet run -- resources teardown  # remove each one

Every Weasel database registered through an IDatabaseSource is one of those resources, so resources setup applies the same migration db-apply does, and resources check runs the same comparison db-assert does. Use the db-* commands when you want the database-specific options (db-patch, db-dump, --parallel); use resources when one deployment step should provision the databases and the message broker objects together.

TIP

resources setup and db-apply are both an explicit instruction to migrate, so both apply the migration as CreateOrUpdate even when the database's AutoCreate is None. resources check and db-assert never change anything. See AutoCreate Policy.

The same Setup() can run at application startup instead of from the command line, with services.AddResourceSetupOnStartup(). The JasperFx documentation covers that, the IHost.SetupResources() / ResetResourceState() helpers for tests, and how to write a resource of your own.

Integration Testing ​

If you need the host to start automatically during integration testing (without CLI arguments), set:

cs
JasperFxEnvironment.AutoStartHost = true;

snippet source | anchor

This bypasses the CLI argument check and starts the host directly.

Released under the MIT License.