|
1
|
+ |
defmodule OpenAgents.Repo.Migrations.ReconcileForgeBuildsReceiptShape do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
Port fix: `forge_builds` was created (223c6f6, "Phase 3 target and receipt
|
|
4
|
+ |
persistence") with a shape that no module in this repo declares — a bigint
|
|
5
|
+ |
key plus `source_sha`/`module_changes`/`artifact_path`. The only schema
|
|
6
|
+ |
bound to that table is `OpenAgents.Forge.BuildReceipt`, which declares the
|
|
7
|
+ |
upstream receipt shape (`repo`, `sha`, `modules`, `warnings`, `tests`,
|
|
8
|
+ |
`artifact`, binary id).
|
|
9
|
+ |
|
|
10
|
+ |
The mismatch is not merely cosmetic: `OpenAgents.Changelog.receipt_index/1`
|
|
11
|
+ |
selects `forge_builds.repo`, so every query raised `UndefinedColumnError`,
|
|
12
|
+ |
and `Changelog.build/1`'s `safely/2` rescue swallowed it — dropping the
|
|
13
|
+ |
push, build **and** deploy receipts for the whole timeline. That is why a
|
|
14
|
+ |
receipted deploy never appeared as a `:receipt` row and no entry ever
|
|
15
|
+ |
carried `receipt_ids`.
|
|
16
|
+ |
|
|
17
|
+ |
Guarded and non-destructive: it acts only when the divergent shape is
|
|
18
|
+ |
present, and preserves any existing rows by renaming the old table aside
|
|
19
|
+ |
rather than dropping it.
|
|
20
|
+ |
"""
|
|
21
|
+ |
|
|
22
|
+ |
use Ecto.Migration
|
|
23
|
+ |
|
|
24
|
+ |
def up do
|
|
25
|
+ |
if divergent_shape?() do
|
|
26
|
+ |
rename(table(:forge_builds), to: table(:forge_builds_phase3_legacy))
|
|
27
|
+ |
|
|
28
|
+ |
create table(:forge_builds, primary_key: false) do
|
|
29
|
+ |
add :id, :binary_id, primary_key: true
|
|
30
|
+ |
add :repo, :string, null: false
|
|
31
|
+ |
add :sha, :string, null: false
|
|
32
|
+ |
add :target_id, :binary_id, null: false
|
|
33
|
+ |
add :modules, {:array, :string}, null: false, default: []
|
|
34
|
+ |
add :warnings, :text
|
|
35
|
+ |
add :tests, :text
|
|
36
|
+ |
add :duration_ms, :integer
|
|
37
|
+ |
add :artifact, :string
|
|
38
|
+ |
timestamps(type: :utc_datetime_usec, updated_at: false)
|
|
39
|
+ |
end
|
|
40
|
+ |
|
|
41
|
+ |
create unique_index(:forge_builds, [:repo, :sha, :target_id])
|
|
42
|
+ |
create index(:forge_builds, [:repo, :inserted_at])
|
|
43
|
+ |
end
|
|
44
|
+ |
end
|
|
45
|
+ |
|
|
46
|
+ |
def down do
|
|
47
|
+ |
if receipt_shape?() do
|
|
48
|
+ |
drop table(:forge_builds)
|
|
49
|
+ |
rename(table(:forge_builds_phase3_legacy), to: table(:forge_builds))
|
|
50
|
+ |
end
|
|
51
|
+ |
end
|
|
52
|
+ |
|
|
53
|
+ |
defp divergent_shape?, do: not has_column?("forge_builds", "repo")
|
|
54
|
+ |
|
|
55
|
+ |
defp receipt_shape?,
|
|
56
|
+ |
do: has_column?("forge_builds", "repo") and table?("forge_builds_phase3_legacy")
|
|
57
|
+ |
|
|
58
|
+ |
defp has_column?(table, column) do
|
|
59
|
+ |
query?("""
|
|
60
|
+ |
SELECT 1 FROM information_schema.columns
|
|
61
|
+ |
WHERE table_name = '#{table}' AND column_name = '#{column}'
|
|
62
|
+ |
""")
|
|
63
|
+ |
end
|
|
64
|
+ |
|
|
65
|
+ |
defp table?(table) do
|
|
66
|
+ |
query?("SELECT 1 FROM information_schema.tables WHERE table_name = '#{table}'")
|
|
67
|
+ |
end
|
|
68
|
+ |
|
|
69
|
+ |
defp query?(sql) do
|
|
70
|
+ |
case repo().query(sql) do
|
|
71
|
+ |
{:ok, %{num_rows: n}} -> n > 0
|
|
72
|
+ |
_ -> false
|
|
73
|
+ |
end
|
|
74
|
+ |
end
|
|
75
|
+ |
end
|