Evolve Chronicle event types with migrations
Event type migrations in Cratis Chronicle describe how to transform an event payload between schema generations. Use them when you rename a property, add a field, or change a value’s representation and consumers still need to read an older shape.
Why migrations?
Section titled “Why migrations?”Changing an event class does not explain how its new properties relate to events already stored. A migration supplies that relationship: an upcast builds the newer generation from the older one, and a downcast builds the older generation from the newer one.
Chronicle applies these declarations when appending an event and stores the registered generations together. Consumers can then read the generation they expect. A migration can lose information, so compatibility depends on what your transformations preserve, not just on having a complete generation chain.
Defining migrations
Section titled “Defining migrations”Declare a migrator for the newer generation and the one immediately before it. It supplies the upcast and downcast for that pair. Chronicle reads the generation numbers from the event types, so you do not repeat them in the migrator.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Migrations;
[EventType("author-registered", generation: 2)]public record MigrationsAuthorRegistered(string FirstName, string LastName);
[EventTypeGenerationFor<MigrationsAuthorRegistered>(1)]public record MigrationsAuthorRegisteredV1(string Name);
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));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.events.migrations.EventTypeMigrationimport io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder
@EventType(id = "author-registered", generation = 2)data class MigrationsAuthorRegistered(val firstName: String, val lastName: String)
@EventType(id = "author-registered", generation = 1)data class MigrationsAuthorRegisteredV1(val name: String)
class MigrationsAuthorRegisteredMigration : EventTypeMigration<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1>( MigrationsAuthorRegistered::class, MigrationsAuthorRegisteredV1::class ) { override fun upcast(builder: EventTypeMigrationBuilder<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1>) { builder .split(MigrationsAuthorRegistered::firstName, MigrationsAuthorRegisteredV1::name, " ", 0) .split(MigrationsAuthorRegistered::lastName, MigrationsAuthorRegisteredV1::name, " ", 1) }
override fun downcast(builder: EventTypeMigrationBuilder<MigrationsAuthorRegisteredV1, MigrationsAuthorRegistered>) { builder.combine(MigrationsAuthorRegisteredV1::name, " ", MigrationsAuthorRegistered::firstName, MigrationsAuthorRegistered::lastName) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.events.migrations.EventTypeMigration;import io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder;
@EventType(id = "author-registered", generation = 2)record MigrationsAuthorRegistered(String firstName, String lastName) {}
@EventType(id = "author-registered", generation = 1)record MigrationsAuthorRegisteredV1(String name) {}
class MigrationsAuthorRegisteredMigration extends EventTypeMigration<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1> { MigrationsAuthorRegisteredMigration() { super(MigrationsAuthorRegistered.class, MigrationsAuthorRegisteredV1.class); }
@Override public void upcast(EventTypeMigrationBuilder<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1> builder) { builder .split("firstName", "name", " ", 0) .split("lastName", "name", " ", 1); }
@Override public void downcast(EventTypeMigrationBuilder<MigrationsAuthorRegisteredV1, MigrationsAuthorRegistered> builder) { builder.combine("name", " ", "firstName", "lastName"); }}defmodule MyApp.Events.MigrationsAuthorRegisteredV1 do use Cratis.Chronicle.Events.EventType, id: "author-registered", generation: 1
defstruct [:name]end
defmodule MyApp.Events.MigrationsAuthorRegistered do use Cratis.Chronicle.Events.EventType, id: "author-registered", generation: 2
defstruct [:first_name, :last_name]end
defmodule MyApp.Migrations.MigrationsAuthorRegisteredMigration do use Cratis.Chronicle.Events.Migration, from: {MyApp.Events.MigrationsAuthorRegisteredV1, generation: 1}, to: {MyApp.Events.MigrationsAuthorRegistered, generation: 2}
alias Cratis.Chronicle.Events.MigrationBuilder
@impl true def upcast(builder) do builder |> MigrationBuilder.split_property(:name, :first_name, " ", 0) |> MigrationBuilder.split_property(:name, :last_name, " ", 1) end
@impl true def downcast(builder) do builder |> MigrationBuilder.combine_properties([:first_name, :last_name], :name, " ") endendimport { eventType, eventTypeMigration, IEventTypeMigration, IEventMigrationBuilder } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType('author-registered', 1)class MigrationsAuthorRegisteredV1 { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
@eventType('author-registered', 2)class MigrationsAuthorRegistered { @field(String) readonly firstName: string; @field(String) readonly lastName: string;
constructor(firstName: string, lastName: string) { this.firstName = firstName; this.lastName = lastName; }}
@eventTypeMigration(MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1)class MigrationsAuthorRegisteredMigration implements IEventTypeMigration<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1> { upcast(builder: IEventMigrationBuilder<MigrationsAuthorRegistered, MigrationsAuthorRegisteredV1>): void { builder.properties(pb => pb .split('firstName', 'name', ' ', 0) .split('lastName', 'name', ' ', 1)); }
downcast(builder: IEventMigrationBuilder<MigrationsAuthorRegisteredV1, MigrationsAuthorRegistered>): void { builder.properties(pb => pb .combine('name', ' ', 'firstName', 'lastName')); }}In the .NET example, the previous generation takes its event type id from the current generation rather than carrying a separately typed id. Both generations therefore resolve to the same identity.
Generations of one event type
Section titled “Generations of one event type”A generation is one version of an event type’s shape. Generation 1 is the first. Each later generation has its own schema, and consecutive generations differ by exactly one step: the number of a migrated generation is always the previous number plus one. A migrator bridges one such step, so an event type at generation 3 needs a migrator for 1→2 and another for 2→3.
Every generation of an event type shares one event type id. Only the generation number varies. The generation validation rules describe what the kernel rejects when this chain is incomplete.
For the C# syntax, see C# client usage.
Migration operations
Section titled “Migration operations”Choose an operation based on what changed between the two shapes. The examples below show how each declaration relates a target property to its source.
Use a separator to split one source property into parts. The defining-migrations example uses this to turn a full name into separate name properties:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Migrations;
[EventType("person-registered", generation: 2)]public record MigrationsSplitPersonRegistered(string FirstName, string LastName);
[EventTypeGenerationFor<MigrationsSplitPersonRegistered>(1)]public record MigrationsSplitPersonRegisteredV1(string FullName);
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));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.events.migrations.EventTypeMigrationimport io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder
@EventType(id = "person-registered", generation = 2)data class MigrationsSplitPersonRegistered(val firstName: String, val lastName: String)
@EventType(id = "person-registered", generation = 1)data class MigrationsSplitPersonRegisteredV1(val fullName: String)
class MigrationsSplitPersonRegisteredMigration : EventTypeMigration<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1>( MigrationsSplitPersonRegistered::class, MigrationsSplitPersonRegisteredV1::class ) { override fun upcast(builder: EventTypeMigrationBuilder<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1>) { builder .split(MigrationsSplitPersonRegistered::firstName, MigrationsSplitPersonRegisteredV1::fullName, " ", 0) // Gets first part .split(MigrationsSplitPersonRegistered::lastName, MigrationsSplitPersonRegisteredV1::fullName, " ", 1) // Gets second part }
override fun downcast(builder: EventTypeMigrationBuilder<MigrationsSplitPersonRegisteredV1, MigrationsSplitPersonRegistered>) { builder.combine(MigrationsSplitPersonRegisteredV1::fullName, " ", MigrationsSplitPersonRegistered::firstName, MigrationsSplitPersonRegistered::lastName) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.events.migrations.EventTypeMigration;import io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder;
@EventType(id = "person-registered", generation = 2)record MigrationsSplitPersonRegistered(String firstName, String lastName) {}
@EventType(id = "person-registered", generation = 1)record MigrationsSplitPersonRegisteredV1(String fullName) {}
class MigrationsSplitPersonRegisteredMigration extends EventTypeMigration<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1> { MigrationsSplitPersonRegisteredMigration() { super(MigrationsSplitPersonRegistered.class, MigrationsSplitPersonRegisteredV1.class); }
@Override public void upcast( EventTypeMigrationBuilder<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1> builder) { builder .split("firstName", "fullName", " ", 0) // Gets first part .split("lastName", "fullName", " ", 1); // Gets second part }
@Override public void downcast( EventTypeMigrationBuilder<MigrationsSplitPersonRegisteredV1, MigrationsSplitPersonRegistered> builder) { builder.combine("fullName", " ", "firstName", "lastName"); }}defmodule MyApp.Events.MigrationsSplitPersonRegisteredV1 do use Cratis.Chronicle.Events.EventType, id: "person-registered", generation: 1
defstruct [:full_name]end
defmodule MyApp.Events.MigrationsSplitPersonRegistered do use Cratis.Chronicle.Events.EventType, id: "person-registered", generation: 2
defstruct [:first_name, :last_name]end
defmodule MyApp.Migrations.MigrationsSplitPersonRegisteredMigration do use Cratis.Chronicle.Events.Migration, from: {MyApp.Events.MigrationsSplitPersonRegisteredV1, generation: 1}, to: {MyApp.Events.MigrationsSplitPersonRegistered, generation: 2}
alias Cratis.Chronicle.Events.MigrationBuilder
@impl true def upcast(builder) do builder # Gets first part |> MigrationBuilder.split_property(:full_name, :first_name, " ", 0) # Gets second part |> MigrationBuilder.split_property(:full_name, :last_name, " ", 1) end
@impl true def downcast(builder) do builder |> MigrationBuilder.combine_properties([:first_name, :last_name], :full_name, " ") endendimport { eventType, eventTypeMigration, IEventTypeMigration, IEventMigrationBuilder } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType('person-registered', 1)class MigrationsSplitPersonRegisteredV1 { @field(String) readonly fullName: string;
constructor(fullName: string) { this.fullName = fullName; }}
@eventType('person-registered', 2)class MigrationsSplitPersonRegistered { @field(String) readonly firstName: string; @field(String) readonly lastName: string;
constructor(firstName: string, lastName: string) { this.firstName = firstName; this.lastName = lastName; }}
@eventTypeMigration(MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1)class MigrationsSplitPersonRegisteredMigration implements IEventTypeMigration<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1> { upcast(builder: IEventMigrationBuilder<MigrationsSplitPersonRegistered, MigrationsSplitPersonRegisteredV1>): void { builder.properties(pb => pb .split('firstName', 'fullName', ' ', 0) // Gets first part .split('lastName', 'fullName', ' ', 1)); // Gets second part }
downcast(builder: IEventMigrationBuilder<MigrationsSplitPersonRegisteredV1, MigrationsSplitPersonRegistered>): void { builder.properties(pb => pb .combine('fullName', ' ', 'firstName', 'lastName')); }}Combine
Section titled “Combine”Combine source properties with a separator when the target generation needs one value, such as a full name built from separate name properties:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Migrations;
[EventType("shipping-address-recorded", generation: 2)]public record MigrationsCombineShippingAddressRecorded(string FormattedAddress);
[EventTypeGenerationFor<MigrationsCombineShippingAddressRecorded>(1)]public record MigrationsCombineShippingAddressRecordedV1(string Street, string City);
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));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.events.migrations.EventTypeMigrationimport io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder
@EventType(id = "shipping-address-recorded", generation = 2)data class MigrationsCombineShippingAddressRecorded(val formattedAddress: String)
@EventType(id = "shipping-address-recorded", generation = 1)data class MigrationsCombineShippingAddressRecordedV1(val street: String, val city: String)
class MigrationsCombineShippingAddressRecordedMigration : EventTypeMigration<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1>( MigrationsCombineShippingAddressRecorded::class, MigrationsCombineShippingAddressRecordedV1::class ) { override fun upcast(builder: EventTypeMigrationBuilder<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1>) { // Joins with space separator builder.combine( MigrationsCombineShippingAddressRecorded::formattedAddress, " ", MigrationsCombineShippingAddressRecordedV1::street, MigrationsCombineShippingAddressRecordedV1::city ) }
override fun downcast(builder: EventTypeMigrationBuilder<MigrationsCombineShippingAddressRecordedV1, MigrationsCombineShippingAddressRecorded>) { builder .split(MigrationsCombineShippingAddressRecordedV1::street, MigrationsCombineShippingAddressRecorded::formattedAddress, " ", 0) .split(MigrationsCombineShippingAddressRecordedV1::city, MigrationsCombineShippingAddressRecorded::formattedAddress, " ", 1) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.events.migrations.EventTypeMigration;import io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder;
@EventType(id = "shipping-address-recorded", generation = 2)record MigrationsCombineShippingAddressRecorded(String formattedAddress) {}
@EventType(id = "shipping-address-recorded", generation = 1)record MigrationsCombineShippingAddressRecordedV1(String street, String city) {}
class MigrationsCombineShippingAddressRecordedMigration extends EventTypeMigration<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1> { MigrationsCombineShippingAddressRecordedMigration() { super(MigrationsCombineShippingAddressRecorded.class, MigrationsCombineShippingAddressRecordedV1.class); }
@Override public void upcast(EventTypeMigrationBuilder<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1> builder) { // Joins with space separator builder.combine("formattedAddress", " ", "street", "city"); }
@Override public void downcast(EventTypeMigrationBuilder<MigrationsCombineShippingAddressRecordedV1, MigrationsCombineShippingAddressRecorded> builder) { builder .split("street", "formattedAddress", " ", 0) .split("city", "formattedAddress", " ", 1); }}defmodule MyApp.Events.MigrationsCombineShippingAddressRecordedV1 do use Cratis.Chronicle.Events.EventType, id: "shipping-address-recorded", generation: 1
defstruct [:street, :city]end
defmodule MyApp.Events.MigrationsCombineShippingAddressRecorded do use Cratis.Chronicle.Events.EventType, id: "shipping-address-recorded", generation: 2
defstruct [:formatted_address]end
defmodule MyApp.Migrations.MigrationsCombineShippingAddressRecordedMigration do use Cratis.Chronicle.Events.Migration, from: {MyApp.Events.MigrationsCombineShippingAddressRecordedV1, generation: 1}, to: {MyApp.Events.MigrationsCombineShippingAddressRecorded, generation: 2}
alias Cratis.Chronicle.Events.MigrationBuilder
@impl true def upcast(builder) do builder # Joins with space separator |> MigrationBuilder.combine_properties([:street, :city], :formatted_address, " ") end
@impl true def downcast(builder) do builder |> MigrationBuilder.split_property(:formatted_address, :street, " ", 0) |> MigrationBuilder.split_property(:formatted_address, :city, " ", 1) endendimport { eventType, eventTypeMigration, IEventTypeMigration, IEventMigrationBuilder } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType('shipping-address-recorded', 1)class MigrationsCombineShippingAddressRecordedV1 { @field(String) readonly street: string; @field(String) readonly city: string;
constructor(street: string, city: string) { this.street = street; this.city = city; }}
@eventType('shipping-address-recorded', 2)class MigrationsCombineShippingAddressRecorded { @field(String) readonly formattedAddress: string;
constructor(formattedAddress: string) { this.formattedAddress = formattedAddress; }}
@eventTypeMigration(MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1)class MigrationsCombineShippingAddressRecordedMigration implements IEventTypeMigration<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1> { upcast(builder: IEventMigrationBuilder<MigrationsCombineShippingAddressRecorded, MigrationsCombineShippingAddressRecordedV1>): void { builder.properties(pb => pb .combine('formattedAddress', ' ', 'street', 'city')); // Joins with space separator }
downcast(builder: IEventMigrationBuilder<MigrationsCombineShippingAddressRecordedV1, MigrationsCombineShippingAddressRecorded>): void { builder.properties(pb => pb .split('street', 'formattedAddress', ' ', 0) .split('city', 'formattedAddress', ' ', 1)); }}Rename
Section titled “Rename”Use RenamedFrom when a property’s name changes but its value should carry across:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Migrations;
[EventType("payment-processed", generation: 2)]public record MigrationsRenamePaymentProcessed(decimal Amount);
[EventTypeGenerationFor<MigrationsRenamePaymentProcessed>(1)]public record MigrationsRenamePaymentProcessedV1(decimal OldAmount);
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));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.events.migrations.EventTypeMigrationimport io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder
@EventType(id = "payment-processed", generation = 2)data class MigrationsRenamePaymentProcessed(val amount: Double)
@EventType(id = "payment-processed", generation = 1)data class MigrationsRenamePaymentProcessedV1(val oldAmount: Double)
class MigrationsRenamePaymentProcessedMigration : EventTypeMigration<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1>( MigrationsRenamePaymentProcessed::class, MigrationsRenamePaymentProcessedV1::class ) { override fun upcast(builder: EventTypeMigrationBuilder<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1>) { builder.renamedFrom(MigrationsRenamePaymentProcessed::amount, MigrationsRenamePaymentProcessedV1::oldAmount) }
override fun downcast(builder: EventTypeMigrationBuilder<MigrationsRenamePaymentProcessedV1, MigrationsRenamePaymentProcessed>) { builder.renamedFrom(MigrationsRenamePaymentProcessedV1::oldAmount, MigrationsRenamePaymentProcessed::amount) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.events.migrations.EventTypeMigration;import io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder;
@EventType(id = "payment-processed", generation = 2)record MigrationsRenamePaymentProcessed(double amount) {}
@EventType(id = "payment-processed", generation = 1)record MigrationsRenamePaymentProcessedV1(double oldAmount) {}
class MigrationsRenamePaymentProcessedMigration extends EventTypeMigration<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1> { MigrationsRenamePaymentProcessedMigration() { super(MigrationsRenamePaymentProcessed.class, MigrationsRenamePaymentProcessedV1.class); }
@Override public void upcast( EventTypeMigrationBuilder<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1> builder) { builder.renamedFrom("amount", "oldAmount"); }
@Override public void downcast( EventTypeMigrationBuilder<MigrationsRenamePaymentProcessedV1, MigrationsRenamePaymentProcessed> builder) { builder.renamedFrom("oldAmount", "amount"); }}defmodule MyApp.Events.MigrationsRenamePaymentProcessedV1 do use Cratis.Chronicle.Events.EventType, id: "payment-processed", generation: 1
defstruct [:old_amount]end
defmodule MyApp.Events.MigrationsRenamePaymentProcessed do use Cratis.Chronicle.Events.EventType, id: "payment-processed", generation: 2
defstruct [:amount]end
defmodule MyApp.Migrations.MigrationsRenamePaymentProcessedMigration do use Cratis.Chronicle.Events.Migration, from: {MyApp.Events.MigrationsRenamePaymentProcessedV1, generation: 1}, to: {MyApp.Events.MigrationsRenamePaymentProcessed, generation: 2}
alias Cratis.Chronicle.Events.MigrationBuilder
@impl true def upcast(builder) do builder |> MigrationBuilder.rename_property(:old_amount, :amount) end
@impl true def downcast(builder) do builder |> MigrationBuilder.rename_property(:amount, :old_amount) endendimport { eventType, eventTypeMigration, IEventTypeMigration, IEventMigrationBuilder } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType('payment-processed', 1)class MigrationsRenamePaymentProcessedV1 { @field(Number) readonly oldAmount: number;
constructor(oldAmount: number) { this.oldAmount = oldAmount; }}
@eventType('payment-processed', 2)class MigrationsRenamePaymentProcessed { @field(Number) readonly amount: number;
constructor(amount: number) { this.amount = amount; }}
@eventTypeMigration(MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1)class MigrationsRenamePaymentProcessedMigration implements IEventTypeMigration<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1> { upcast(builder: IEventMigrationBuilder<MigrationsRenamePaymentProcessed, MigrationsRenamePaymentProcessedV1>): void { builder.properties(pb => pb .renamedFrom('amount', 'oldAmount')); }
downcast(builder: IEventMigrationBuilder<MigrationsRenamePaymentProcessedV1, MigrationsRenamePaymentProcessed>): void { builder.properties(pb => pb .renamedFrom('oldAmount', 'amount')); }}Default value
Section titled “Default value”Supply a default when older payloads lack a new property and absence is not the value you want:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Migrations;
[EventType("order-shipped", generation: 2)]public record MigrationsDefaultValueOrderShipped(string TrackingNumber, int RetryCount, string Description);
[EventTypeGenerationFor<MigrationsDefaultValueOrderShipped>(1)]public record MigrationsDefaultValueOrderShippedV1(string TrackingNumber);
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 }}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.events.migrations.EventTypeMigrationimport io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder
@EventType(id = "order-shipped", generation = 2)data class MigrationsDefaultValueOrderShipped(val trackingNumber: String, val retryCount: Int, val description: String)
@EventType(id = "order-shipped", generation = 1)data class MigrationsDefaultValueOrderShippedV1(val trackingNumber: String)
class MigrationsDefaultValueOrderShippedMigration : EventTypeMigration<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1>( MigrationsDefaultValueOrderShipped::class, MigrationsDefaultValueOrderShippedV1::class ) { override fun upcast(builder: EventTypeMigrationBuilder<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1>) { builder .defaultValue(MigrationsDefaultValueOrderShipped::retryCount, 42) .defaultValue(MigrationsDefaultValueOrderShipped::description, "default string") }
override fun downcast(builder: EventTypeMigrationBuilder<MigrationsDefaultValueOrderShippedV1, MigrationsDefaultValueOrderShipped>) { // retryCount and description did not exist in generation 1 — nothing to map back }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.events.migrations.EventTypeMigration;import io.cratis.chronicle.events.migrations.EventTypeMigrationBuilder;
@EventType(id = "order-shipped", generation = 2)record MigrationsDefaultValueOrderShipped(String trackingNumber, int retryCount, String description) {}
@EventType(id = "order-shipped", generation = 1)record MigrationsDefaultValueOrderShippedV1(String trackingNumber) {}
class MigrationsDefaultValueOrderShippedMigration extends EventTypeMigration<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1> { MigrationsDefaultValueOrderShippedMigration() { super(MigrationsDefaultValueOrderShipped.class, MigrationsDefaultValueOrderShippedV1.class); }
@Override public void upcast( EventTypeMigrationBuilder<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1> builder) { builder .defaultValue("retryCount", 42) .defaultValue("description", "default string"); }
@Override public void downcast( EventTypeMigrationBuilder<MigrationsDefaultValueOrderShippedV1, MigrationsDefaultValueOrderShipped> builder) { // retryCount and description did not exist in generation 1 — nothing to map back }}defmodule MyApp.Events.MigrationsDefaultValueOrderShippedV1 do use Cratis.Chronicle.Events.EventType, id: "order-shipped", generation: 1
defstruct [:tracking_number]end
defmodule MyApp.Events.MigrationsDefaultValueOrderShipped do use Cratis.Chronicle.Events.EventType, id: "order-shipped", generation: 2
defstruct [:tracking_number, :retry_count, :description]end
defmodule MyApp.Migrations.MigrationsDefaultValueOrderShippedMigration do use Cratis.Chronicle.Events.Migration, from: {MyApp.Events.MigrationsDefaultValueOrderShippedV1, generation: 1}, to: {MyApp.Events.MigrationsDefaultValueOrderShipped, generation: 2}
alias Cratis.Chronicle.Events.MigrationBuilder
@impl true def upcast(builder) do builder |> MigrationBuilder.default_value(:retry_count, 42) |> MigrationBuilder.default_value(:description, "default string") end
@impl true def downcast(builder) do # retry_count and description did not exist in generation 1 — nothing to map back builder endendimport { eventType, eventTypeMigration, IEventTypeMigration, IEventMigrationBuilder } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType('order-shipped', 1)class MigrationsDefaultValueOrderShippedV1 { @field(String) readonly trackingNumber: string;
constructor(trackingNumber: string) { this.trackingNumber = trackingNumber; }}
@eventType('order-shipped', 2)class MigrationsDefaultValueOrderShipped { @field(String) readonly trackingNumber: string; @field(Number) readonly retryCount: number; @field(String) readonly description: string;
constructor(trackingNumber: string, retryCount: number, description: string) { this.trackingNumber = trackingNumber; this.retryCount = retryCount; this.description = description; }}
@eventTypeMigration(MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1)class MigrationsDefaultValueOrderShippedMigration implements IEventTypeMigration<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1> { upcast(builder: IEventMigrationBuilder<MigrationsDefaultValueOrderShipped, MigrationsDefaultValueOrderShippedV1>): void { builder.properties(pb => pb .defaultValue('retryCount', 42) .defaultValue('description', 'default string')); }
downcast(_builder: IEventMigrationBuilder<MigrationsDefaultValueOrderShippedV1, MigrationsDefaultValueOrderShipped>): void { // retryCount and description did not exist in generation 1 — nothing to map back }}Value map
Section titled “Value map”A value map translates the values themselves. Use it when an enum is renumbered or a status code changes: each pair states which old value corresponds to which new value.
You declare a value map on the migration itself rather than inside the upcast or the downcast:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Events.Migrations;
public enum MigrationsSubscriptionStateV1{ Unknown = 0, Active = 1, Cancelled = 2}
public enum MigrationsSubscriptionState{ Unspecified = 100, Running = 101, Stopped = 102}
[EventType("subscription-state-changed", generation: 2)]public record MigrationsSubscriptionStateChanged(MigrationsSubscriptionState State);
[EventTypeGenerationFor<MigrationsSubscriptionStateChanged>(1)]public record MigrationsSubscriptionStateChangedV1(MigrationsSubscriptionStateV1 State);
public class MigrationsSubscriptionStateChangedMigration : EventTypeMigration<MigrationsSubscriptionStateChanged, MigrationsSubscriptionStateChangedV1>{ public override void Upcast(IEventMigrationBuilder<MigrationsSubscriptionStateChanged, MigrationsSubscriptionStateChangedV1> builder) { // The value map covers State in both directions }
public override void Downcast(IEventMigrationBuilder<MigrationsSubscriptionStateChangedV1, MigrationsSubscriptionStateChanged> builder) { // The value map covers State in both directions }
public override void MapValues(IEventValueMapBuilder<MigrationsSubscriptionStateChanged, MigrationsSubscriptionStateChangedV1> builder) => builder.For(current => current.State, previous => previous.State, map => map .Map(MigrationsSubscriptionStateV1.Unknown, MigrationsSubscriptionState.Unspecified) .Map(MigrationsSubscriptionStateV1.Active, MigrationsSubscriptionState.Running) .Map(MigrationsSubscriptionStateV1.Cancelled, MigrationsSubscriptionState.Stopped));}Kotlin does not support this workflow yet.Java does not support this workflow yet.Elixir does not support this workflow yet.TypeScript does not support this workflow yet.A map declared this way applies in both directions: forward when upcasting and inverted when downcasting. Values the map does not mention carry across unchanged, so you only need to declare values that changed.
If several old values map to one new value, there is no single inverse. Chronicle uses the first declared pair for that target value when downcasting. The two property expressions may also name different properties, allowing one map to handle both a rename and a value change.
The migration-level map runs before the upcast and downcast declarations. A direction’s explicit transformation for the same property takes precedence.
Value maps in one direction
Section titled “Value maps in one direction”Declare a map inside the upcast or downcast when the two directions need different translations. For example, if several old values collapse onto one new value, an explicit downcast map can choose which old value to return instead of using the first declared pair. Each direction can override the migration-level map for that property.
For the C# syntax, see C# client usage.
Properties inside a nested object
Section titled “Properties inside a nested object”The same operations work on properties inside nested objects. Chronicle follows the property path one segment at a time when reading and creates missing objects along the path when writing. A rename can therefore move a top-level value into a nested object as well as change its name.
A path that leads nowhere is rejected at registration with InvalidMigrationPropertyForEventType, which names the path so you can see which segment is wrong. Separators, default values and mapped values are literals and are never renamed. These operations describe property paths. They are not arbitrary custom-converter transformations.
For the C# syntax, including how typed expressions resolve to JSON names, see C# client usage.
When no operations are needed
Section titled “When no operations are needed”Adding or removing an optional property, at any depth, needs a migrator for the generation pair but no operations inside it. Chronicle converts a payload by walking the target generation’s schema. Upcasting leaves a property the older payload never carried absent, and downcasting drops a property the older generation does not declare. The migrator only has to exist so the generation chain is complete.
Use a default value when absence would give the wrong result and you need a specific value instead.
For the C# syntax, see C# client usage.
Chaining several generations
Section titled “Chaining several generations”When an event type spans more than two generations, declare one migrator per consecutive pair. Chronicle chains them: an event appended at generation 1 is upcast 1→2 and then 2→3, and all three generations are stored. Always migrate between consecutive generations, never directly from 1 to 3.
For the C# syntax, see C# client usage.
How migrations work
Section titled “How migrations work”On append, Chronicle starts with the generation supplied by the writer and loads the registered migrations for that event type. It upcasts through any higher generations and downcasts through any lower ones, storing all generations in the event sequence.
The appended generation is stored as written. The other generations contain only what the migrations produce. For example, the name split above turns “Mary Ann Smith” into “Mary” and “Ann” in generation 2, dropping “Smith”. Check both directions against the information each consumer needs.
A backfill adds missing generations to previously stored events; current backfills do not overwrite stored generations. Earlier backfills may already have overwritten content, and SQL revisions still replace content in place. The protection and backfill boundaries below matter when upgrading an existing store.
Opt in to pinned observer delivery
Section titled “Opt in to pinned observer delivery”Client reactors and reducers can request the generation their handlers subscribe to, rather than
receiving the appended generation live and the highest stored generation on replay. In the .NET
client, set ChronicleOptions.EventGenerationDelivery to EventGenerationDelivery.Pinned before
connecting; see the .NET client option.
The default remains Compatibility in this major version. Upgrade every kernel silo, meaning every running kernel node, before opting in.
For pinned observers, the kernel selects the subscribed generation’s content and hash and releases
protected values with that generation’s schema on live, catch-up, replay, and partition-retry delivery.
EventContext.EventType.Generation is the delivered generation; EventContext.AppendedGeneration
records the original append generation, or is null for older events where it is unknown.
If the pinned generation has not been stored yet, the kernel releases the known appended generation
(or the highest stored generation for legacy events) and migrates it for that delivery only. It does
not persist this result or cache plaintext. Such a delivery has EventHash.NotSet, because it has
no durable stored hash. Erased personal data stays erased through the migration.
A revised event is delivered from its latest revision, migrated to the pin when necessary. Its hash
is the revision’s stored hash only when the revision already matches the pin; otherwise it is
EventHash.NotSet. SQL revisions made before this release cannot be distinguished from original
content: those rows keep the existing generation-selection behavior until revised again.
A reactor or reducer can pin only one generation per event type. Registration rejects unregistered generations and handlers for multiple generations of the same type. If a pinned generation disappears from the registered definition later, delivery fails explicitly rather than substituting a schema. For reactors and reducers, changing a pin or delivery mode does not itself trigger a replay; request one when you need to rebuild prior state.
The .NET option also opts typed materialized projections into pinned delivery. Each From, Join, child,
key, parent-key, and removal clause uses the generation it names, including events read from storage
to resolve joins or parent keys. A pinned projection can name only one generation per event type
across all its clauses, including children and nested objects; registration rejects conflicting pins,
just as it does for reactors and reducers. Different event types can pin different generations.
Strongly consistent reads of a pinned projection (GetInstanceById, stream-scoped reads, Workbench
snapshots and previews) use the same generation selection and release as its observer. Like observers,
a read fails when its pinned generation is unavailable.
Live, catch-up, and replay use the same generation-selection and release boundary.
For a Projection Declaration Language (PDL) definition saved with GenerationDelivery = Pinned, a bare from PersonRegistered resolves
to the latest registered generation when you save it. That resolved generation is persisted as the
pin; registering a newer generation later does not move it. The same rule applies to join and removal
references, including those in children and nested objects. Saving the declaration again resolves its
names against the generations registered at that time.
PDL, which is owned by Screenplay, does not have generation-qualified event references. Use the .NET projection builders or model-bound attributes with the .NET event type for the generation you want when a clause must pin an explicit generation. Compatibility definitions keep the existing bare-name and identifier-only dispatch behavior.
Webhooks opt in separately, per registration, with the .NET builder’s
WithGenerationDelivery(EventGenerationDelivery.Pinned) or Core’s GenerationDelivery property.
Their subscribed event types supply exact generation pins, which remain fixed until you deliberately
update the registration. The HTTP payload envelope is unchanged, including GenerationalContent;
only the selected content, delivered generation, and hash follow the pin. See
webhook generation delivery.
Event-store subscriptions also opt in separately with
WithGenerationDelivery(EventGenerationDelivery.Pinned). The target application’s declared event types
supply exact pins for the source outbox observer. The kernel delivers those generations on live,
catch-up, replay, and retry, then appends the delivered generation to the target inbox. Both stores
must have that exact generation registered; a missing target generation fails forwarding without
appending or acknowledging the failing event. Pins survive reconnects and identical registrations;
only a deliberate registration update changes them. See
pinned subscription delivery.
GenerationalContent remains on delivered events for compatibility in this major version. The global
client option does not change immediate/passive or wildcard (all) projections, webhooks,
captures, or event-store subscriptions.
Protected values
Section titled “Protected values”Adding protection metadata within an existing generation affects future appends only. Creating a new generation also leaves the original generation’s stored content untouched. Neither approach encrypts historical plaintext in place; see Adding PII to an existing event type and Event redaction for the policy and remedy.
Migration expressions operate on plaintext, including values protected by [PII] or
[Encrypted]. Split, combine, and value-map operations therefore see the original
values rather than ciphertext.
Before storing a generated generation, Chronicle applies that generation’s protection
metadata under the original event’s subject, falling back to its event source when no
subject was supplied. This also protects default values in classified target properties.
A lower generation’s generated copy also inherits [PII] or [Encrypted] from a higher
generation that classifies the same plain-string property path. The copy at the generation
you append is protected with that generation’s own metadata, so keep the classification on
the generation your clients append. Pinned delivery releases
these inherited protected values. This does not apply to non-string or formatted
properties; keep the appropriate metadata on every generation for those properties.
Already stored copies are not re-encrypted; use Event redaction
when historical plaintext must be removed.
Compatibility delivery of an older generation now receives ciphertext for these
inherited protected properties. Use EventGenerationDelivery.Pinned to receive released
values in the pinned generation’s shape.
When a new generation is registered, backfill covers the default namespace’s event log only. It migrates from the known appended generation’s base content, not a separate revision. For legacy events whose appended generation is unknown, it uses the highest stored base generation without inferring an appended generation. Each added generation records that source, whether it is the known appended generation, and a content-addressed migration-definition version. Content, hash, and provenance are written together, guarded against concurrent revision or redaction. A conflicting write is re-read and retried once; a second conflict is logged and skipped. Changing a migration alone does not rewrite existing generations.
Register new generations only after every kernel silo has been upgraded. Older workers can still run replace-all backfills during a rolling upgrade.
Before transforming the source, Chronicle releases its content under the original subject. Erasing that subject’s PII keys also makes protected PII in the generated generations unavailable; independent confidentiality keys are not erased by a PII erasure request.
Registration
Section titled “Registration”Migrations are discovered and registered when your application connects to Chronicle. You declare the migrators, and the client then:
- Discovers all migrators in the application
- Captures the upcast and downcast declarations from each one
- Converts the declarations into JMESPath transformations
- Sends the definitions to the kernel during event type registration
For the C# discovery details, see How registration works.
Catching misuse early
Section titled “Catching misuse early”The C# client checks migration identities and duplicate transitions at startup. Its code analysis rules also catch invalid generation references at compile time. The kernel enforces the generation validation rules for every client.
| Check | What it rejects |
|---|---|
MigrationGenerationsMustShareEventTypeId |
EventTypeMigration<TUpgrade, TPrevious> throws during construction if its types resolve to different event type ids. |
MultipleMigratorsForSameEventTypeGeneration |
EventTypeMigrators.GetMigratorsFor rejects two migrator classes claiming the same generation transition. |
| CHR0037 | A migration’s types do not resolve to the same event type, or [EventTypeGenerationFor<T>] does not reference the migration’s TUpgrade. |
| CHR0049 | [EventTypeGenerationFor<T>] references a type without its own [EventType] identity, including another generation marker. |
Best practices
Section titled “Best practices”Keep one migrator for each consecutive pair of generations. In C#, derive the previous generation’s id with [EventTypeGenerationFor<T>] so it stays tied to the current generation’s [EventType] identity.
Test both directions with representative payloads, especially values that cannot be recovered after a split or a many-to-one map. Use defaults deliberately: an optional property may need no operation at all. Record what changed between generations so the next migration author can distinguish an intentional loss from a missing mapping.
To implement these declarations in C#, continue with C# client usage. Use the generation validation rules to diagnose registration failures.