Skip to content

Commit

Permalink
Merge pull request #277 from grandcestry/master
Browse files Browse the repository at this point in the history
Allow configuring the default database
  • Loading branch information
slashdotdash committed Oct 18, 2023
2 parents 93ccebe + 8c44e6d commit a300b3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions guides/Getting Started.md
Expand Up @@ -41,6 +41,14 @@ EventStore is [available in Hex](https://hex.pm/packages/eventstore) and can be
url: "postgres://postgres:postgres@localhost/eventstore"
```


**Note:** Some managed database providers (such as DigitalOcean) don't provide access to the default `postgres` database. In such case, you can specify a default database in the following way:

```elixir
config :my_app, MyApp.EventStore,
default_database: "defaultdb",
```

**Note:** To use an EventStore with Commanded you should configure the event
store to use Commanded's JSON serializer which provides additional support for
JSON decoding:
Expand Down
8 changes: 6 additions & 2 deletions lib/event_store/storage/database.ex
Expand Up @@ -89,7 +89,9 @@ defmodule EventStore.Storage.Database do
raise ":database is nil in repository configuration"

encoding = opts[:encoding] || "UTF8"
opts = Keyword.put(opts, :database, "postgres")

default_database = Keyword.get(opts, :default_database, "postgres")
opts = Keyword.put(opts, :database, default_database)

command =
~s(CREATE DATABASE "#{database}" ENCODING '#{encoding}')
Expand Down Expand Up @@ -117,7 +119,9 @@ defmodule EventStore.Storage.Database do
Keyword.fetch!(opts, :database) || raise ":database is nil in repository configuration"

command = "DROP DATABASE \"#{database}\""
opts = Keyword.put(opts, :database, "postgres")

default_database = Keyword.get(opts, :default_database, "postgres")
opts = Keyword.put(opts, :database, default_database)

case run_query(command, opts) do
{:ok, _} ->
Expand Down

0 comments on commit a300b3b

Please sign in to comment.