• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mruoss / kompost / bf7b407df487178ba9b793a13ed30727a6359fb6

21 Apr 2024 05:01PM UTC coverage: 61.483% (+0.9%) from 60.579%
bf7b407df487178ba9b793a13ed30727a6359fb6

push

github

mruoss
chore(deps): update dependency bandit to ~> 1.5.0

340 of 553 relevant lines covered (61.48%)

7.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/kompost/kompo/postgres/instance.ex
1
defmodule Kompost.Kompo.Postgres.Instance do
2
  @moduledoc """
3
  Provides an interface to Postgrex. `connect/2` starts Postgrex under
4
  a dynamic supervisor (`Kompost.Kompo.Postgres.ConnectionSupervisor`) and
5
  register the process at the `Kompost.Kompo.Postgres.ConnectionRegistry`
6
  """
7

8
  alias Kompost.Kompo.Postgres.ConnectionRegistry
9
  alias Kompost.Kompo.Postgres.ConnectionSupervisor
10
  alias Kompost.Kompo.Postgres.Privileges
11

12
  alias Kompost.Tools.NamespaceAccess
13

14
  @typedoc """
15
  Defines the ID for an instance. The ID used as key when registering the
16
  Postgrex process at the `Kompost.Kompo.Postgres.ConnectionRegistry`
17
  """
18
  @type id :: {namespace :: binary() | :cluster, name :: binary()}
19

20
  @doc """
21
  Checks in the `Kompost.Kompo.Postgres.ConnectionRegistry` for an existing
22
  connection defined by the given `id`. If no such connection exists, a new
23
  `Postgrex` process is started, connecting to a Postgres instance defined by
24
  the `conn_args`. The process is then registered at the
25
  `Kompost.Kompo.Postgres.ConnectionRegistry`.
26
  """
27
  @spec connect(
28
          id(),
29
          conn_args :: Keyword.t(),
30
          allowed_namespaces :: NamespaceAccess.allowed_namespaces()
31
        ) ::
32
          {:ok, Postgrex.conn()}
33
          | {:error, Postgrex.Error.t() | Exception.t()}
34
          | DynamicSupervisor.on_start_child()
35
  def connect(id, conn_args, allowed_namespaces) do
36
    with {:lookup, []} <- {:lookup, lookup(id)},
13✔
37
         {:ok, _} <-
12✔
38
           conn_args
39
           |> Postgrex.Utils.default_opts()
40
           |> Postgrex.Protocol.connect() do
41
      args =
11✔
42
        Keyword.put(
43
          conn_args,
44
          :name,
45
          {:via, Registry,
46
           {ConnectionRegistry, id,
47
            Keyword.put(conn_args, :allowed_namespaces, allowed_namespaces)}}
48
        )
49

50
      DynamicSupervisor.start_child(ConnectionSupervisor, {Postgrex, args})
11✔
51
    else
52
      {:lookup, [{conn, _, _}]} -> {:ok, conn}
53
      connection_error -> connection_error
1✔
54
    end
55
  end
56

57
  @doc """
58
  Checks in the `Kompost.Kompo.Postgres.ConnectionRegistry` for an existing
59
  connection defined by the given `id`.
60
  """
61
  @spec lookup(id()) :: [
62
          {pid, conn_args :: keyword(),
63
           allowed_namespaces :: NamespaceAccess.allowed_namespaces()}
64
        ]
65
  def lookup(id) do
66
    Registry.lookup(ConnectionRegistry, id)
67
    |> Enum.map(fn {pid, args} ->
37✔
68
      {allowed_namespaces, conn_args} = Keyword.pop!(args, :allowed_namespaces)
22✔
69
      {pid, conn_args, allowed_namespaces}
22✔
70
    end)
71
  end
72

73
  @doc """
74
  Checks if the user connected to the given `conn` has all the privileges
75
  required to work with this `Kompo`.
76
  """
77
  @spec check_privileges(conn :: Postgrex.conn()) :: :ok | {:error, binary()}
78
  def check_privileges(conn) do
79
    with :ok <- Privileges.check_create_role_privilege(conn) do
17✔
80
      Privileges.check_create_database_privilege(conn)
15✔
81
    end
82
  end
83

84
  @doc """
85
  Disconnects the connection with the given `id` by stopping the referenced
86
  `Postgrex` process.
87
  """
88
  @spec disconnect(id()) :: :ok
89
  def disconnect(id) do
90
    case lookup(id) do
7✔
91
      [{conn, _, _}] ->
92
        # This will also unregister the process at the ConnectionRegistry:
93
        DynamicSupervisor.terminate_child(ConnectionSupervisor, conn)
5✔
94
        :ok
95

96
      [] ->
2✔
97
        :ok
98
    end
99
  end
100
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc