Skip to content

Commit

Permalink
Fetch existing subscription if create fails as already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
slashdotdash committed Jan 29, 2018
1 parent 09be5a2 commit 5c902d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
36 changes: 18 additions & 18 deletions guides/Getting Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ EventStore is [available in Hex](https://hex.pm/packages/eventstore) and can be

1. Add eventstore to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:eventstore, "~> 0.13"}]
end
```
```elixir
def deps do
[{:eventstore, "~> 0.13"}]
end
```

2. Add an `eventstore` config entry containing the PostgreSQL database connection details to each environment's mix config file (e.g. `config/dev.exs`):
```elixir
config :eventstore, EventStore.Storage,
serializer: EventStore.TermSerializer,
username: "postgres",
password: "postgres",
database: "eventstore_dev",
hostname: "localhost",
pool_size: 10,
pool_overflow: 5
```
```elixir
config :eventstore, EventStore.Storage,
serializer: EventStore.TermSerializer,
username: "postgres",
password: "postgres",
database: "eventstore_dev",
hostname: "localhost",
pool_size: 10,
pool_overflow: 5
```
The database connection pool configuration options are:
Expand All @@ -30,9 +30,9 @@ EventStore is [available in Hex](https://hex.pm/packages/eventstore) and can be
3. Create the EventStore database and tables using the `mix` task:
```console
$ mix do event_store.create, event_store.init
```
```console
$ mix do event_store.create, event_store.init
```
## Initialize an existing database
Expand Down
14 changes: 12 additions & 2 deletions lib/event_store/storage/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule EventStore.Storage.Subscription do
{:ok, subscription}
else
{:error, :subscription_not_found} ->
Subscription.Subscribe.execute(conn, stream_uuid, subscription_name, start_from, opts)
create_subscription(conn, stream_uuid, subscription_name, start_from, opts)

reply ->
reply
Expand All @@ -52,9 +52,19 @@ defmodule EventStore.Storage.Subscription do
def unsubscribe_from_stream(conn, stream_uuid, subscription_name, opts \\ []),
do: Subscription.Unsubscribe.execute(conn, stream_uuid, subscription_name, opts)

defp create_subscription(conn, stream_uuid, subscription_name, start_from, opts) do
case Subscription.Subscribe.execute(conn, stream_uuid, subscription_name, start_from, opts) do
{:error, :subscription_already_exists} ->
Subscription.Query.execute(conn, stream_uuid, subscription_name, opts)

reply ->
reply
end
end

defmodule All do
@moduledoc false

def execute(conn, opts) do
conn
|> Postgrex.query(Statements.query_all_subscriptions, [], opts)
Expand Down

0 comments on commit 5c902d8

Please sign in to comment.