-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save operations in the database (#547)
* Create operation migration and schema * Implement operation functions * Store operation progress in the database * Adapt tests to initially pass * Add and update tests to avoid implementation details usage * Use operation enums * Use enums in test factories * Add required fields validation in operation schema * Restore updated_at field * Use missing enums properly * Use jsonb specifially as operation schema type * Add new database indexes for jsonb fields * Update index migration using ecto and adding jsonb_path_ops
- Loading branch information
Showing
12 changed files
with
663 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Wanda.Operations.Enums.Status do | ||
@moduledoc """ | ||
Type that represents an operation execution status. | ||
""" | ||
|
||
use Wanda.Support.Enum, | ||
values: [:running, :completed] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Wanda.Operations.Operation do | ||
@moduledoc """ | ||
Schema of a persisted operation. | ||
""" | ||
|
||
use Ecto.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
require Wanda.Operations.Enums.Result, as: Result | ||
require Wanda.Operations.Enums.Status, as: Status | ||
|
||
@type t :: %__MODULE__{} | ||
|
||
@fields ~w(operation_id group_id result status agent_reports started_at updated_at completed_at)a | ||
@target_fields ~w(agent_id arguments)a | ||
|
||
@required_fields ~w(operation_id group_id result status)a | ||
@targets_required_fields ~w(agent_id)a | ||
|
||
@derive {Jason.Encoder, [except: [:__meta__]]} | ||
@primary_key false | ||
schema "operations" do | ||
field :operation_id, Ecto.UUID, primary_key: true | ||
field :group_id, Ecto.UUID | ||
field :result, Ecto.Enum, values: Result.values() | ||
field :status, Ecto.Enum, values: Status.values() | ||
|
||
embeds_many :targets, Target, primary_key: false do | ||
@derive Jason.Encoder | ||
|
||
field :agent_id, Ecto.UUID, primary_key: true | ||
field :arguments, :map | ||
end | ||
|
||
field :agent_reports, {:array, :map} | ||
|
||
field :completed_at, :utc_datetime_usec | ||
timestamps(type: :utc_datetime_usec, inserted_at: :started_at) | ||
end | ||
|
||
@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t() | ||
def changeset(operation, params) do | ||
operation | ||
|> cast(params, @fields) | ||
|> cast_embed(:targets, with: &target_changeset/2, required: true) | ||
|> validate_required(@required_fields) | ||
end | ||
|
||
defp target_changeset(target, params) do | ||
target | ||
|> cast(params, @target_fields) | ||
|> validate_required(@targets_required_fields) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule Wanda.Repo.Migrations.AddOperation do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:operations, primary_key: false) do | ||
add :operation_id, :uuid, primary_key: true | ||
add :group_id, :uuid, null: false | ||
add :result, :string, null: false | ||
add :status, :string, null: false | ||
add :targets, :jsonb, null: false, default: "[]" | ||
add :agent_reports, :jsonb, null: false, default: "[]" | ||
add :completed_at, :utc_datetime_usec | ||
timestamps(type: :utc_datetime_usec, inserted_at: :started_at) | ||
end | ||
|
||
create index(:operations, [:group_id]) | ||
create unique_index(:operations, [:operation_id, :group_id]) | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
priv/repo/migrations/20250116095113_add_operation_jsonb_indexes.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Wanda.Repo.Migrations.AddOperationJsonbIndexes do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create index(:operations, ["targets jsonb_path_ops"], using: "GIN") | ||
create index(:operations, ["agent_reports jsonb_path_ops"], using: "GIN") | ||
end | ||
end |
Oops, something went wrong.