Giter Site home page Giter Site logo

Comments (3)

slashdotdash avatar slashdotdash commented on June 2, 2024 1

For dynamic event stores you cannot use the Mix tasks provided by the event store library. Instead you will need to create your own Mix task - or release tasks module for deployment - and manually call the event store tasks to create, init, and migrate the separate event store schemas.

You need to include the dynamic schema in the event store's config:

alias EventStore.Tasks.{Create, Init, Migrate}
alias MyApp.Commanded.EventStore

for schema <- ["eventstore_default", "eventstore_other"] do
  config = EventStore.config() |> Keyword.merge(pool_size: 1, schema: schema)

  Create.exec(config, quiet: true)
  Init.exec(config, quiet: true)
  Migrate.exec(config, quiet: true)
end

For deployment using a release tasks module see the Phoenix deploying with releases documentation where they have an example of Ecto migrations and custom commands. Example show below.

defmodule MyApp.Release do
  alias EventStore.Tasks.{Create, Init, Migrate}
  alias MyApp.Commanded.EventStore

  @app :my_app

  def migrate do
    load_app()

    for schema <- ["eventstore_default", "eventstore_other"] do
      config = EventStore.config() |> Keyword.merge(pool_size: 1, schema: schema)

      Create.exec(config, quiet: true)
      Init.exec(config, quiet: true)
      Migrate.exec(config, quiet: true)
    end
  end

  defp load_app do
    Application.load(@app)
  end
end

Then you'd run:

$ MIX_ENV=prod mix release
$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.migrate"

from eventstore.

suzdalnitski avatar suzdalnitski commented on June 2, 2024

Worked like a charm, thank you!

from eventstore.

kingdomcoding avatar kingdomcoding commented on June 2, 2024

I think this information should be somewhere public, probably in the guides.

For anyone curious, here's what I modified my (autogenerated, as per the guide referred to above) releases module into for deployment using elixir releases

defmodule MyApp.Release do
  @moduledoc """
  Used for executing DB release tasks when run in production without Mix
  installed.
  """
  alias EventStore.Tasks.{Create, Init, Migrate}

  @app :my_app

  def migrate do
    load_app()

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    load_app()
    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  def setup_event_stores do
    load_app()

    for event_store <- event_stores() do
      config = event_store.config()

      Create.exec(config, quiet: true)
      Init.exec(config, quiet: true)
      Migrate.exec(config, quiet: true)
    end
  end

  defp repos do
    Application.fetch_env!(@app, :ecto_repos)
  end

  defp event_stores do
    Application.fetch_env!(@app, :event_stores)
  end

  defp load_app do
    Application.load(@app)
  end
end

setup_event_stores/0 is the interesting function

Then I ran

$ MIX_ENV=prod mix release
$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.migrate"
$ _build/prod/rel/my_app/bin/my_app eval "MyApp.Release.setup_event_stores"

from eventstore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.