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

replacing ecto/postgres for erlang mnesia database #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ erl_crash.dump

*.secret.exs
mix.lock

# all the Mnesia databases
Mnesia*
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
# Largo

**TODO: Add description**
Largo is a Slack Integration done in Elixir to store key/values in your Slack chat.

## Dependencies
You need to install Erlang and Elixir, we strongly recommend:
http://elixir-lang.org/install.html

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `largo` to your list of dependencies in `mix.exs`:
1. Install the dependencies:
`mix deps.get`

2. Copy your Slack API-KEY token in `api_token` under `config/dev.secret.exs`

3. Create the Mnesia database
`mix amnesia.create -d Database --disk`

```elixir
def deps do
[{:largo, "~> 0.1.0"}]
end
```
4. Run the app!
`iex -S mix`

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/largo](https://hexdocs.pm/largo).
## Uses
You can tag @largo bot in your Slack chat or just chat directly with it.

- @largo guarda foo=bar
- @largo trae foo
- @largo guarda hora reunion=10:00 am
- @largo trae todo
- @largo borra foo
- @largo trae todo
10 changes: 0 additions & 10 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

config :largo, Largo.Repo,
adapter: Ecto.Adapters.Postgres,
database: "largo",
username: "postgres",
password: "postgres",
hostname: "localhost",
port: 15432

config :largo, ecto_repos: [Database.Repo]

config :largo, default_message_parameters: %{
as_user: true
}
Expand Down
31 changes: 31 additions & 0 deletions lib/database.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use Amnesia

defdatabase Database do
deftable Value

deftable Value, [{ :id, autoincrement }, :key, :value, :last_updater], type: :ordered_set, index: [:key] do
@type t :: %Value{id: non_neg_integer, key: String.t, value: String.t, last_updater: String.t}

# this is a helper function to add a message to the user, using write
# on the created records makes it write to the mnesia table
# def add_value(self, content) do
# %Message{user_id: self.id, content: content} |> Message.write
# end

# # like above, but again with dirty operations, the bang methods are used
# # thorough amnesia to be the dirty counterparts of the bang-less functions
# def add_message!(self, content) do
# %Message{user_id: self.id, content: content} |> Message.write!
# end

# # this is a helper to fetch all messages for the user
# def messages(self) do
# Message.read(self.id)
# end

# # like above, but with dirty operations
# def messages!(self) do
# Message.read!(self.id)
# end
end
end
2 changes: 1 addition & 1 deletion lib/largo/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Largo.Application do
import Supervisor.Spec, warn: false

# Define workers and child supervisors to be supervised
default_workers = [ supervisor(Largo.Repo, [])]
default_workers = []

slack_workers = if Application.get_env(:largo, :enable_ws_client, true) do
[worker(Slack.Bot, [Largo, [], Application.get_env(:slack, :api_token)])]
Expand Down
3 changes: 0 additions & 3 deletions lib/largo/repo.ex

This file was deleted.

55 changes: 31 additions & 24 deletions lib/largo/responders/memmory_responder.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Largo.Responders.MemmoryResponder do
@behaviour Largo.Responders.Responder
alias Largo.{Value, Repo}
import Ecto.Query
use Database
use Amnesia
alias Amnesia.Selection

@matchers [
{~r/Guard[aá]\s+(?<key>.+)[=:](?<value>.+)$/iu, :save},
Expand Down Expand Up @@ -35,42 +36,48 @@ defmodule Largo.Responders.MemmoryResponder do

defp get(key) do
key = clean_key(key)
case Ecto.Query.from(v in Value, where: v.key == ^key) |> Repo.one do
nil -> {:ok, build_colored_message("No encontré la clave #{key}", "warn")}
value -> {:ok, build_kv_message([{value.key, value.value}], "hmmmmmmm", "good")}
Amnesia.transaction do
case Value.read_at(key, :key) do
nil -> {:ok, build_colored_message("No encontré la clave #{key}", "warn")}
values -> {:ok, build_kv_message([{hd(values).key, hd(values).value}], "hmmmmmmm", "good")}
end
end
end

defp delete(key) do
key = clean_key(key)
case Ecto.Query.from(v in Value, where: v.key == ^key) |> Repo.one do
nil -> {:ok, build_colored_message("No encontré la clave #{key}", "warning")}
value ->
case Repo.delete(value) do
{:error, _} -> {:ok, build_colored_message("No pude borrar la clave #{key}", "danger")}
{:ok, value} -> {:ok, build_colored_message("Borré la clave #{value.key}. Último valor: #{value.value}", "good")}
Amnesia.transaction do
case Value.read_at(key, :key) do
nil -> {:ok, build_colored_message("No encontré la clave #{key}", "warning")}
values ->
case Value.delete(hd(values)) do
:error -> {:ok, build_colored_message("No pude borrar la clave #{key}", "danger")}
:ok -> {:ok, build_colored_message("Borré la clave #{hd(values).key}. Último valor: #{hd(values).value}", "good")}
end
end
end
end

defp list do
msg = Repo.all(Value)
|> Enum.map(&{&1.key, &1.value})
|> build_kv_message("*hmmmmm*", "good")
{:ok, msg}
Amnesia.transaction do
msg = Value.where(1 == 1) |> Amnesia.Selection.values
|> Enum.map(&{&1.key, &1.value})
|> build_kv_message("*hmmmmm*", "good")
{:ok, msg}
end
end

defp save(key, value) do
key = clean_key(key)
case Repo.get_by(Value, key: key) do
nil ->
Value.changeset(%Value{}, %{key: key, value: String.trim(value)})
|> Repo.insert!
{:ok, build_colored_message("guardado...", "good")}
db_val ->
Value.changeset(db_val, %{value: String.trim(value)})
|> Repo.update!
{:ok, build_colored_message("actualizado...", "good")}
Amnesia.transaction do
case Value.read_at(key, :key) do
nil ->
%Value{key: key, value: String.trim(value)} |> Value.write
{:ok, build_colored_message("guardado...", "good")}
_ ->
%Value{value: String.trim(value)} |> Value.write
{:ok, build_colored_message("actualizado...", "good")}
end
end
end

Expand Down
20 changes: 0 additions & 20 deletions lib/largo/value.ex

This file was deleted.

3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ defmodule Largo.Mixfile do
#
# Type "mix help deps" for more examples and options
defp deps do
[{:ecto, "~> 2.0"},
{:postgrex, "~> 0.11"},
[{:amnesia, "~> 0.2.7"},
{:slack, "~> 0.11.0"},
{:poison, "~> 3.0"}]
end
Expand Down
16 changes: 0 additions & 16 deletions priv/repo/migrations/20170326200900_create_key_values.exs

This file was deleted.