postgres_ext 2.4.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98ff2c9bb400e566e90b0eee78c7fbc3c4d20626
4
- data.tar.gz: 235aeec3bafdf81928a2abadd4e19dcfdfcf26dd
3
+ metadata.gz: d0f08d6b38730f2207bde034d586897d7e23ef4d
4
+ data.tar.gz: 3fcbb19e1d568f78da0af6d27ca24ab608a7f3f1
5
5
  SHA512:
6
- metadata.gz: 2fbaa25ab99012bea7aa49c91c95ccd088f8abe1b51a78b584454e661073224082782eeedca079b6d763d0fd0ac9a1ed44cfc5cd616eaa5440ab9d2b5bbbbbf5
7
- data.tar.gz: 8991fc711864a429fcc3b95a9a76dbcd353f5723597d3027651934d5ef36208dcdf9fa3ee173dae52e821757582c7d2e39431326ad4971150564fe0ae47cd425
6
+ metadata.gz: 9266913406317d855c1a7eb6e6ed8205770c29560fb2a8f6bad749b431c6d471511300d6a142f9539a6e3cbb76876774ca6504f19d6b21c66f5648929e82efdf
7
+ data.tar.gz: 1a323f3e109c6c18d80f5c57fea2e6607d3e1ad86c457fcd24dc1666bfb517f60b87af008c74c2eb4bbc7e75f8c0dff657d4b8361bb56b3a95784721ceb9a6ec
@@ -1,3 +1,7 @@
1
+ ## 3.0.0
2
+
3
+ * Fixes merging queries with CTE - chitux
4
+
1
5
  ## 2.4.1
2
6
 
3
7
  * Fixes error when creating a join between STI related models - edpaget
data/README.md CHANGED
@@ -47,7 +47,7 @@ To work on postgres\_ext locally, follow these steps:
47
47
  1. Run `bundle install`, this will install (almost) all the development
48
48
  dependencies
49
49
  2. Run `gem install byebug` (not a declared dependency to not break CI)
50
- 3. Run `bundle exec rake setup`, this will set up the `.env` file necessary to run
50
+ 3. Run `bundle exec rake db:setup`, this will set up the `.env` file necessary to run
51
51
  the tests and set up the database
52
52
  4. Run `bundle exec rake db:create`, this will create the test database
53
53
  5. Run `bundle exec rake db:migrate`, this will set up the database tables required
@@ -3,6 +3,7 @@
3
3
  gdep = Gem::Dependency.new('activerecord', '~> 4.2.0.beta4')
4
4
  ar_version_cutoff = gdep.matching_specs.sort_by(&:version).last
5
5
 
6
+ require 'postgres_ext/active_record/relation/merger'
6
7
  require 'postgres_ext/active_record/relation/query_methods'
7
8
  if ar_version_cutoff
8
9
  require 'postgres_ext/active_record/relation/predicate_builder/array_handler'
@@ -0,0 +1,9 @@
1
+ module ActiveRecord
2
+ class Relation
3
+ class Merger # :nodoc:
4
+ def normal_values
5
+ NORMAL_VALUES + [:with]
6
+ end
7
+ end
8
+ end
9
+ end
@@ -19,7 +19,7 @@ module ActiveRecord
19
19
  when Arel::Nodes::In, Arel::Nodes::Equality
20
20
  column = left_column(rel) || column_from_association(rel)
21
21
  equality_for_hstore(rel) if column.type == :hstore
22
-
22
+
23
23
  if column.type == :hstore
24
24
  Arel::Nodes::ContainsHStore.new(rel.left, rel.right)
25
25
  elsif column.respond_to?(:array) && column.array
@@ -71,7 +71,7 @@ module ActiveRecord
71
71
  return if rel.right.is_a?(Hash)
72
72
  rel.right = {new_right_name => rel.right }
73
73
  end
74
-
74
+
75
75
  rel.left.name = rel.left.relation.name.to_sym
76
76
  rel.left.relation.name = rel.left.relation.engine.table_name
77
77
  end
@@ -106,12 +106,12 @@ module ActiveRecord
106
106
  when Arel::Nodes::Equality
107
107
  Arel::Nodes::Equality.new(rel.right, Arel::Nodes::NamedFunction.new(function_name, [rel.left]))
108
108
  else
109
- raise ArgumentError, "Invalid argument for .where.#{funciton_name.downcase}(), got #{rel.class}"
109
+ raise ArgumentError, "Invalid argument for .where.#{function_name.downcase}(), got #{rel.class}"
110
110
  end
111
111
  end
112
112
  end
113
113
  end
114
-
114
+
115
115
  # WithChain objects act as placeholder for queries in which #with does not have any parameter.
116
116
  # In this case, #with must be chained with #recursive to return a new relation.
117
117
  class WithChain
@@ -206,6 +206,8 @@ module ActiveRecord
206
206
  end
207
207
  Arel::Nodes::As.new Arel::Nodes::SqlLiteral.new("\"#{name.to_s}\""), select
208
208
  end
209
+ when Arel::Nodes::As
210
+ with_value
209
211
  end
210
212
  end
211
213
  unless with_statements.empty?
@@ -1,3 +1,3 @@
1
1
  module PostgresExt
2
- VERSION = '2.4.1'
2
+ VERSION = '3.0.0'
3
3
  end
@@ -31,6 +31,34 @@ describe 'Common Table Expression queries' do
31
31
  end
32
32
  end
33
33
 
34
+ describe '.with(common_table_exression_arel_nodes_as)' do
35
+ it 'generates an expression with the CTE' do
36
+ table_def = Arel::Nodes::SqlLiteral.new("update_cte(id, new_lucky)")
37
+ new_values = "(1,12),(2,3),(3,8)"
38
+
39
+ select = Arel::Nodes::SqlLiteral.new( "(VALUES #{new_values})" )
40
+ with = Arel::Nodes::As.new(table_def, select)
41
+
42
+ regex_safe = new_values.gsub("(","\\(").gsub(")","\\)")
43
+
44
+ query = Person.with(with).joins('JOIN update_cte ON update_cte.id = people.id')
45
+ query.to_sql.must_match(/WITH update_cte\(id, new_lucky\) AS \(VALUES #{regex_safe}\) SELECT \"people\".* FROM \"people\" JOIN update_cte ON update_cte.id = people.id/)
46
+ end
47
+
48
+ it 'generates an expression mixed with multiple with calls' do
49
+ table_def = Arel::Nodes::SqlLiteral.new("update_cte(id, new_lucky)")
50
+ new_values = "(1,12),(2,3),(3,8)"
51
+
52
+ select = Arel::Nodes::SqlLiteral.new( "(VALUES #{new_values})" )
53
+ with = Arel::Nodes::As.new(table_def, select)
54
+
55
+ regex_safe = new_values.gsub("(","\\(").gsub(")","\\)")
56
+
57
+ query = Person.with(with).with(lucky_number_seven: Person.where(lucky_number: 7)).joins('JOIN update_cte ON update_cte.id = people.id').joins('JOIN lucky_number_seven ON lucky_number_seven.id = people.id')
58
+ query.to_sql.must_match(/WITH update_cte\(id, new_lucky\) AS \(VALUES #{regex_safe}\), "lucky_number_seven" AS \(SELECT "people".* FROM "people"(\s+)WHERE "people"."lucky_number" = 7\) SELECT \"people\".* FROM \"people\" JOIN update_cte ON update_cte.id = people.id JOIN lucky_number_seven ON lucky_number_seven.id = people.id/)
59
+ end
60
+ end
61
+
34
62
  describe '.from_cte(common_table_expression_hash)' do
35
63
  it 'generates an expression with the CTE as the main table' do
36
64
  query = Person.from_cte('lucky_number_seven', Person.where(lucky_number: 7)).where(id: 5)
@@ -52,4 +80,12 @@ describe 'Common Table Expression queries' do
52
80
  people.model_name.must_equal 'Person'
53
81
  end
54
82
  end
83
+
84
+ describe '.merge(Model.with(common_table_expression_hash))' do
85
+ it 'keeps the CTE in the merged request' do
86
+ query = Person.all.merge(Person.with(lucky_number_seven: Person.where(lucky_number: 7))).joins('JOIN lucky_number_seven ON lucky_number_seven.id = people.id')
87
+ query.to_sql.must_match(/WITH "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/)
88
+ end
89
+ end
90
+
55
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgres_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan McClain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2016-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -174,6 +174,7 @@ files:
174
174
  - lib/postgres_ext/active_record/cte_proxy.rb
175
175
  - lib/postgres_ext/active_record/querying.rb
176
176
  - lib/postgres_ext/active_record/relation.rb
177
+ - lib/postgres_ext/active_record/relation/merger.rb
177
178
  - lib/postgres_ext/active_record/relation/predicate_builder.rb
178
179
  - lib/postgres_ext/active_record/relation/predicate_builder/array_handler.rb
179
180
  - lib/postgres_ext/active_record/relation/query_methods.rb
@@ -219,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
220
  version: '0'
220
221
  requirements: []
221
222
  rubyforge_project:
222
- rubygems_version: 2.4.5
223
+ rubygems_version: 2.4.8
223
224
  signing_key:
224
225
  specification_version: 4
225
226
  summary: Extends ActiveRecord to handle native PostgreSQL data types