Skip to content

The search box knows all the secrets -- try it!

Polecat is part of the Critter Stack ecosystem.

JasperFx Logo JasperFx provides formal support for Polecat and other Critter Stack libraries. Please check our Support Plans for more details.

Schema Migrations ​

Polecat uses Weasel.SqlServer for diff-based schema migrations.

Choosing an Approach ​

Polecat always computes a migration the same way: Weasel compares the schema objects your StoreOptions describe against the live database and produces the DDL for the difference. What you choose is when that runs and who runs it:

ApproachHowTypical use
Lazily, on first useAutoCreateSchemaObjects = AutoCreate.CreateOrUpdate (the default), described belowLocal development
Once at application startup.ApplyAllDatabaseChangesOnStartup() on AddPolecat(), or services.AddResourceSetupOnStartup() to set up every JasperFx stateful resource in the application, Wolverine's message storage and broker objects includedDevelopment, integration tests, and production deployments where the application owns its schema
As an explicit deployment stepdotnet run -- db-apply, or dotnet run -- resources setup for every resource at once, with the application itself on AutoCreate.NoneProduction, when the deployment identity may run DDL
From reviewed SQL scriptsdotnet run -- db-patch and db-dump, applied by a DBA or a migration tool, with the application on AutoCreate.NoneProduction, when the application's credentials cannot run DDL

In every case, dotnet run -- db-assert (or resources check) reports whether the database matches the configuration without changing anything, and is the right gate for a deployment pipeline.

Auto-Create Behavior ​

By default (AutoCreate.CreateOrUpdate), Polecat automatically creates and updates database schema on application startup:

cs
opts.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;

What Auto-Create Does ​

  • Creates document tables on first use
  • Adds new columns when document configuration changes (e.g., enabling soft deletes)
  • Drops columns and indexes that the configuration no longer declares on a table it knows about (removing a duplicated field drops that column and its data)
  • Creates event store tables on startup, including the tables flat table projections write to
  • Creates the HiLo table on first use
  • Never drops a whole table

What Auto-Create Does NOT Do ​

  • Drop or recreate a table (only AutoCreate.All does that, and only when a change cannot be expressed as an ALTER)
  • Rename columns
  • Touch objects the configuration does not know about

CreateOrUpdate is not "additive only"

For a table it manages, the update delta brings the table in line with the configuration in both directions, so a column that is in the database and no longer in the configuration is dropped. If a deployment must never lose a schema object, use AutoCreate.CreateOnly, which refuses the migration instead. This is Weasel behaviour shared by every Critter Stack store; see the AutoCreate policy.

Disabling Auto-Create ​

For production environments:

cs
opts.AutoCreateSchemaObjects = AutoCreate.None;

When auto-create is disabled, you're responsible for ensuring the database schema matches your configuration. There are three ways to meet that:

  • Run dotnet run -- db-apply or dotnet run -- resources setup as a deployment step. Both are an explicit instruction to migrate, so both apply the changes as CreateOrUpdate even when the store is configured with None, and CreateOrUpdate never drops a table.
  • Generate scripts for a DBA with db-patch (the delta against the live database, plus a rollback file) or db-dump (the whole schema), or programmatically with the schema export feature.
  • Keep .ApplyAllDatabaseChangesOnStartup() or AddResourceSetupOnStartup() registered. They respect AutoCreate.None and do nothing under it, so the application can carry the same registration in every environment with the policy deciding whether it acts.

Then gate the deployment with dotnet run -- db-assert, which exits non-zero when the database differs from the configuration. None on its own does not report drift: a missing table surfaces as SQL Server's own Invalid object name error.

Migration Flow ​

On startup, Polecat's migration flow:

  1. Compare desired schema (from configuration) against actual database schema
  2. Generate DDL for differences (new tables, new columns)
  3. Execute DDL within a transaction
  4. Run initial data seeding if configured

Concurrent Migrations ​

Everything that changes a store's schema takes the same application lock first, so two replicas starting together, or a daemon and a session's first write, cannot collide on a fresh schema. That covers startup migrations, ApplyAllConfiguredChangesToDatabaseAsync(), db-apply, resources setup, the async daemon, a session's first use of the event store or of a document table, the HiLo table and pc_tenants. The lock is per schema within a database, so tenants' databases and stores in other schemas do not block each other.

Whoever finds the lock taken waits up to StoreOptions.StartupMigrationLockTimeout, 30 seconds by default, and then finds the schema already there:

cs
opts.StartupMigrationLockTimeout = TimeSpan.FromMinutes(5);

If the wait runs out, a session's first use fails and the next use tries again, and an apply fails or carries on according to ResourceMigrationFailureMode.

A session with a transaction open never waits, because a migration waiting on that transaction's locks would then wait on the session forever, with nothing SQL Server can detect. If the lock is taken it skips the check, so a table that exists is used as normal and a table that does not fails the statement straight away. A HiLo sequence used outside a session behaves the same way, because it can't tell whether its caller has a transaction open.

Weasel.SqlServer ​

All schema management is delegated to Weasel.SqlServer, which provides:

  • Diff-based migrations -- Only applies changes that are needed
  • Bounded operations -- Never drops a whole table except under AutoCreate.All; see the warning above for what CreateOrUpdate does drop
  • Idempotent -- Safe to run multiple times
  • Transaction safety -- Schema changes are transactional

Command Line Schema Management ​

With RunJasperFxCommands(args) on a host that calls AddPolecat(...), both families of schema commands see the Polecat database:

bash
dotnet run -- db-apply
CommandDoes
db-applyApply every outstanding migration. --parallel <n> walks a fleet of tenant databases concurrently
db-assertFail if the database is out of step with the configuration — useful as a deployment gate
db-patchWrite the delta against the live database as a SQL file, with a rollback file beside it
db-dumpWrite the whole DDL to a file instead of executing it
db-listList the databases the host manages, with the identifiers the other commands' -d flag takes
resources setupProvision every registered resource, Polecat's schema among them
resources list / resources checkList or verify registered resources

The db-* commands are Weasel's and operate on Weasel databases specifically; resources * is JasperFx's stateful resource model and covers every JasperFx resource in the application, so on a Wolverine host it also provisions envelope storage and broker topology alongside the Polecat schema. Both work against a single database and against every tenant database under separate-database or master-table tenancy, and ancillary stores registered with AddPolecatStore<T>() are included.

The same Setup() that resources setup runs can run at application startup instead, with services.AddResourceSetupOnStartup(). For a Polecat-only host that is equivalent to .ApplyAllDatabaseChangesOnStartup(); on a host with Wolverine it also builds the message storage and any broker objects before the first message moves.

Released under the MIT License.