From 8c44e6d48f035de9b857580736c11150310bfe05 Mon Sep 17 00:00:00 2001 From: Ilya Suzdalnitski Date: Tue, 12 Sep 2023 00:26:23 +0000 Subject: [PATCH] allow configuring the default database --- guides/Getting Started.md | 8 ++++++++ lib/event_store/storage/database.ex | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/guides/Getting Started.md b/guides/Getting Started.md index 923a5c60..4bb5e38d 100644 --- a/guides/Getting Started.md +++ b/guides/Getting Started.md @@ -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: diff --git a/lib/event_store/storage/database.ex b/lib/event_store/storage/database.ex index 6cdf8332..a7eabc0e 100644 --- a/lib/event_store/storage/database.ex +++ b/lib/event_store/storage/database.ex @@ -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}') @@ -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, _} ->