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..2ca19813 --- /dev/null +++ b/priv/repo/migrations/20250123102550_create_check_customizations.exs @@ -0,0 +1,18 @@ +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 + + create index(:check_customizations, [:check_id]) + create index(:check_customizations, [:group_id]) + create unique_index(:check_customizations, [:check_id, :group_id]) + end +end