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
0 commit comments