-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from uberbrodt/master
Turn EventStore mix tasks into generic tasks for use with Distillery
- Loading branch information
Showing
9 changed files
with
264 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule EventStore.Tasks.Create do | ||
@moduledoc """ | ||
Task to create the EventStore | ||
""" | ||
|
||
alias EventStore.Storage.Database | ||
import EventStore.Tasks.Output | ||
|
||
@doc """ | ||
Runs task | ||
## Parameters | ||
- config: the parsed EventStore config | ||
## Opts | ||
- is_mix: set to `true` if running as part of a Mix task | ||
- quiet: set to `true` to silence output | ||
""" | ||
def exec(config, opts) do | ||
opts = Keyword.merge([is_mix: false, quiet: false], opts) | ||
|
||
case Database.create(config) do | ||
:ok -> | ||
write_info("The EventStore database has been created.", opts) | ||
|
||
{:error, :already_up} -> | ||
write_info("The EventStore database already exists.", opts) | ||
|
||
{:error, term} -> | ||
raise_msg( | ||
"The EventStore database couldn't be created, reason given: #{inspect(term)}.", | ||
opts | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule EventStore.Tasks.Drop do | ||
@moduledoc """ | ||
Task to drop the EventStore database. | ||
""" | ||
|
||
alias EventStore.Storage.Database | ||
import EventStore.Tasks.Output | ||
|
||
@doc """ | ||
Runs task | ||
## Parameters | ||
- config: the parsed EventStore config | ||
## Opts | ||
- is_mix: set to `true` if running as part of a Mix task | ||
- quiet: set to `true` to silence output | ||
""" | ||
def exec(config, opts) do | ||
opts = Keyword.merge([is_mix: false, quiet: false], opts) | ||
|
||
case Database.drop(config) do | ||
:ok -> | ||
write_info("The EventStore database has been dropped.", opts) | ||
|
||
{:error, :already_down} -> | ||
write_info("The EventStore database has already been dropped.", opts) | ||
|
||
{:error, term} -> | ||
raise_msg( | ||
"The EventStore database couldn't be dropped, reason given: #{inspect(term)}.", | ||
opts | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule EventStore.Tasks.Init do | ||
@moduledoc """ | ||
Task to initalize the EventStore database | ||
""" | ||
|
||
import EventStore.Tasks.Output | ||
alias EventStore.Storage.Initializer | ||
|
||
@is_events_table_exists """ | ||
SELECT EXISTS ( | ||
SELECT 1 | ||
FROM information_schema.tables | ||
WHERE table_schema = 'public' | ||
AND table_name = 'events' | ||
) | ||
""" | ||
|
||
@doc """ | ||
Runs task | ||
## Parameters | ||
- config: the parsed EventStore config | ||
## Opts | ||
- is_mix: set to `true` if running as part of a Mix task | ||
- quiet: set to `true` to silence output | ||
""" | ||
def exec(config, opts) do | ||
opts = Keyword.merge([is_mix: false, quiet: false], opts) | ||
|
||
{:ok, conn} = Postgrex.start_link(config) | ||
|
||
conn_opts = [pool: DBConnection.Poolboy] | ||
|
||
Postgrex.query!(conn, @is_events_table_exists, [], conn_opts) | ||
|> case do | ||
%{rows: [[true]]} -> | ||
write_info("The EventStore database has already been initialized.", opts) | ||
|
||
%{rows: [[false]]} -> | ||
Initializer.run!(conn, conn_opts) | ||
write_info("The EventStore database has been initialized.", opts) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
defmodule EventStore.Tasks.Migrate do | ||
@moduledoc """ | ||
Task to migrate EventStore | ||
""" | ||
|
||
alias EventStore.Storage.Database | ||
import EventStore.Tasks.Output | ||
|
||
@available_migrations [ | ||
"0.13.0", | ||
"0.14.0" | ||
] | ||
|
||
@doc """ | ||
Run task | ||
## Parameters | ||
- config: the parsed EventStore config | ||
## Opts | ||
- is_mix: set to `true` if running as part of a Mix task | ||
- quiet: set to `true` to silence output | ||
""" | ||
def exec(config, opts) do | ||
opts = Keyword.merge([is_mix: false, quiet: false], opts) | ||
|
||
migrations = | ||
case event_store_schema_version(config) do | ||
[] -> | ||
# run all migrations | ||
@available_migrations | ||
|
||
[event_store_version | _] -> | ||
# only run newer migrations | ||
@available_migrations | ||
|> Enum.filter(fn migration_version -> | ||
migration_version |> Version.parse!() |> Version.compare(event_store_version) == :gt | ||
end) | ||
end | ||
|
||
migrate(config, opts, migrations) | ||
end | ||
|
||
defp migrate(_config, opts, []) do | ||
write_info("The EventStore database is already migrated.", opts) | ||
end | ||
|
||
defp migrate(config, opts, migrations) do | ||
for migration <- migrations do | ||
write_info("Running migration v#{migration}...", opts) | ||
|
||
path = Application.app_dir(:eventstore, "priv/event_store/migrations/v#{migration}.sql") | ||
script = File.read!(path) | ||
|
||
case Database.migrate(config, script) do | ||
:ok -> | ||
:ok | ||
|
||
{:error, term} -> | ||
raise_msg( | ||
"The EventStore database couldn't be migrated, reason given: #{inspect(term)}.", | ||
opts | ||
) | ||
end | ||
end | ||
|
||
write_info("The EventStore database has been migrated.", opts) | ||
end | ||
|
||
defp event_store_schema_version(config) do | ||
config | ||
|> query_schema_migrations() | ||
|> Enum.sort(fn left, right -> | ||
case Version.compare(left, right) do | ||
:gt -> true | ||
_ -> false | ||
end | ||
end) | ||
end | ||
|
||
defp query_schema_migrations(config) do | ||
with {:ok, conn} <- Postgrex.start_link(config) do | ||
conn | ||
|> Postgrex.query!( | ||
"SELECT major_version, minor_version, patch_version FROM schema_migrations", | ||
[], | ||
pool: DBConnection.Poolboy | ||
) | ||
|> handle_response() | ||
end | ||
end | ||
|
||
defp handle_response(%Postgrex.Result{num_rows: 0}), do: [] | ||
|
||
defp handle_response(%Postgrex.Result{rows: rows}) do | ||
Enum.map(rows, fn [major_version, minor_version, patch_version] -> | ||
Version.parse!("#{major_version}.#{minor_version}.#{patch_version}") | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule EventStore.Tasks.Output do | ||
@moduledoc false | ||
|
||
def write_info(msg, opts) do | ||
unless opts[:quiet] do | ||
info(msg, opts[:is_mix]) | ||
end | ||
end | ||
|
||
defp info(msg, true) do | ||
Mix.shell().info(msg) | ||
end | ||
|
||
defp info(msg, _) do | ||
IO.puts(msg) | ||
end | ||
|
||
def raise_msg(msg, opts) do | ||
unless opts[:quiet] do | ||
do_raise(msg, opts[:is_mix]) | ||
end | ||
end | ||
|
||
defp do_raise(msg, true) do | ||
Mix.raise(msg) | ||
end | ||
|
||
defp do_raise(msg, _) do | ||
raise msg | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.