Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: use exandra #71

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"test/**/*.{ex,exs}",
"mix.exs"
],
import_deps: [:skogsra]
import_deps: [:skogsra, :ecto]
]
21 changes: 3 additions & 18 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2017 Ispirata Srl
# Copyright 2017 - 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -20,23 +20,8 @@
# and its dependencies with the aid of the Mix.Config module.
import Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.
config :astarte_data_access, ecto_repos: [Astarte.DataAccess.Repo]

# You can configure your application as:
#
# config :astarte_data_access, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:astarte_data_access, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#
config :astarte_data_access, Astarte.DataAccess.Repo, sync_connect: 5000

import_config "#{config_env()}.exs"
11 changes: 10 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import Config

cassandra_host = System.get_env("CASSANDRA_DB_HOST", "cassandra")

cassandra_port =
System.get_env("CASSANDRA_DB_PORT", "9042")
|> String.to_integer()

config :cqerl,
cassandra_nodes: [{System.get_env("CASSANDRA_DB_HOST") || "cassandra", System.get_env("CASSANDRA_DB_PORT") || 9042}]
cassandra_nodes: [{cassandra_host, cassandra_port}]

config :astarte_data_access,
xandra_nodes: "#{cassandra_host}:#{cassandra_port}"
10 changes: 3 additions & 7 deletions lib/astarte_data_access.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Astarte.
#
# Copyright 2023 SECO Mind Srl
# Copyright 2023 - 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -26,14 +26,10 @@ defmodule Astarte.DataAccess do

@impl true
def init(init_arg) do
xandra_options =
Keyword.fetch!(init_arg, :xandra_options)
|> Keyword.put(:name, :astarte_data_access_xandra)
# TODO move to string keys
|> Keyword.put(:atom_keys, true)
xandra_options = Keyword.fetch!(init_arg, :xandra_options)

children = [
{Xandra.Cluster, xandra_options}
{Astarte.DataAccess.Repo, xandra_options}
]

opts = [strategy: :one_for_one, name: Astarte.DataAccess.Supervisor]
Expand Down
28 changes: 28 additions & 0 deletions lib/astarte_data_access/astarte/kv_store.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.DataAccess.Astarte.KvStore do
use TypedEctoSchema

@primary_key false
typed_schema "kv_store" do
field :group, :string, primary_key: true
field :key, :string, primary_key: true
field :value, :binary
end
end
33 changes: 33 additions & 0 deletions lib/astarte_data_access/astarte/realm.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.DataAccess.Astarte.Realm do
use TypedEctoSchema

alias Astarte.Core.CQLUtils
alias Astarte.DataAccess.Config

@primary_key {:realm_name, :string, autogenerate: false}
typed_schema "realms" do
end

@spec keyspace_name(String.t()) :: String.t()
def keyspace_name(realm_name) do
CQLUtils.realm_name_to_keyspace_name(realm_name, Config.astarte_instance_id!())
end
end
5 changes: 2 additions & 3 deletions lib/astarte_data_access/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule Astarte.DataAccess.Config do
@envdoc """
Discover nodes in the same cluster as specified in CASSANDRA_NODES. If your Cassandra
instance is outside of your network, enabling the autodiscovery leads to connection failures.
Defaults to false.
This option is deprecated and will be ignored.
"""
app_env :autodiscovery_enabled, :astarte_data_access, :autodiscovery_enabled,
os_env: "CASSANDRA_AUTODISCOVERY_ENABLED",
Expand Down Expand Up @@ -171,8 +171,7 @@ defmodule Astarte.DataAccess.Config do
nodes: xandra_nodes!(),
authentication: xandra_authentication_options!(),
pool_size: pool_size!(),
encryption: ssl_enabled!(),
autodiscovery: autodiscovery_enabled!()
encryption: ssl_enabled!()
]
|> populate_xandra_ssl_options()
end
Expand Down
Loading
Loading