-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from upmaru/feature/ops-576-state-machine-for-v…
…ersions Create state machine for version
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Polar.Streams.Version.Event do | ||
alias Polar.Streams.Version | ||
alias Polar.Accounts.User | ||
|
||
use Eventful, | ||
parent: {:version, Version}, | ||
actor: {:user, User} | ||
|
||
alias Version.Transitions | ||
|
||
handle(:transitions, using: Transitions) | ||
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,17 @@ | ||
defimpl Eventful.Transit, for: Polar.Streams.Version do | ||
alias Polar.Streams.Version.Event | ||
|
||
def perform(version, user, event_name, options \\ []) do | ||
comment = Keyword.get(options, :comment) | ||
domain = Keyword.get(options, :domain, "transitions") | ||
parameters = Keyword.get(options, :parameters, %{}) | ||
|
||
version | ||
|> Event.handle(user, %{ | ||
domain: domain, | ||
name: event_name, | ||
comment: comment, | ||
parameters: parameters | ||
}) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Polar.Streams.Version.Transitions do | ||
@behaviour Eventful.Handler | ||
use Eventful.Transition, repo: Polar.Repo | ||
|
||
alias Polar.Streams.Version | ||
|
||
Version | ||
|> transition( | ||
[from: "active", to: "inactive", via: "deactivate"], | ||
fn changes -> transit(changes) end | ||
) | ||
end |
28 changes: 28 additions & 0 deletions
28
priv/repo/migrations/20240226072127_create_version_events.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,28 @@ | ||
defmodule Polar.Repo.Migrations.CreateVersionEvents do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:version_events) do | ||
add(:name, :citext, null: false) | ||
add(:domain, :citext, null: false) | ||
add(:metadata, :map, default: "{}") | ||
|
||
add( | ||
:version_id, | ||
references(:versions, on_delete: :restrict), | ||
null: false | ||
) | ||
|
||
add( | ||
:user_id, | ||
references(:users, on_delete: :restrict), | ||
null: false | ||
) | ||
|
||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
create index(:version_events, [:version_id]) | ||
create index(:version_events, [:user_id]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Polar.Streams.Version.TransitionsTest do | ||
use Polar.DataCase, async: true | ||
|
||
alias Polar.Streams | ||
|
||
import Polar.AccountsFixtures | ||
|
||
setup do | ||
user = user_fixture() | ||
|
||
{:ok, product} = | ||
Streams.create_product(%{ | ||
aliases: ["alpine/3.19", "alpine/3.19/default"], | ||
arch: "amd64", | ||
os: "Alpine", | ||
release: "3.19", | ||
release_title: "3.19", | ||
variant: "default", | ||
requirements: %{ | ||
secureboot: false | ||
} | ||
}) | ||
|
||
{:ok, version} = | ||
Streams.create_version(product, %{ | ||
serial: "20240209_13:00", | ||
items: [ | ||
%{ | ||
name: "lxd.tar.gz", | ||
file_type: "lxd.tar.gz", | ||
hash: "35363f3d086271ed5402d61ab18ec03987bed51758c00079b8c9d372ff6d62dd", | ||
size: 876, | ||
path: "images/alpine/edge/amd64/default/20240209_13:00/incus.tar.xz" | ||
}, | ||
%{ | ||
name: "root.squashfs", | ||
file_type: "squashfs", | ||
hash: "47cc4070da1bf17d8364c390…3603f4ed7e9e46582e690d2", | ||
size: 2_982_800, | ||
path: "images/alpine/edge/amd64/default/20240209_13:00/rootfs.tar.xz" | ||
} | ||
] | ||
}) | ||
|
||
{:ok, user: user, version: version} | ||
end | ||
|
||
describe "deactivate" do | ||
test "can deactivate active version", %{version: version, user: user} do | ||
assert {:ok, %{resource: inactive_version}} = | ||
Eventful.Transit.perform(version, user, "deactivate") | ||
|
||
assert inactive_version.current_state == "inactive" | ||
end | ||
end | ||
end |