Skip to content

Event Type Migrations

Event type migrations enable you to evolve your event schemas over time while maintaining compatibility with existing events. When an event type changes, you can define upcasters and downcasters that automatically transform events between different generations.

In evolving systems, event schemas naturally change:

  • Properties are added or removed
  • Properties are renamed
  • Complex properties are split or combined

Chronicle’s migration system allows you to:

  1. Define declarative transformation rules
  2. Automatically store all generations of an event when appending
  3. Read events in any generation format

To define a migration, extend the EventTypeMigration<TUpgrade, TPrevious> base class (it handles generation extraction and validation for you) and override Upcast/Downcast:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType]
public record MigrationsAuthorRegisteredV1(string Name);
[EventType("author-registered", generation: 2)]
public record MigrationsAuthorRegistered(string FirstName, string LastName);
public class MigrationsAuthorRegisteredMigration : EventTypeMigration<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1> builder) =>
builder.Properties(pb => pb
.Split(m => m.FirstName, e => e.Name, PropertySeparator.Space, SplitPartIndex.First)
.Split(m => m.LastName, e => e.Name, PropertySeparator.Space, SplitPartIndex.Second));
public override void Downcast(IEventMigrationBuilder<MigrationsAuthorRegisteredV1, MigrationsAuthorRegistered> builder) =>
builder.Properties(pb => pb
.Combine(m => m.Name, PropertySeparator.Space, e => e.FirstName, e => e.LastName));
}

Kotlin and Java don’t currently have a migration API — there’s no equivalent to EventTypeMigration<TUpgrade, TPrevious> in the Kotlin client SDK.

The migration builder supports the following operations:

Splits a source property into parts using a separator:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType]
public record MigrationsSplitPersonRegisteredV1(string FullName);
[EventType("person-registered", generation: 2)]
public record MigrationsSplitPersonRegistered(string FirstName, string LastName);
public class MigrationsSplitPersonRegisteredMigration : EventTypeMigration<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1> builder) =>
builder.Properties(pb => pb
.Split(m => m.FirstName, e => e.FullName, PropertySeparator.Space, SplitPartIndex.First) // Gets first part
.Split(m => m.LastName, e => e.FullName, PropertySeparator.Space, SplitPartIndex.Second)); // Gets second part
public override void Downcast(IEventMigrationBuilder<MigrationsSplitPersonRegisteredV1, MigrationsSplitPersonRegistered> builder) =>
builder.Properties(pb => pb
.Combine(m => m.FullName, PropertySeparator.Space, e => e.FirstName, e => e.LastName));
}

Combines multiple source properties into a single value using a separator:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType]
public record MigrationsCombineShippingAddressRecordedV1(string Street, string City);
[EventType("shipping-address-recorded", generation: 2)]
public record MigrationsCombineShippingAddressRecorded(string FormattedAddress);
public class MigrationsCombineShippingAddressRecordedMigration : EventTypeMigration<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1> builder) =>
builder.Properties(pb => pb
.Combine(m => m.FormattedAddress, PropertySeparator.Space, e => e.Street, e => e.City)); // Joins with space separator
public override void Downcast(IEventMigrationBuilder<MigrationsCombineShippingAddressRecordedV1, MigrationsCombineShippingAddressRecorded> builder) =>
builder.Properties(pb => pb
.Split(m => m.Street, e => e.FormattedAddress, PropertySeparator.Space, SplitPartIndex.First)
.Split(m => m.City, e => e.FormattedAddress, PropertySeparator.Space, SplitPartIndex.Second));
}

Renames a property from a previous name using RenamedFrom:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType]
public record MigrationsRenamePaymentProcessedV1(decimal OldAmount);
[EventType("payment-processed", generation: 2)]
public record MigrationsRenamePaymentProcessed(decimal Amount);
public class MigrationsRenamePaymentProcessedMigration : EventTypeMigration<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1> builder) =>
builder.Properties(pb => pb
.RenamedFrom(m => m.Amount, e => e.OldAmount));
public override void Downcast(IEventMigrationBuilder<MigrationsRenamePaymentProcessedV1, MigrationsRenamePaymentProcessed> builder) =>
builder.Properties(pb => pb
.RenamedFrom(m => m.OldAmount, e => e.Amount));
}

Sets a default value for a new property:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Migrations;
[EventType]
public record MigrationsDefaultValueOrderShippedV1(string TrackingNumber);
[EventType("order-shipped", generation: 2)]
public record MigrationsDefaultValueOrderShipped(string TrackingNumber, int RetryCount, string Description);
public class MigrationsDefaultValueOrderShippedMigration : EventTypeMigration<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1>
{
public override void Upcast(IEventMigrationBuilder<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1> builder) =>
builder.Properties(pb => pb
.DefaultValue(m => m.RetryCount, 42)
.DefaultValue(m => m.Description, "default string"));
public override void Downcast(IEventMigrationBuilder<MigrationsDefaultValueOrderShippedV1, MigrationsDefaultValueOrderShipped> builder)
{
// RetryCount and Description did not exist in generation 1 — nothing to map back
}
}

When an event is appended to the event store:

  1. Chronicle identifies the event’s current generation
  2. The migration system retrieves all registered migrations for the event type
  3. Upcasting: If there are higher generations, the event is transformed upward (1→2→3)
  4. Downcasting: If there are lower generations, the event is transformed downward (3→2→1)
  5. All generations are stored in the event sequence

This ensures that:

  • Older consumers can still read events in their expected format
  • Newer consumers can read events with the latest schema
  • No data is lost during schema evolution

Migrations are automatically discovered and registered when you connect to Chronicle. Simply implement IEventTypeMigrationFor<TEvent> in your client application, and Chronicle will:

  1. Discover all migrators via IClientArtifactsProvider
  2. Build migration definitions with JmesPath transformations
  3. Send the definitions to the kernel during event type registration
  1. Incremental generations: Always migrate between consecutive generations (1→2, 2→3, not 1→3)
  2. Reversible transformations: Ensure downcast can recreate the original structure where possible
  3. Default values: Use DefaultValue() for new properties that didn’t exist in older generations
  4. Test migrations: Verify both upcast and downcast transformations work correctly
  5. Document changes: Keep track of what changed between generations in your event types