Curiosus.Migrations.Utils
6.0.0
dotnet add package Curiosus.Migrations.Utils --version 6.0.0
NuGet\Install-Package Curiosus.Migrations.Utils -Version 6.0.0
<PackageReference Include="Curiosus.Migrations.Utils" Version="6.0.0" />
<PackageVersion Include="Curiosus.Migrations.Utils" Version="6.0.0" />
<PackageReference Include="Curiosus.Migrations.Utils" />
paket add Curiosus.Migrations.Utils --version 6.0.0
#r "nuget: Curiosus.Migrations.Utils, 6.0.0"
#:package Curiosus.Migrations.Utils@6.0.0
#addin nuget:?package=Curiosus.Migrations.Utils&version=6.0.0
#tool nuget:?package=Curiosus.Migrations.Utils&version=6.0.0
Curiosus.Migrations.Utils
Helpers for Curiosus.Migrations code migrations.
MassUpdateCodeMigrationBase updates large tables in small batches, each in its own short transaction, with a pause between batches. This lets data migrations run on a live production database without long locks.
Installation
dotnet add package Curiosus.Migrations.Utils
Usage
Derive from MassUpdateCodeMigrationBase. The base class marks the migration as long-running and turns off the engine transaction. Call DoMassUpdateAsync with a query that selects a limited batch where id > @id and returns the updated ids. The loop continues until a batch comes back empty.
using Curiosus.Migrations;
using Curiosus.Migrations.Utils;
public class FillNewResultCodeMigration : MassUpdateCodeMigrationBase
{
public FillNewResultCodeMigration() : base(stepDelay: TimeSpan.FromMilliseconds(100)) { }
public override MigrationVersion Version => new(3, 1);
public override string? Comment => "Fill calls.new_result_code in batches";
public override async Task UpgradeAsync(DbTransaction? transaction = null, CancellationToken cancellationToken = default)
{
const string sql = @"
WITH cte AS (
SELECT id FROM calls
WHERE id > @id
ORDER BY id
LIMIT 10000)
UPDATE calls c
SET new_result_code = result_code + 1
FROM cte
WHERE cte.id = c.id
RETURNING cte.id;";
var total = await DoMassUpdateAsync(
sql,
(step, processed) => Logger?.LogInformation("Updated {Step} rows, {Total} total", step, processed),
cancellationToken);
}
}
The example is for PostgreSQL. On other databases, write an equivalent batched UPDATE that returns the ids it changed. Because the migration is long-running, it runs only when the policy allows it, for example MigrationPolicy.LongRunningAllowed or MigrationPolicy.AllAllowed.
See also
- Curiosus.Migrations: core engine
- Curiosus.Migrations.PostgreSQL and Curiosus.Migrations.SqlServer: database providers
- Code migrations
- Short-running vs long-running migrations
- Curiosus.Migrations: repository and all packages
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Curiosus.Migrations (>= 6.0.0)
- Dapper (>= 2.1.66)
-
net9.0
- Curiosus.Migrations (>= 6.0.0)
- Dapper (>= 2.1.66)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.