Skip to content

Commit a361d75

Browse files
authored
Extract PostgreSQL version constants (#181)
- Extract magic number into a structured constant - Add documentation with links to PostgreSQL release notes - Add comprehensive tests for version checking This refactoring improves code readability and makes it easier to add support for future PostgreSQL versions.
1 parent 7ec51af commit a361d75

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

lib/fx/adapters/postgres/connection.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ class Postgres
1010
#
1111
# @api private
1212
class Connection < SimpleDelegator
13-
# https://www.postgresql.org/docs/9.6/sql-dropfunction.html
14-
# https://www.postgresql.org/docs/10/sql-dropfunction.html
13+
# PostgreSQL version constants for feature support
14+
POSTGRES_VERSIONS = {
15+
# PostgreSQL 10.0 - introduced DROP FUNCTION without args
16+
# https://www.postgresql.org/docs/10/sql-dropfunction.html
17+
v10: 10_00_00
18+
}.freeze
19+
1520
def support_drop_function_without_args
16-
pg_connection = undecorated_connection.raw_connection
17-
pg_connection.server_version >= 10_00_00
21+
server_version >= POSTGRES_VERSIONS[:v10]
1822
end
1923

2024
private
2125

26+
def server_version
27+
undecorated_connection.raw_connection.server_version
28+
end
29+
2230
def undecorated_connection
2331
__getobj__
2432
end

spec/fx/adapters/postgres_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,46 @@
141141
expect(adapter.triggers.map(&:name)).to eq ["uppercase_users_name"]
142142
end
143143
end
144+
145+
describe "#support_drop_function_without_args" do
146+
it "returns true for PostgreSQL version 10.0" do
147+
adapter = Fx::Adapters::Postgres.new
148+
connection = adapter.send(:connection)
149+
allow(connection).to receive(:server_version).and_return(10_00_00)
150+
151+
result = connection.support_drop_function_without_args
152+
153+
expect(result).to be(true)
154+
end
155+
156+
it "returns true for PostgreSQL version 11.0" do
157+
adapter = Fx::Adapters::Postgres.new
158+
connection = adapter.send(:connection)
159+
allow(connection).to receive(:server_version).and_return(11_00_00)
160+
161+
result = connection.support_drop_function_without_args
162+
163+
expect(result).to be(true)
164+
end
165+
166+
it "returns false for PostgreSQL version 9.6" do
167+
adapter = Fx::Adapters::Postgres.new
168+
connection = adapter.send(:connection)
169+
allow(connection).to receive(:server_version).and_return(9_06_00)
170+
171+
result = connection.support_drop_function_without_args
172+
173+
expect(result).to be(false)
174+
end
175+
176+
it "returns false for PostgreSQL version 9.5" do
177+
adapter = Fx::Adapters::Postgres.new
178+
connection = adapter.send(:connection)
179+
allow(connection).to receive(:server_version).and_return(9_05_00)
180+
181+
result = connection.support_drop_function_without_args
182+
183+
expect(result).to be(false)
184+
end
185+
end
144186
end

0 commit comments

Comments
 (0)