From b6706aaafeb1ec8121dbc51148dcccfe73149a50 Mon Sep 17 00:00:00 2001 From: Nelson Kopliku Date: Thu, 23 Jan 2025 11:42:12 +0100 Subject: [PATCH] Add check customization schema and migration --- lib/wanda/catalog/check_customization.ex | 40 +++++++++++++++++++ ...0123102550_create_check_customizations.exs | 14 +++++++ 2 files changed, 54 insertions(+) create mode 100644 lib/wanda/catalog/check_customization.ex create mode 100644 priv/repo/migrations/20250123102550_create_check_customizations.exs diff --git a/lib/wanda/catalog/check_customization.ex b/lib/wanda/catalog/check_customization.ex new file mode 100644 index 00000000..66e412e3 --- /dev/null +++ b/lib/wanda/catalog/check_customization.ex @@ -0,0 +1,40 @@ +defmodule Wanda.Catalog.CheckCustomization do + @moduledoc """ + Schema representing Customizations applied for a Check in a specific execution group. + """ + use Ecto.Schema + import Ecto.Changeset + + @fields ~w(id check_id group_id inserted_at updated_at)a + @required_fields ~w(check_id group_id)a + + @custom_value_fields ~w(name value)a + + @primary_key false + schema "check_customizations" do + field :id, Ecto.UUID, primary_key: true + field :check_id, :string + field :group_id, Ecto.UUID + + embeds_many :custom_values, CustomValue, primary_key: false do + field :name, :string + field :value, :string + end + + timestamps(type: :utc_datetime_usec) + end + + + def changeset(check_customization, attrs) do + check_customization + |> cast(attrs, @fields) + |> validate_required(@required_fields) + |> cast_embed(:custom_values, with: &custom_value_changeset/2, required: true) + end + + defp custom_value_changeset(custom_value, attrs) do + custom_value + |> cast(attrs, @custom_value_fields) + |> validate_required(@custom_value_fields) + end +end diff --git a/priv/repo/migrations/20250123102550_create_check_customizations.exs b/priv/repo/migrations/20250123102550_create_check_customizations.exs new file mode 100644 index 00000000..13472224 --- /dev/null +++ b/priv/repo/migrations/20250123102550_create_check_customizations.exs @@ -0,0 +1,14 @@ +defmodule Wanda.Repo.Migrations.CreateCheckCustomizations do + use Ecto.Migration + + def change do + create table(:check_customizations, primary_key: false) do + add :id, :uuid, primary_key: true + add :check_id, :string, null: false + add :group_id, :uuid, null: false + add :custom_values, :jsonb, null: false, default: "[]" + + timestamps() + end + end +end