From a8f8900f1c62ec4d0fc6b749fccb083186b35e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Rodrigues?= Date: Sun, 13 Aug 2023 18:15:52 +0100 Subject: [PATCH] Create sessions minimum and max entries constraint --- lib/atomic/activities/session.ex | 1 + priv/repo/migrations/20221023200102_create_sessions.exs | 4 ++++ test/support/fixtures/activities_fixtures.ex | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/atomic/activities/session.ex b/lib/atomic/activities/session.ex index 39a957822..f62875764 100644 --- a/lib/atomic/activities/session.ex +++ b/lib/atomic/activities/session.ex @@ -48,6 +48,7 @@ defmodule Atomic.Activities.Session do |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) |> validate_location() + |> check_constraint(:minimum_entries, name: :minimum_entries_lower_than_maximum_entries) |> maybe_mark_for_deletion() |> maybe_put_departments(attrs) |> maybe_put_speakers(attrs) diff --git a/priv/repo/migrations/20221023200102_create_sessions.exs b/priv/repo/migrations/20221023200102_create_sessions.exs index d189ddf8d..877b59bdd 100644 --- a/priv/repo/migrations/20221023200102_create_sessions.exs +++ b/priv/repo/migrations/20221023200102_create_sessions.exs @@ -18,5 +18,9 @@ defmodule Atomic.Repo.Migrations.CreateSessions do end create index(:sessions, [:activity_id]) + + create constraint(:sessions, :minimum_entries_lower_than_maximum_entries, + check: "minimum_entries < maximum_entries" + ) end end diff --git a/test/support/fixtures/activities_fixtures.ex b/test/support/fixtures/activities_fixtures.ex index 946462121..350eb5c3a 100644 --- a/test/support/fixtures/activities_fixtures.ex +++ b/test/support/fixtures/activities_fixtures.ex @@ -29,7 +29,7 @@ defmodule Atomic.ActivitiesFixtures do attrs |> Enum.into(%{ maximum_entries: 42, - minimum_entries: 42, + minimum_entries: 0, finish: ~N[2022-10-22 20:00:00], start: ~N[2022-10-22 20:00:00] })