Skip to content

Upgrading

Arc 2.0 declares queries with query and observe blocks instead of the @query and @observe attributes, reads the field types of the generated TypeScript from the model’s Chronicle schema instead of the types: option, and requires Chronicle for Elixir 5.0. mix arc.upgrade rewrites an existing application for both Arc and Chronicle.

  1. Bump the dependencies in mix.exs:

    {:cratis_arc, "~> 2.0"},
    {:cratis_chronicle, "~> 5.0"}
  2. Fetch them:

    Terminal window
    mix deps.get
  3. Rewrite your code:

    Terminal window
    mix arc.upgrade

    The task rewrites lib and test, or the paths you pass (mix arc.upgrade lib/my_app/authors), and formats the files it changed. --dry-run lists the files it would change and its notices without writing. It does not compile your application, which cannot compile against the new versions until the task has run.

    It applies Chronicle’s 5.0 rewrite first, the same as mix chronicle.upgrade: event types and read models get a schema block, reducers and reactors lose @handles, projections become define/1 pipelines, and every id whose 5.0 default differs is pinned to the one it was stored under, so nothing in the event store moves. Then it applies Arc’s: every @query or @observe with its function becomes a block, and the types: option of use Cratis.Arc.ReadModel is removed.

  4. Add Arc and the Chronicle client to import_deps in .formatter.exs, so field and the other declarations format without parentheses:

    [
    import_deps: [:cratis_arc, :cratis_chronicle],
    inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
    ]
  5. Act on the notices the task printed. Each names the file and what it left for you, for example:

    • the types: it removed from a read model module, such as removed types: [id: :uuid]. Give those fields the same types in the model’s schema.
    • a query function with more than one clause. Merge the clauses into one block.
    • anything Chronicle’s rewrite could not decide; see Upgrading Chronicle for Elixir.

    The task exits with 1 while there are notices.

  6. Compile:

    Terminal window
    mix compile
  7. Review the result with git diff.

Queries are blocks. query :name, opts do ... end and observe :name, opts do ... end replace an @query or @observe attribute followed by def name(args, ctx). The options are the same. The block still defines name(args, ctx), so calls such as Cratis.Arc.query/5 and your tests do not change. Inside the block, ctx is the context, args the map of converted parameters, and each declared parameter is bound by its name. A @doc before the block documents the query. @query and @observe now fail compilation with a message that shows the block form. See Queries and read models.

1.x
defmodule MyApp.Authors.Listing do
use Cratis.Arc.ReadModel, model: MyApp.Authors.Author, types: [id: :uuid]
@doc "One author."
@query params: [id: :uuid], many: false
def author(%{id: id}, ctx), do: Cratis.Chronicle.read_model(MyApp.Authors.Author, id, ctx.chronicle)
@observe many: true, watch: [MyApp.Authors.Author]
def live(_args, context) do
Cratis.Chronicle.all(MyApp.Authors.Author, context.chronicle)
end
end
# 2.0
defmodule MyApp.Authors.Listing do
use Cratis.Arc.ReadModel, model: MyApp.Authors.Author
@doc "One author."
query :author, params: [id: :uuid], many: false do
Cratis.Chronicle.read_model(MyApp.Authors.Author, id, ctx.chronicle)
end
observe :live, many: true, watch: [MyApp.Authors.Author] do
context = ctx
Cratis.Chronicle.all(MyApp.Authors.Author, context.chronicle)
end
end

Where the function matched its arguments with another pattern than the parameters’ names, or named its context something other than ctx, the task binds that name at the start of the block, as context = ctx above. Tidy those lines up when you review the diff.

The proxy reads field types from the model. The types: option of use Cratis.Arc.ReadModel is removed and is now a compile error. A Chronicle read model declares its types in its schema, so field :id, :uuid is what makes the id a Guid in TypeScript:

defmodule MyApp.Authors.Author do
use Cratis.Chronicle.ReadModels.ReadModel
schema do
field :id, :uuid
field :name, :string
end
@impl true
def define(projection) do
projection
|> from(MyApp.Authors.AuthorRegistered, set: [id: :event_source_id, name: :name])
end
end

A MongoDB document keeps its own types: on use Cratis.Arc.MongoDB.Document. A plain struct has its types inferred from its defaults; to give it another type, such as a UUID, use Cratis.Chronicle.Schema and declare its fields in a schema block. See React and generated proxies.

Arc requires Chronicle for Elixir 5.0. Event types and read models declare their fields in a schema block, reducers and reactors handle the events their clauses match, projections are define/1 pipelines, and an artifact without id: gets a derived kebab-case id. Upgrading Chronicle for Elixir covers each change.