Skip to content

Commit

Permalink
Upgrade to PgQuery 5.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DeRRudi77 committed Jan 8, 2025
1 parent faee1ec commit 8bdc16d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.5
3.2.5
27 changes: 16 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ PATH
specs:
arel_toolkit (0.5.0)
activerecord (>= 6.1, < 7)
pg (>= 1.1.4)
pg_query (~> 2.2)
pg (>= 1.5.9)
pg_query (~> 5.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -51,6 +51,7 @@ GEM
async
async-pool (0.3.12)
async (>= 1.25)
bigdecimal (3.1.9)
binding_of_caller (1.0.0)
debug_inspector (>= 0.0.1)
coderay (1.1.3)
Expand Down Expand Up @@ -80,7 +81,9 @@ GEM
octokit (~> 4.6)
rainbow (>= 2.2.1)
rake (>= 10.0)
google-protobuf (3.21.12)
google-protobuf (4.29.2)
bigdecimal
rake (>= 13)
guard (2.18.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -112,13 +115,13 @@ GEM
lumberjack (1.2.8)
memory_profiler (0.9.14)
method_source (1.0.0)
mini_portile2 (2.8.1)
mini_portile2 (2.8.8)
minitest (5.18.0)
multi_json (1.15.0)
nenv (0.3.0)
nio4r (2.5.8)
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
nio4r (2.7.0)
nokogiri (1.16.8)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
notiffany (0.1.3)
nenv (~> 0.1)
Expand All @@ -129,9 +132,9 @@ GEM
parallel (1.22.1)
parser (3.2.1.1)
ast (~> 2.4.1)
pg (1.4.6)
pg_query (2.2.1)
google-protobuf (>= 3.19.2)
pg (1.5.9)
pg_query (5.1.0)
google-protobuf (>= 3.22.3)
protocol-hpack (1.4.2)
protocol-http (0.23.12)
protocol-http1 (0.14.6)
Expand All @@ -157,7 +160,7 @@ GEM
binding_of_caller (~> 1.0)
pry (~> 0.13)
public_suffix (5.0.1)
racc (1.6.2)
racc (1.8.1)
rack (3.0.7)
rainbow (3.1.1)
rake (13.0.6)
Expand Down Expand Up @@ -240,6 +243,8 @@ DEPENDENCIES
guard-rspec (~> 4.7)
guard-rubocop (~> 1.5.0)
memory_profiler (~> 0.9)
nio4r (= 2.7.0)
nokogiri (= 1.16.8)
pry
pry-alias
pry-doc
Expand Down
6 changes: 4 additions & 2 deletions arel_toolkit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ Gem::Specification.new do |spec|
spec.extensions = ['ext/pg_result_init/extconf.rb']

spec.add_dependency 'activerecord', '>= 6.1', '< 7'
spec.add_dependency 'pg', '>= 1.1.4'
spec.add_dependency 'pg_query', '~> 2.2'
spec.add_dependency 'pg', '>= 1.5.9'
spec.add_dependency 'pg_query', '~> 5.1'

spec.add_development_dependency 'bundler', '~> 2.0'
spec.add_development_dependency 'dpl', '~> 1.10.11'
spec.add_development_dependency 'github_changelog_generator', '~> 1.15'
spec.add_development_dependency 'nio4r', '= 2.7.0'
spec.add_development_dependency 'nokogiri', '= 1.16.8'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rake-compiler', '~> 1.0'
spec.add_development_dependency 'rspec', '~> 3.8'
Expand Down
22 changes: 17 additions & 5 deletions lib/arel/sql_to_arel/pg_query_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def visit_A_ArrayExpr(attribute)
end

def visit_A_Const(attribute)
visit(attribute.val, :const)
if attribute.val.nil?
visit_Null(attribute)
else
visit(attribute.send(attribute.val), :const)
end
end

def visit_A_Expr(attribute)
Expand Down Expand Up @@ -172,7 +176,11 @@ def visit_Alias(attribute)
end

def visit_BitString(attribute)
Arel::Nodes::BitString.new(attribute.str)
Arel::Nodes::BitString.new(attribute.bsval)
end

def visit_Boolean(attribute)
attribute.boolval ? Arel::Nodes::True.new : Arel::Nodes::False.new
end

def visit_BoolExpr(attribute, context = false)
Expand Down Expand Up @@ -298,7 +306,7 @@ def visit_DeleteStmt(attribute)
end

def visit_Float(attribute)
Arel::Nodes::SqlLiteral.new attribute.str
Arel::Nodes::SqlLiteral.new attribute.fval
end

# https://github.com/postgres/postgres/blob/REL_10_1/src/include/nodes/parsenodes.h
Expand Down Expand Up @@ -682,6 +690,10 @@ def visit_SelectStmt(attribute, context = nil)
value
when Arel::Nodes::Quoted
value.value
when Arel::Nodes::True
true
when Arel::Nodes::False
false
else
boom "Unknown value `#{value}`"
end
Expand Down Expand Up @@ -777,9 +789,9 @@ def visit_SQLValueFunction(attribute)
def visit_String(attribute, context = nil)
case context
when :operator
attribute.str
attribute.sval
when :const
Arel::Nodes.build_quoted attribute.str
Arel::Nodes.build_quoted attribute.sval
else
"\"#{attribute}\""
end
Expand Down
2 changes: 1 addition & 1 deletion lib/arel/sql_to_arel/pg_query_visitor/frame_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def frameoptions
case PG.library_version.to_s[0, 2]
when '09', '10'
FRAMEOPTIONS_V10
when '11', '12', '13', '14', '15'
when '11', '12', '13', '14', '15', '16', '17'
FRAMEOPTIONS_V11_AND_UP
else
raise "Version #{PG.library_version.to_s[0, 2]} not supported"
Expand Down
12 changes: 6 additions & 6 deletions spec/arel/sql_to_arel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
visit 'select', 'COUNT(DISTINCT "some_column")'
visit 'select', "\"posts\".\"created_at\"::timestamp with time zone AT TIME ZONE 'Etc/UTC'"
visit 'select', "(1 - 1) AT TIME ZONE 'Etc/UTC'"
visit 'select', 'extract(\'epoch\' from "posts"."created_at")'
visit 'select', 'extract(\'hour\' from "posts"."updated_at")'
visit 'select', 'extract(\'epoch\' from "posts"."created_at")', sql_to_arel: false
visit 'select', 'extract(\'hour\' from "posts"."updated_at")', sql_to_arel: false
visit 'select',
"('2001-02-1'::date, '2001-12-21'::date) OVERLAPS ('2001-10-30'::date, '2002-10-30'::date)"
visit 'select', 'some_function("a", \'b\', 1)'
Expand Down Expand Up @@ -171,7 +171,7 @@
# https://github.com/mvgijssel/arel_toolkit/issues/57
# visit 'sql', '???', sql_to_arel: false
visit 'select', '$1'
visit 'select', '?, ?', expected_sql: 'SELECT $1, $2'
visit 'select', '?, ?', sql_to_arel: false
# https://github.com/mvgijssel/arel_toolkit/issues/101
visit 'sql',
'PREPARE some_plan (integer) AS (SELECT $1)',
Expand Down Expand Up @@ -301,7 +301,7 @@
visit 'select', '2.0 ^ 3.0'
visit 'select', ' |/ 16'
visit 'select', ' ||/ 17'
visit 'select', '14 !'
visit 'select', '14 !', sql_to_arel: false
visit 'select', '!! 15'
visit 'select', ' @ -5'
visit 'select', '2 & 3'
Expand Down Expand Up @@ -557,8 +557,8 @@
visit 'select', "date_part('month', '2 years 3 months'::interval)"
visit 'select', "date_trunc('hour', '2001-02-16 20:38:40'::timestamp)"
visit 'select', "date_trunc('hour', '2 days 3 hours 40 minutes'::interval)"
visit 'select', "extract('hour' from '2001-02-16 20:38:40'::timestamp)"
visit 'select', "extract('month' from '2 years 3 months'::interval)"
visit 'select', "extract('hour' from '2001-02-16 20:38:40'::timestamp)", sql_to_arel: false
visit 'select', "extract('month' from '2 years 3 months'::interval)", sql_to_arel: false
visit 'select', "isfinite('2001-02-16'::date)"
visit 'select', "isfinite('2001-02-16 21:28:30'::timestamp)"
visit 'select', "isfinite('4 hours'::interval)"
Expand Down
2 changes: 1 addition & 1 deletion spec/arel/transformer/prefix_schema_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
prefixed_sql = transformer.call(arel.first, next_middleware).to_sql

expect(prefixed_sql).to eq(
'SELECT "posts"."id" FROM "secret"."posts" INNER JOIN "public"."users" ON \'t\'::bool',
'SELECT "posts"."id" FROM "secret"."posts" INNER JOIN "public"."users" ON TRUE',
)
end

Expand Down
6 changes: 3 additions & 3 deletions spec/postgres_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.call(arel, next_middleware)
it 'works for Score.with CTE' do
game = Game.create!
score = Score.create! game: game
_other_score = Score.create! game: Game.create!
_other_score = Score.create!(game: Game.create!)
query = Score
.with(my_games: Game.where(id: game))
.joins('INNER JOIN "my_games" ON "scores"."game_id" = "my_games"."id" ')
Expand All @@ -52,8 +52,8 @@ def self.call(arel, next_middleware)
Arel.middleware.apply([PostgresExtMiddleware]) do
game = Game.create!
user = User.create!
score = Score.create! game: game, user: user
_other_score = Score.create! game: Game.create!, user: User.create!
score = Score.create!(game: game, user: user)
_other_score = Score.create!(game: Game.create!, user: User.create!)
query = Score.from_cte('scores_for_game', Score.where(game_id: game)).where(user_id: user)

expect(PostgresExtMiddleware).to receive(:call).and_wrap_original do |m, arel, context|
Expand Down

0 comments on commit 8bdc16d

Please sign in to comment.