Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve RECURSIVE flag of CTE relations when they are merged. #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/postgres_ext/active_record/relation/merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ActiveRecord
class Relation
class Merger # :nodoc:
def normal_values
NORMAL_VALUES + [:with]
NORMAL_VALUES + [:with, :recursive]
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/postgres_ext/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def ranked!(value)
self
end

def recursive!(value)
self.recursive_value = value
self
end

def build_arel_with_extensions
arel = build_arel_without_extensions

Expand Down
8 changes: 8 additions & 0 deletions test/queries/common_table_expression_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
query.to_sql.must_match(/WITH RECURSIVE "lucky_number_seven" AS \(SELECT "people".* FROM "people"(\s+)WHERE "people"."lucky_number" = 7\) SELECT "people".* FROM "people" JOIN lucky_number_seven ON lucky_number_seven.id = people.id/)
end

it 'preserves recursive expression during merges' do
merge_recursive_in = Tag.all.merge(Tag.recursive)
merge_recursive_in.to_sql.must_match(/WITH RECURSIVE "recursive" AS \(SELECT "tags".* FROM "tags"(\s+)WHERE "tags"."tag" = 'tag'\) SELECT "tags".* FROM "tags"/)

merge_into_recursive = Tag.recursive.merge(Tag.all)
merge_into_recursive.to_sql.must_match(/WITH RECURSIVE "recursive" AS \(SELECT "tags".* FROM "tags"(\s+)WHERE "tags"."tag" = 'tag'\) SELECT "tags".* FROM "tags"/)
end

it 'accepts Arel::SelectMangers' do
arel_table = Arel::Table.new 'test'
arel_manager = arel_table.project arel_table[:foo]
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def self.wicked_people

class Tag < ActiveRecord::Base
belongs_to :person

scope :recursive, -> { with.recursive(recursive: Tag.where(tag: 'tag')) }
end

class ParentTag < Tag
Expand Down