Skip to content

Commit 34db494

Browse files
committed
fix(rm): faulty checks for existing database resources
There were some faulty checks for existing database resources, due to the used `Repo.some?` custom function which now returns a `{:ok, bool}` tuple instead of directly a boolean, as in previous iterations. Indeed, the tuple was either checked in `if` statements where it always computed to a trueish value, of it was pattern matched to boolean values which resulted in an match error and a crash. The latter one is also the case of a bug in the DeviceRemover task, which crashed just after launch and resulted in devices to be stuck in the "in deletion" status. Fixes astarte-platform#1493. Signed-off-by: Davide Briani <davide.briani@secomind.com>
1 parent 3317e9c commit 34db494

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
8+
## [1.2.1] - Unreleased
9+
### Fixed
10+
- [astarte_realm_management] Faulty checks for existing database resources. This fix also addresses a bug where devices got stuck in the "in deletion" status: [#1493](https://github.com/astarte-platform/astarte/issues/1493).
11+
712
## [1.2.1-rc.0] - 2025-08-26
813
### Added
914
- New environment variables to control how clustering work, needed on AppEngine and DUP.

apps/astarte_realm_management/lib/astarte_realm_management/device_removal/core.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ defmodule Astarte.RealmManagement.DeviceRemoval.Core do
3636
end
3737

3838
defp retrieve_individual_datastreams_keys!(realm_name, device_id) do
39-
if Queries.table_exist?(realm_name, "individual_datastreams") do
40-
Queries.retrieve_individual_datastreams_keys!(realm_name, device_id)
41-
else
42-
[]
39+
case Queries.table_exist?(realm_name, "individual_datastreams") do
40+
{:ok, true} -> Queries.retrieve_individual_datastreams_keys!(realm_name, device_id)
41+
{:ok, false} -> []
4342
end
4443
end
4544

@@ -69,10 +68,9 @@ defmodule Astarte.RealmManagement.DeviceRemoval.Core do
6968
end
7069

7170
defp retrieve_individual_properties_keys!(realm_name, device_id) do
72-
if Queries.table_exist?(realm_name, "individual_properties") do
73-
Queries.retrieve_individual_properties_keys!(realm_name, device_id)
74-
else
75-
[]
71+
case Queries.table_exist?(realm_name, "individual_properties") do
72+
{:ok, true} -> Queries.retrieve_individual_properties_keys!(realm_name, device_id)
73+
{:ok, false} -> []
7674
end
7775
end
7876

apps/astarte_realm_management/lib/astarte_realm_management/device_removal/device_remover.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ defmodule Astarte.RealmManagement.DeviceRemoval.DeviceRemover do
3333
@spec run(%{:device_id => <<_::128>>, :realm_name => binary()}) :: :ok | no_return()
3434
def run(%{realm_name: realm_name, device_id: device_id} = args) do
3535
case Queries.check_device_exists(realm_name, device_id) do
36-
true -> do_run(args)
37-
false -> cleanup(args)
36+
{:ok, true} -> do_run(args)
37+
{:ok, false} -> cleanup(args)
3838
end
3939
end
4040

apps/astarte_realm_management/test/astarte_realm_management/device_removal/core_test.exs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ defmodule Astarte.RealmManagement.DeviceRemover.CoreTest do
6767
end
6868
end
6969

70+
property "delete_individual_datastream/2 does not crash when individual datastream table is missing",
71+
%{realm: realm} do
72+
keyspace = Realm.keyspace_name(realm)
73+
74+
Ecto.Migrator.run(Repo, [{0, CreateDatastreamIndividualMultiInterface}], :up,
75+
prefix: keyspace,
76+
all: true
77+
)
78+
79+
Repo.query!("DROP TABLE #{keyspace}.individual_datastreams;")
80+
81+
check all(device_id <- Astarte.Core.Generators.Device.id()) do
82+
Core.delete_individual_datastreams!(realm, device_id)
83+
end
84+
end
85+
7086
property "delete_individual_properties/2 removes individual properties data of a valid device",
7187
%{realm: realm} do
7288
keyspace = Realm.keyspace_name(realm)
@@ -88,6 +104,17 @@ defmodule Astarte.RealmManagement.DeviceRemover.CoreTest do
88104
end
89105
end
90106

107+
property "delete_individual_properties/2 does not crash when individual properties table is missing",
108+
%{realm: realm} do
109+
keyspace = Realm.keyspace_name(realm)
110+
111+
Repo.query!("DROP TABLE #{keyspace}.individual_properties;")
112+
113+
check all(device_id <- Astarte.Core.Generators.Device.id()) do
114+
Core.delete_individual_properties!(realm, device_id)
115+
end
116+
end
117+
91118
property "delete_object_datastream/2 removes object datastreams of a valid device", %{
92119
realm: realm
93120
} do

0 commit comments

Comments
 (0)