Skip to content

Commit d492ba0

Browse files
committed
ensure running supported major postgres version
add migration updating earthdistance extension update docs reflecting required versions
1 parent f5d0631 commit d492ba0

File tree

7 files changed

+80
-11
lines changed

7 files changed

+80
-11
lines changed

lib/teslamate/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ defmodule TeslaMate.Application do
1111
:ok = :telemetry.detach({Phoenix.Logger, [:phoenix, :socket_connected]})
1212
:ok = :telemetry.detach({Phoenix.Logger, [:phoenix, :channel_joined]})
1313

14+
TeslaMate.DatabaseCheck.check_postgres_version()
15+
1416
Supervisor.start_link(children(), strategy: :one_for_one, name: TeslaMate.Supervisor)
1517
end
1618

lib/teslamate/database_check.ex

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
defmodule TeslaMate.DatabaseCheck do
2+
alias Ecto.Adapters.SQL
3+
alias TeslaMate.Repo
4+
5+
# Minimum versions for supported major releases
6+
@min_version_16 "16.7"
7+
@min_version_17 "17.3"
8+
9+
def check_postgres_version do
10+
# Start the Repo manually without running migrations
11+
{:ok, _pid} = Repo.start_link()
12+
13+
# Query the PostgreSQL version
14+
{:ok, result} = SQL.query(Repo, "SELECT regexp_replace(version(), 'PostgreSQL ([^ ]+) .*', '\\1') AS version", [])
15+
raw_version = result.rows |> List.first() |> List.first()
16+
17+
# Normalize to SemVer by appending .0 if needed
18+
version = normalize_version(raw_version)
19+
20+
# Split into major and minor parts
21+
[major, _minor] = String.split(version, ".", parts: 2)
22+
major_int = String.to_integer(major)
23+
24+
# Check based on major version
25+
case major_int do
26+
16 ->
27+
case Version.compare(version, normalize_version(@min_version_16)) do
28+
:lt ->
29+
raise "PostgreSQL version #{raw_version} is not supported. Minimum required for 16.x is #{@min_version_16}."
30+
_ ->
31+
IO.puts("PostgreSQL version #{raw_version} is compatible (16.x series).")
32+
end
33+
34+
17 ->
35+
case Version.compare(version, normalize_version(@min_version_17)) do
36+
:lt ->
37+
raise "PostgreSQL version #{raw_version} is not supported. Minimum required for 17.x is #{@min_version_17}."
38+
_ ->
39+
IO.puts("PostgreSQL version #{raw_version} is compatible (17.x series).")
40+
end
41+
42+
major_int when major_int > 17 ->
43+
IO.puts("PostgreSQL version #{raw_version} is not officially tested or supported yet. Use at your own risk.")
44+
45+
_ ->
46+
raise "PostgreSQL version #{raw_version} is not supported. Only 16.x (min #{@min_version_16}) and 17.x (min #{@min_version_17}) are supported."
47+
end
48+
49+
# Stop the Repo after the check
50+
Repo.stop()
51+
end
52+
53+
# Helper function to normalize PostgreSQL version to SemVer
54+
defp normalize_version(version) do
55+
case String.split(version, ".") do
56+
[major, minor] -> "#{major}.#{minor}.0"
57+
[major, minor, patch | _] -> "#{major}.#{minor}.#{patch}"
58+
_ -> raise "Invalid PostgreSQL version format: #{version}"
59+
end
60+
end
61+
end

nix/module.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ in
8282
'';
8383
};
8484

85-
package = mkPackageOption pkgs "postgresql_16" {
86-
# 17 is not yet available in nixpkgs
85+
package = mkPackageOption pkgs "postgresql_17" {
8786
extraDescription = ''
8887
The postgresql package to use.
8988
'';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule TeslaMate.Repo.Migrations.UpgradeEarthdistance do
2+
use Ecto.Migration
3+
4+
def change do
5+
execute("ALTER EXTENSION earthdistance UPDATE")
6+
end
7+
end

website/docs/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar_label: Development and Contributing
77
## Requirements
88

99
- **Elixir** >= 1.17.3-otp-26
10-
- **Postgres** >= 17
10+
- **Postgres** >= 17.3
1111
- An **MQTT broker** e.g. mosquitto (_optional_)
1212
- **NodeJS** >= 20.15.0
1313

website/docs/installation/debian.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This document provides the necessary steps for installation of TeslaMate on a va
1010
Click on the following items to view detailed installation steps.
1111

1212
<details>
13-
<summary>Postgres (v17+)</summary>
13+
<summary>Postgres (v17.3+)</summary>
1414

1515
```bash
1616
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
@@ -37,7 +37,7 @@ Source: [erlang.org/downloads](https://www.erlang.org/downloads#prebuilt)
3737
</details>
3838

3939
<details>
40-
<summary>Grafana (v11.5.0+)</summary>
40+
<summary>Grafana (v11.6.0+)</summary>
4141

4242
```bash
4343
sudo apt-get install -y apt-transport-https software-properties-common

website/docs/installation/freebsd.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ pkg install elixir
5050
</details>
5151

5252
<details>
53-
<summary>Postgres (v17+)</summary>
53+
<summary>Postgres (v17.3+)</summary>
5454

5555
```bash
56-
pkg install postgresql17-server-17.0
57-
pkg install postgresql17-contrib-17.0
56+
pkg install postgresql17-server
57+
pkg install postgresql17-contrib
5858
echo postgres_enable="yes" >> /etc/rc.conf
5959
```
6060

@@ -70,7 +70,7 @@ service postgresql initdb
7070
</details>
7171

7272
<details>
73-
<summary>Grafana (v11.5.0+)</summary>
73+
<summary>Grafana (v11.6.0+)</summary>
7474

7575
```bash
7676
pkg install grafana
@@ -93,8 +93,8 @@ echo mosquitto_enable="yes" >> /etc/rc.conf
9393
<summary>Node.js (v20+)</summary>
9494

9595
```bash
96-
pkg install node20-20.18.1
97-
pkg install npm-node20-10.9.0
96+
pkg install node20
97+
pkg install npm-node20
9898
```
9999

100100
</details>

0 commit comments

Comments
 (0)