Skip to content

Commit 5105e03

Browse files
committed
feat(storage): add V2 migration with index adjustments
Drops indexes that duplicate primary keys on `occurrences` and `snapshots` (audit #10), adds composite `workitems(enactment_id, state)` covering the `list_live_workitems` query (audit #4), and adds indexes for two FK columns that were missing them: `enactments(flow_id)` (audit #8) and `workitem_logs(workitem_id)` (audit #9). The single-column `workitems(state)` index is preserved because `WorkitemStream.live_query/1` issues a bare `state in [...]` query without an `enactment_id` filter, which the composite cannot serve as a left prefix.
1 parent 004a5d3 commit 5105e03

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

  • lib/coloured_flow/runner/storage/migrations
  • test/support
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule ColouredFlow.Runner.Migrations.V2 do
2+
@moduledoc false
3+
4+
use Ecto.Migration
5+
6+
@prefix "coloured_flow"
7+
8+
@spec change(prefix: String.t()) :: :ok
9+
def change(opts \\ []) do
10+
prefix = Keyword.get(opts, :prefix, @prefix)
11+
index_options = [prefix: prefix]
12+
13+
# Drop indexes that duplicate composite primary keys.
14+
# `occurrences` PK is `(enactment_id, step_number)` and `snapshots` PK is
15+
# `enactment_id`; both PKs already provide the same coverage.
16+
drop index("occurrences", [:enactment_id, :step_number], index_options)
17+
drop index("snapshots", [:enactment_id], index_options)
18+
19+
# Add a composite `(enactment_id, state)` index covering the
20+
# `list_live_workitems` query in
21+
# `ColouredFlow.Runner.Storage.Default.list_live_workitems/1`. The existing
22+
# single-column `workitems(state)` index is preserved because
23+
# `ColouredFlow.Runner.Worklist.WorkitemStream.live_query/1` filters by
24+
# `state in @live_states` without an `enactment_id` filter, which the
25+
# composite cannot serve as a left prefix.
26+
create index("workitems", [:enactment_id, :state], index_options)
27+
28+
# Cover queries that look up enactments by their parent flow, and support
29+
# the foreign key reference defined in V0.
30+
create index("enactments", [:flow_id], index_options)
31+
32+
# Speed up the `ON DELETE CASCADE` from `workitems` and any future lookup
33+
# by `workitem_id`. The foreign key is defined in V1.
34+
create index("workitem_logs", [:workitem_id], index_options)
35+
36+
:ok
37+
end
38+
end

test/support/repo.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ defmodule ColouredFlow.TestRepo do
99
__MODULE__,
1010
[
1111
{0, ColouredFlow.Runner.Migrations.V0},
12-
{1, ColouredFlow.Runner.Migrations.V1}
12+
{1, ColouredFlow.Runner.Migrations.V1},
13+
{2, ColouredFlow.Runner.Migrations.V2}
1314
],
1415
:up,
1516
all: true

0 commit comments

Comments
 (0)