| 2 |
2
|
|
use Ecto.Migration
|
| 3 |
3
|
|
|
| 4 |
4
|
|
@moduledoc """
|
| 5 |
|
- |
`machine_pairings.user_id` was written by `do_approve_pairing/4` and read by
|
| 6 |
|
- |
nothing. It is also strictly derivable: approval sets `user_id` and
|
| 7 |
|
- |
`machine_id` in one changeset, nothing sets either alone, and
|
| 8 |
|
- |
`machines.user_id` is the same account. See issue #184 and CANON-002.
|
|
5
|
+ |
`machine_pairings.user_id` is no longer written or read, and the drop is
|
|
6
|
+ |
deferred to the release after the one that stops writing it.
|
|
7
|
+ |
|
|
8
|
+ |
This is the expand half of expand-and-contract, and the reason is the roll
|
|
9
|
+ |
rather than the column. A rolling replacement runs migrations on the first
|
|
10
|
+ |
node while the other two still serve the previous release, and that release
|
|
11
|
+ |
declares `belongs_to :user` on `OpenAgents.Machines.Pairing` — which puts
|
|
12
|
+ |
`user_id` into every generated `SELECT`. Dropping the column here would make
|
|
13
|
+ |
every read and write of `machine_pairings` fail on the un-replaced nodes, so
|
|
14
|
+ |
CLI device pairing would break for two thirds of traffic, then one third,
|
|
15
|
+ |
for the length of the roll.
|
|
16
|
+ |
|
|
17
|
+ |
The column is dead weight and nothing else: the current release neither
|
|
18
|
+ |
writes nor reads it, and the owner it named is reachable through
|
|
19
|
+ |
`machines.user_id` (issue #184, CANON-002). Carrying it for one release
|
|
20
|
+ |
costs a nullable column; dropping it during a roll costs pairing.
|
|
21
|
+ |
|
|
22
|
+ |
The contract half — the actual `remove` — belongs in a later migration, once
|
|
23
|
+ |
the release that stopped writing it is live on every node.
|
| 9 |
24
|
|
"""
|
| 10 |
25
|
|
|
| 11 |
26
|
|
def up do
|
| 12 |
|
- |
alter table(:machine_pairings) do
|
| 13 |
|
- |
remove :user_id
|
| 14 |
|
- |
end
|
|
27
|
+ |
:ok
|
| 15 |
28
|
|
end
|
| 16 |
29
|
|
|
| 17 |
30
|
|
def down do
|
| 18 |
|
- |
alter table(:machine_pairings) do
|
| 19 |
|
- |
add :user_id, references(:users, type: :binary_id, on_delete: :delete_all)
|
| 20 |
|
- |
end
|
| 21 |
|
- |
|
| 22 |
|
- |
# The owner was never lost: it is the computer's owner, which is where the
|
| 23 |
|
- |
# column's only writer read it from in the first place.
|
| 24 |
|
- |
execute("""
|
| 25 |
|
- |
UPDATE machine_pairings AS p
|
| 26 |
|
- |
SET user_id = m.user_id
|
| 27 |
|
- |
FROM machines AS m
|
| 28 |
|
- |
WHERE m.id = p.machine_id
|
| 29 |
|
- |
""")
|
|
31
|
+ |
:ok
|
| 30 |
32
|
|
end
|
| 31 |
33
|
|
end
|