Skip to content

Commit

Permalink
Add Keyspace integration
Browse files Browse the repository at this point in the history
Edit current implementation in order to add keyspace differentation from env param
Add new env param  to .schema file

Signed-off-by: Eddy Babetto <[email protected]>
  • Loading branch information
eddbbt committed Apr 17, 2024
1 parent a9e33b4 commit d7e1682
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/astarte_vmq_plugin/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ defmodule Astarte.VMQ.Plugin.Config do
Application.get_env(:astarte_vmq_plugin, :registry_mfa)
end

def astarte_instance_id do
Application.get_env(:astarte_vmq_plugin, :astarte_instance_id, "") |> to_string()
end

def device_heartbeat_interval_ms do
Application.get_env(
:astarte_vmq_plugin,
Expand Down
7 changes: 6 additions & 1 deletion lib/astarte_vmq_plugin/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ defmodule Astarte.VMQ.Plugin.Queries do

alias Astarte.Core.Device
alias Astarte.Core.Realm
alias Astarte.Core.CQLUtils
alias Astarte.VMQ.Plugin.Config

@doc """
Checks whether a device row exists in Astarte database (i.e. it has at least been registered).
Expand Down Expand Up @@ -130,8 +132,11 @@ defmodule Astarte.VMQ.Plugin.Queries do
end

defp use_realm(conn, realm) when is_binary(realm) do
keyspace_name =
CQLUtils.realm_name_to_keyspace_name(realm, Config.astarte_instance_id())

with :ok <- verify_realm(realm),
{:ok, %Xandra.SetKeyspace{}} <- Xandra.execute(conn, "USE #{realm}") do
{:ok, %Xandra.SetKeyspace{}} <- Xandra.execute(conn, "USE #{keyspace_name}") do
:ok
end
end
Expand Down
6 changes: 6 additions & 0 deletions priv/astarte_vmq_plugin.schema
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@
Splitted = string:split(Nodes, ",", all),
lists:map(fun(E) -> string:trim(E, both) end, Splitted)
end}.

{mapping, "astarte_vmq_plugin.astarte_instance_id", "astarte_vmq_plugin.astarte_instance_id", [
{default, ""},
{datatype, string}
]}.

20 changes: 12 additions & 8 deletions test/support/database_test_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,53 @@ defmodule Astarte.VMQ.Plugin.DatabaseTestHelper do
require Logger
alias Astarte.Core.Device
import ExUnit.Assertions
alias Astarte.Core.CQLUtils
alias Astarte.VMQ.Plugin.Config

@test_keyspace CQLUtils.realm_name_to_keyspace_name("test", Config.astarte_instance_id())

@create_test_keyspace """
CREATE KEYSPACE test
CREATE KEYSPACE #{@test_keyspace}
WITH
replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND
durable_writes = true;
"""

@create_devices_table """
CREATE TABLE test.devices (
CREATE TABLE #{@test_keyspace}.devices (
device_id uuid,
PRIMARY KEY (device_id)
);
"""

@create_deletion_in_progress_table """
CREATE TABLE test.deletion_in_progress (
CREATE TABLE #{@test_keyspace}.deletion_in_progress (
device_id uuid,
vmq_ack boolean,
PRIMARY KEY (device_id)
);
"""

@insert_device_into_devices """
INSERT INTO test.devices (device_id)
INSERT INTO #{@test_keyspace}.devices (device_id)
VALUES (:device_id);
"""

@insert_device_into_deletion_in_progress """
INSERT INTO test.deletion_in_progress (device_id, vmq_ack)
INSERT INTO #{@test_keyspace}.deletion_in_progress (device_id, vmq_ack)
VALUES (:device_id, :vmq_ack);
"""

@truncate_devices_table """
TRUNCATE test.devices;
TRUNCATE #{@test_keyspace}.devices;
"""

@truncate_deletion_in_progress_table """
TRUNCATE test.deletion_in_progress;
TRUNCATE #{@test_keyspace}.deletion_in_progress;
"""

@drop_test_keyspace """
DROP KEYSPACE test;
DROP KEYSPACE #{@test_keyspace};
"""

def setup_db!() do
Expand Down

0 comments on commit d7e1682

Please sign in to comment.