Skip to content

Commit 3542923

Browse files
lawiknshoes
andauthored
Match deployments to devices with nil tags (#2244)
Co-authored-by: Nate Shoemaker <[email protected]>
1 parent edefdbc commit 3542923

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

lib/nerves_hub/devices/device.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ defmodule NervesHub.Devices.Device do
9595
|> cast_embed(:firmware_metadata)
9696
|> cast_embed(:extensions)
9797
|> validate_required(@required_params)
98-
|> validate_length(:tags, min: 1)
9998
|> unique_constraint(:identifier)
10099
|> then(fn changeset ->
101100
if device.deleted_at && !Map.has_key?(changeset.changes, :deleted_at) do

lib/nerves_hub/managed_deployments.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,16 @@ defmodule NervesHub.ManagedDeployments do
506506
|> ignore_same_deployment_group(device)
507507
|> where([d, firmware: f], f.platform == ^device.firmware_metadata.platform)
508508
|> where([d, firmware: f], f.architecture == ^device.firmware_metadata.architecture)
509-
|> where([d], fragment("?->'tags' <@ to_jsonb(?::text[])", d.conditions, ^device.tags))
509+
|> where(
510+
[d],
511+
fragment(
512+
"?->'tags' <@ to_jsonb(?::text[]) OR (jsonb_array_length(?->'tags') = 0 and ?::text[] is null)",
513+
d.conditions,
514+
^device.tags,
515+
d.conditions,
516+
^device.tags
517+
)
518+
)
510519
|> Repo.all()
511520
|> Enum.filter(&version_match?(device, &1))
512521
|> Enum.sort_by(

test/nerves_hub/managed_deployments_test.exs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ defmodule NervesHub.ManagedDeploymentsTest do
248248
end
249249
end
250250

251-
describe "device's matching deployments" do
251+
describe "devices matching deployments" do
252252
test "finds all matching deployments", state do
253253
%{org: org, product: product, firmware: firmware} = state
254254

@@ -277,6 +277,58 @@ defmodule NervesHub.ManagedDeploymentsTest do
277277
] = ManagedDeployments.matching_deployment_groups(device)
278278
end
279279

280+
test "finds matching deployment with no tag condition", state do
281+
%{org: org, product: product, firmware: firmware} = state
282+
283+
%{id: blank_deployment_group_id} =
284+
Fixtures.deployment_group_fixture(org, firmware, %{
285+
name: "beta",
286+
conditions: %{"tags" => [], "version" => ""}
287+
})
288+
289+
Fixtures.deployment_group_fixture(org, firmware, %{
290+
name: "rpi",
291+
conditions: %{"tags" => ["rpi"], "version" => ""}
292+
})
293+
294+
Fixtures.deployment_group_fixture(org, firmware, %{
295+
name: "rpi0",
296+
conditions: %{"tags" => ["rpi0"], "version" => ""}
297+
})
298+
299+
%{tags: []} = device = Fixtures.device_fixture(org, product, firmware, %{tags: []})
300+
301+
assert [
302+
%{id: ^blank_deployment_group_id}
303+
] = ManagedDeployments.matching_deployment_groups(device)
304+
end
305+
306+
test "finds matching deployment when device tags is null", state do
307+
%{org: org, product: product, firmware: firmware} = state
308+
309+
%{id: blank_deployment_group_id} =
310+
Fixtures.deployment_group_fixture(org, firmware, %{
311+
name: "beta",
312+
conditions: %{"tags" => [], "version" => ""}
313+
})
314+
315+
Fixtures.deployment_group_fixture(org, firmware, %{
316+
name: "rpi",
317+
conditions: %{"tags" => ["rpi"], "version" => ""}
318+
})
319+
320+
Fixtures.deployment_group_fixture(org, firmware, %{
321+
name: "rpi0",
322+
conditions: %{"tags" => ["rpi0"], "version" => ""}
323+
})
324+
325+
%{tags: nil} = device = Fixtures.device_fixture(org, product, firmware, %{tags: nil})
326+
327+
assert [
328+
%{id: ^blank_deployment_group_id}
329+
] = ManagedDeployments.matching_deployment_groups(device)
330+
end
331+
280332
test "finds matching deployments including the platform", state do
281333
%{org: org, org_key: org_key, product: product} = state
282334

0 commit comments

Comments
 (0)