Packages
surgex
5.1.1
6.0.1
6.0.0
5.1.1
5.1.0
5.0.0
5.0.0-git-015aa39a
4.15.2
4.15.1
4.15.1-git-98b3
4.15.0
4.15.0-git-943e
4.15.0-git-5806
4.14.1
4.14.0
4.13.1
4.13.0
4.12.0
4.11.0
4.11.0-git-97d4
4.11.0-git-14c8
4.10.0
4.10.0-git-37f2
4.9.0
4.9.0-git-9d5f
4.8.1-git-80a7
4.8.0
4.8.0-git-fbda
4.8.0-git-e058
4.7.0
4.7.0-git-5414
4.6.1
4.6.0
4.5.0
4.4.0
4.3.0
4.2.1
4.2.0
4.1.1
4.1.1-git-9f6b
4.1.1-git-6f0cd
4.1.0
4.1.0-git-e934
4.1.0-git-8c28
4.1.0-git-56a9
4.1.0-git-55fd
4.0.0
3.2.8
3.2.7
3.2.6
3.2.5
3.2.4
3.2.3
3.2.2
3.2.1
3.2.0
retired
3.1.0
3.0.0
2.24.1
2.24.0
retired
2.23.0
2.22.0
2.21.0
2.20.1
2.20.0
2.19.0
2.18.1
2.18.0
2.17.0
2.16.0
2.15.0
2.14.0
2.13.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.0
2.6.0
2.5.1
2.5.0
2.4.0
retired
2.3.0
2.2.1
2.2.0
2.1.1
2.1.0
2.0.0
1.6.0
1.5.2
1.5.1
1.5.0
1.4.0
1.3.1
retired
1.3.0
retired
1.2.1
1.2.0
1.1.0
1.0.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
All Things Elixir @ Surge Ventures Inc, the creators of Fresha
Current section
Files
Jump to
Current section
Files
lib/surgex/data_pipe/postgres_system_utils.ex
defmodule Surgex.DataPipe.PostgresSystemUtils do
@moduledoc """
Executes system-level PostgreSQL queries (server version, WAL status etc).
"""
def full_version_string(repo) do
%{rows: [[value]]} = repo.query!("SELECT version()")
value
end
def version(repo) do
full_version_string = full_version_string(repo)
version_match = Regex.run(~r/PostgreSQL (\d+\.\d+(\.\d+)?)/, full_version_string)
unless version_match do
raise("Invalid full version string: #{full_version_string}")
end
version_match
|> Enum.at(1)
|> append_patch_version()
|> Version.parse!()
end
defp append_patch_version(version_string) do
if String.match?(version_string, ~r/^\d+\.\d+$/) do
"#{version_string}.0"
else
version_string
end
end
def version_match?(repo, requirement) do
repo
|> version()
|> Version.match?(requirement)
end
def get_current_wal_lsn(repo) do
get_lsn(repo, get_current_wal_lsn_function(repo))
end
def get_current_wal_lsn_function(repo) do
if version_match?(repo, ">= 10.0.0") do
"pg_current_wal_lsn()"
else
"pg_current_xlog_location()"
end
end
def get_last_wal_replay_lsn(repo) do
get_lsn(repo, get_last_wal_replay_lsn_function(repo))
end
def get_last_wal_replay_lsn_function(repo) do
if version_match?(repo, ">= 10.0.0") do
"pg_last_wal_replay_lsn()"
else
"pg_last_xlog_replay_location()"
end
end
def get_lsn(repo, func) do
with %{rows: [[lsn]]} <- repo.query!("SELECT #{func}::varchar"),
true <- lsn_valid?(lsn) do
{:ok, lsn}
else
_ -> :error
end
end
def lsn_valid?(lsn) do
is_binary(lsn) && String.match?(lsn, ~r/^[0-9A-F]+\/[0-9A-F]+$/)
end
end