Migrations
Event type migrations are shared Chronicle behavior. The shared docs own the migration model and client-tabbed examples.
Declaring a previous generation
Section titled “Declaring a previous generation”A module representing an older generation of an event type can declare generation_for: instead
of giving itself a separate :id. The id is resolved from the target module, so the two
generations can never drift apart by a hand-typed, mismatched id string - see
A previous generation: generation_for: on the
Events page.
defmodule MyApp.Events.AuthorRegistered do use Cratis.Chronicle.Events.EventType, id: "author-registered", generation: 2 defstruct [:first_name, :last_name]end
defmodule MyApp.Events.AuthorRegisteredV1 do use Cratis.Chronicle.Events.EventType, generation_for: MyApp.Events.AuthorRegistered, generation: 1 defstruct [:name]end
defmodule MyApp.Migrations.AuthorRegisteredMigration do use Cratis.Chronicle.Events.Migration, from: {MyApp.Events.AuthorRegisteredV1, generation: 1}, to: {MyApp.Events.AuthorRegistered, 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 MigrationBuilder.combine_properties(builder, [:first_name, :last_name], :name, " ") endendMigration checks
Section titled “Migration checks”A few mistakes are checked before the client connects. By default the client logs the full error
and carries on; start it with strict_validation: true to raise instead, so a bad set fails
immediately instead of being retried. Cratis.Chronicle.Events.EventTypes.validate/2 runs the same
checks and returns :ok or {:error, exception}; validate!/2 raises.
- Two modules representing the same generation of the same event type id are reported as
Cratis.Chronicle.Events.MultipleEventTypesWithSameIdFound. - More than one migration module bridging the same pair of generations is reported as
Cratis.Chronicle.Events.MultipleMigratorsForSameEventTypeGeneration- exactly one migrator may bridge a pair of generations. - A migration whose
:fromand:toresolve to different event type ids raisesArgumentError(as in earlier releases, now with a longer message) - mark the previous generation withgeneration_for:instead of giving it a separate:id. - A non-adjacent generation gap (for example
from: generation: 1toto: generation: 3) raisesCratis.Chronicle.Events.InvalidMigrationGenerationGap.