Skip to content

Instantly share code, notes, and snippets.

@danhawkins
Created August 29, 2022 11:39
Show Gist options
  • Save danhawkins/3dbf4da9c2f80507159280c8c3933d97 to your computer and use it in GitHub Desktop.
Save danhawkins/3dbf4da9c2f80507159280c8c3933d97 to your computer and use it in GitHub Desktop.
Reset Commanded projections (domain specific)
defmodule Mix.Tasks.ResetProjections do
@moduledoc """
Reset all system projects from events
"""
use Mix.Task
@projection_modules ~w(
Ivrl.Tournaments.Projectors.Tournament
Ivrl.Teams.Projectors.Team
Ivrl.Players.Projectors.Player
Ivrl.Matches.Projectors.Match
)
@projection_tables ~w(
matches
players
team_players
teams
tournament_teams
tournaments
projection_versions
)
alias EventStore.Config.Store, as: ConfigStore
alias Ivrl.Repo
def run(_) do
ensure_started()
IO.puts("Truncating projection tables")
truncate_tables()
IO.puts("Restarting subscriptions")
delete_subscriptions()
IO.puts("Projections ready to re-populate")
end
defp truncate_tables do
"TRUNCATE #{Enum.join(@projection_tables, ",")} RESTART IDENTITY CASCADE"
|> Repo.query!()
end
defp delete_subscriptions do
Enum.map(@projection_modules, &Ivrl.EventStore.delete_all_streams_subscription/1)
end
defp ensure_started do
[:postgrex, :ecto]
|> Enum.each(&Application.ensure_all_started/1)
Ivrl.Repo.start_link()
ConfigStore.start_link([])
Ivrl.EventStore.start_link()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment