From eeaaf8b542025e89851eeb38e64c4cda1f49e362 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Thu, 23 Jan 2025 10:40:36 +0000 Subject: [PATCH] Move ErrorTracker to its own repo and file Note that the SQlite adapter supports version 2 onwards. --- config/config.exs | 6 ++++-- config/dev.exs | 6 ++++++ config/runtime.exs | 13 +++++++++++++ config/test.exs | 5 +++++ lib/music_library/application.ex | 1 + lib/music_library/error_repo.ex | 5 +++++ .../migrations/20250101170610_add_error_tracker.exs | 3 +-- 7 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 lib/music_library/error_repo.ex rename priv/{repo => error_repo}/migrations/20250101170610_add_error_tracker.exs (50%) diff --git a/config/config.exs b/config/config.exs index ca85aaca..082ead88 100644 --- a/config/config.exs +++ b/config/config.exs @@ -8,7 +8,7 @@ import Config config :music_library, - ecto_repos: [MusicLibrary.Repo], + ecto_repos: [MusicLibrary.Repo, MusicLibrary.ErrorRepo], generators: [timestamp_type: :utc_datetime, binary_id: true] config :music_library, MusicLibraryWeb, login_password: "change me", api_token: "change me" @@ -69,10 +69,12 @@ config :logger, :console, config :phoenix, :json_library, Jason config :error_tracker, - repo: MusicLibrary.Repo, + repo: MusicLibrary.ErrorRepo, otp_app: :music_library, enabled: true +config :music_library, MusicLibrary.ErrorRepo, priv: "priv/error_repo" + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{config_env()}.exs" diff --git a/config/dev.exs b/config/dev.exs index 502a7b99..6dd16ff5 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -8,6 +8,12 @@ config :music_library, MusicLibrary.Repo, stacktrace: true, show_sensitive_data_on_connection_error: true +config :music_library, MusicLibrary.ErrorRepo, + database: Path.expand("../data/music_library_errors_dev.db", __DIR__), + pool_size: 5, + stacktrace: true, + show_sensitive_data_on_connection_error: true + # For development, we disable any cache and enable # debugging and code reloading. # diff --git a/config/runtime.exs b/config/runtime.exs index 6d97bb4a..b27c6bba 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -51,6 +51,19 @@ if config_env() == :prod do cache_size: -8000, pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5") + error_database_path = + System.get_env("ERROR_DATABASE_PATH") || + raise """ + environment variable ERROR_DATABASE_PATH is missing. + For example: /etc/music_library/music_library_errors.db + """ + + config :music_library, MusicLibrary.ErrorRepo, + database: error_database_path, + # 16MB * pool_size = base memory usage + cache_size: -8000, + pool_size: String.to_integer(System.get_env("POOL_SIZE") || "5") + # The secret key base is used to sign/encrypt cookies and other secrets. # A default value is used in config/dev.exs and config/test.exs but you # want to use a different value for prod and you most likely don't want diff --git a/config/test.exs b/config/test.exs index 6c5d3451..a01e018a 100644 --- a/config/test.exs +++ b/config/test.exs @@ -11,6 +11,11 @@ config :music_library, MusicLibrary.Repo, pool_size: 32, pool: Ecto.Adapters.SQL.Sandbox +config :music_library, MusicLibrary.ErrorRepo, + database: Path.expand("../data/music_library_errors_test.db", __DIR__), + pool_size: 32, + pool: Ecto.Adapters.SQL.Sandbox + # We don't run a server during test. If one is required, # you can enable the server option below. config :music_library, MusicLibraryWeb.Endpoint, diff --git a/lib/music_library/application.ex b/lib/music_library/application.ex index 49eefd03..6131a961 100644 --- a/lib/music_library/application.ex +++ b/lib/music_library/application.ex @@ -10,6 +10,7 @@ defmodule MusicLibrary.Application do children = [ MusicLibraryWeb.Telemetry, MusicLibrary.Repo, + MusicLibrary.ErrorRepo, {Ecto.Migrator, repos: Application.fetch_env!(:music_library, :ecto_repos), skip: skip_migrations?()}, {DNSCluster, query: Application.get_env(:music_library, :dns_cluster_query) || :ignore}, diff --git a/lib/music_library/error_repo.ex b/lib/music_library/error_repo.ex new file mode 100644 index 00000000..a950af95 --- /dev/null +++ b/lib/music_library/error_repo.ex @@ -0,0 +1,5 @@ +defmodule MusicLibrary.ErrorRepo do + use Ecto.Repo, + otp_app: :music_library, + adapter: Ecto.Adapters.SQLite3 +end diff --git a/priv/repo/migrations/20250101170610_add_error_tracker.exs b/priv/error_repo/migrations/20250101170610_add_error_tracker.exs similarity index 50% rename from priv/repo/migrations/20250101170610_add_error_tracker.exs rename to priv/error_repo/migrations/20250101170610_add_error_tracker.exs index a63a31f9..fb935415 100644 --- a/priv/repo/migrations/20250101170610_add_error_tracker.exs +++ b/priv/error_repo/migrations/20250101170610_add_error_tracker.exs @@ -2,6 +2,5 @@ defmodule MusicLibrary.Repo.Migrations.AddErrorTracker do use Ecto.Migration def up, do: ErrorTracker.Migration.up(version: 4) - # We specify `version: 1` in `down`, to ensure we remove all migrations. - def down, do: ErrorTracker.Migration.down(version: 1) + def down, do: ErrorTracker.Migration.down(version: 2) end