Skip to content

Commit

Permalink
Add default activity log retention time at app startup and seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonkopliku committed Jun 14, 2024
1 parent b193a82 commit 4835dd2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/trento/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Trento.Release do
"""

alias Pow.Ecto.Schema.Password
alias Trento.ActivityLog.Settings, as: ActivityLogSettings
alias Trento.Settings.ApiKeySettings

@app :trento
Expand All @@ -15,6 +16,7 @@ defmodule Trento.Release do
migrate_event_store()
init_admin_user()
init_default_api_key()
init_default_activity_log_retention_time()
end

def migrate do
Expand Down Expand Up @@ -101,6 +103,17 @@ defmodule Trento.Release do
end
end

def init_default_activity_log_retention_time do
load_app()
Enum.each([:postgrex, :ecto], &Application.ensure_all_started/1)
Trento.Repo.start_link()

Trento.Repo.insert!(ActivityLogSettings.with_default_retention_time(),
on_conflict: :nothing,
conflict_target: :type
)
end

defp repos do
Application.fetch_env!(@app, :ecto_repos)
end
Expand Down
4 changes: 4 additions & 0 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
created_at: DateTime.utc_now()
})
|> Trento.Repo.insert!(on_conflict: :nothing)

Trento.Repo.insert!(Trento.ActivityLog.Settings.with_default_retention_time(),
on_conflict: :nothing
)
46 changes: 46 additions & 0 deletions test/trento/release_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule Trento.ReleaseTest do
@moduledoc false
use ExUnit.Case
use Trento.DataCase

import Trento.Factory

require Trento.ActivityLog.RetentionPeriodUnit, as: RetentionPeriodUnit

alias Trento.ActivityLog.RetentionTime
alias Trento.ActivityLog.Settings, as: ActivityLogSettings

describe "Activity Log settings initiation" do
test "should init default activity log retention time" do
Trento.Release.init_default_activity_log_retention_time()

assert %ActivityLogSettings{
retention_time: %RetentionTime{
retention_period: 1,
retention_period_unit: RetentionPeriodUnit.months()
}
} = Trento.Repo.one(ActivityLogSettings.base_query())
end

test "should not change previously saved retention time" do
retention_period = 3
retention_period_unit = RetentionPeriodUnit.weeks()

insert(:activity_log_settings,
retention_time: %{
retention_period: retention_period,
retention_period_unit: retention_period_unit
}
)

Trento.Release.init_default_activity_log_retention_time()

assert %ActivityLogSettings{
retention_time: %RetentionTime{
retention_period: ^retention_period,
retention_period_unit: ^retention_period_unit
}
} = Trento.Repo.one(ActivityLogSettings.base_query())
end
end
end

0 comments on commit 4835dd2

Please sign in to comment.