active_record_extended 2.0.3 → 3.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +140 -77
- data/lib/active_record_extended/arel/nodes.rb +1 -1
- data/lib/active_record_extended/arel/{sql_literal.rb → sql_literal_patch.rb} +2 -2
- data/lib/active_record_extended/arel/visitors/postgresql_decorator.rb +16 -12
- data/lib/active_record_extended/arel.rb +1 -1
- data/lib/active_record_extended/patch/array_handler_patch.rb +22 -0
- data/lib/active_record_extended/patch/relation_patch.rb +82 -0
- data/lib/active_record_extended/patch/where_clause_patch.rb +13 -0
- data/lib/active_record_extended/query_methods/any_of.rb +7 -31
- data/lib/active_record_extended/query_methods/either.rb +5 -7
- data/lib/active_record_extended/query_methods/{select.rb → foster_select.rb} +4 -4
- data/lib/active_record_extended/query_methods/json.rb +2 -2
- data/lib/active_record_extended/query_methods/unionize.rb +3 -3
- data/lib/active_record_extended/query_methods/where_chain.rb +96 -90
- data/lib/active_record_extended/query_methods/window.rb +3 -3
- data/lib/active_record_extended/query_methods/with_cte.rb +70 -9
- data/lib/active_record_extended/utilities/order_by.rb +1 -1
- data/lib/active_record_extended/utilities/support.rb +1 -1
- data/lib/active_record_extended/version.rb +1 -1
- data/lib/active_record_extended.rb +55 -4
- metadata +34 -83
- data/lib/active_record_extended/active_record/relation_patch.rb +0 -50
- data/lib/active_record_extended/active_record.rb +0 -25
- data/lib/active_record_extended/patch/5_1/where_clause.rb +0 -11
- data/lib/active_record_extended/patch/5_2/where_clause.rb +0 -11
- data/lib/active_record_extended/predicate_builder/array_handler_decorator.rb +0 -20
- data/spec/active_record_extended_spec.rb +0 -7
- data/spec/query_methods/any_of_spec.rb +0 -131
- data/spec/query_methods/array_query_spec.rb +0 -64
- data/spec/query_methods/either_spec.rb +0 -70
- data/spec/query_methods/hash_query_spec.rb +0 -45
- data/spec/query_methods/inet_query_spec.rb +0 -112
- data/spec/query_methods/json_spec.rb +0 -157
- data/spec/query_methods/select_spec.rb +0 -115
- data/spec/query_methods/unionize_spec.rb +0 -165
- data/spec/query_methods/window_spec.rb +0 -51
- data/spec/query_methods/with_cte_spec.rb +0 -50
- data/spec/spec_helper.rb +0 -28
- data/spec/sql_inspections/any_of_sql_spec.rb +0 -41
- data/spec/sql_inspections/arel/aggregate_function_name_spec.rb +0 -41
- data/spec/sql_inspections/arel/array_spec.rb +0 -63
- data/spec/sql_inspections/arel/inet_spec.rb +0 -66
- data/spec/sql_inspections/contains_sql_queries_spec.rb +0 -47
- data/spec/sql_inspections/either_sql_spec.rb +0 -71
- data/spec/sql_inspections/json_sql_spec.rb +0 -82
- data/spec/sql_inspections/unionize_sql_spec.rb +0 -124
- data/spec/sql_inspections/window_sql_spec.rb +0 -98
- data/spec/sql_inspections/with_cte_sql_spec.rb +0 -95
- data/spec/support/database_cleaner.rb +0 -15
- data/spec/support/models.rb +0 -80
@@ -1,95 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
RSpec.describe "Active Record WITH CTE tables" do
|
6
|
-
let(:with_personal_query) { /WITH.+personal_id_one.+AS \(SELECT.+users.+FROM.+WHERE.+users.+personal_id.+ = 1\)/ }
|
7
|
-
|
8
|
-
it "should contain WITH statement that creates the CTE table" do
|
9
|
-
query = User.with(personal_id_one: User.where(personal_id: 1))
|
10
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
11
|
-
.to_sql
|
12
|
-
expect(query).to match_regex(with_personal_query)
|
13
|
-
end
|
14
|
-
|
15
|
-
it "will maintain the CTE table when merging" do
|
16
|
-
query = User.all
|
17
|
-
.merge(User.with(personal_id_one: User.where(personal_id: 1)))
|
18
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
19
|
-
.to_sql
|
20
|
-
|
21
|
-
expect(query).to match_regex(with_personal_query)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "will pipe Children CTE's into the Parent relation" do
|
25
|
-
personal_id_one_query = User.where(personal_id: 1)
|
26
|
-
personal_id_two_query = User.where(personal_id: 2)
|
27
|
-
|
28
|
-
sub_query = personal_id_two_query.with(personal_id_one: personal_id_one_query)
|
29
|
-
query = User.all.with(personal_id_two: sub_query)
|
30
|
-
expected_order = User.with(
|
31
|
-
personal_id_one: personal_id_one_query,
|
32
|
-
personal_id_two: personal_id_two_query
|
33
|
-
)
|
34
|
-
|
35
|
-
expect(query.to_sql).to eq(expected_order.to_sql)
|
36
|
-
end
|
37
|
-
|
38
|
-
context "when multiple CTE's" do
|
39
|
-
let(:chained_with) do
|
40
|
-
User.with(personal_id_one: User.where(personal_id: 1))
|
41
|
-
.with(personal_id_two: User.where(personal_id: 2))
|
42
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
43
|
-
.joins("JOIN personal_id_two ON personal_id_two.id = users.id")
|
44
|
-
.to_sql
|
45
|
-
end
|
46
|
-
|
47
|
-
let(:with_arguments) do
|
48
|
-
User.with(personal_id_one: User.where(personal_id: 1), personal_id_two: User.where(personal_id: 2))
|
49
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
50
|
-
.joins("JOIN personal_id_two ON personal_id_two.id = users.id")
|
51
|
-
.to_sql
|
52
|
-
end
|
53
|
-
|
54
|
-
it "Should only contain a single WITH statement" do
|
55
|
-
expect(with_arguments.scan(/WITH/).count).to eq(1)
|
56
|
-
expect(with_arguments.scan(/AS/).count).to eq(2)
|
57
|
-
end
|
58
|
-
|
59
|
-
it "Should only contain a single WITH statement when chaining" do
|
60
|
-
expect(chained_with.scan(/WITH/).count).to eq(1)
|
61
|
-
expect(chained_with.scan(/AS/).count).to eq(2)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context "when chaining the recursive method" do
|
66
|
-
let(:with_recursive_personal_query) do
|
67
|
-
/WITH.+RECURSIVE.+personal_id_one.+AS \(SELECT.+users.+FROM.+WHERE.+users.+personal_id.+ = 1\)/
|
68
|
-
end
|
69
|
-
|
70
|
-
let(:with_recursive) do
|
71
|
-
User.with
|
72
|
-
.recursive(personal_id_one: User.where(personal_id: 1))
|
73
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
74
|
-
.to_sql
|
75
|
-
end
|
76
|
-
|
77
|
-
it "generates an expression with recursive" do
|
78
|
-
query = User.with
|
79
|
-
.recursive(personal_id_one: User.where(personal_id: 1))
|
80
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
81
|
-
.to_sql
|
82
|
-
|
83
|
-
expect(query).to match_regex(with_recursive_personal_query)
|
84
|
-
end
|
85
|
-
|
86
|
-
it "will maintain the CTE table when merging" do
|
87
|
-
sub_query = User.with.recursive(personal_id_one: User.where(personal_id: 1))
|
88
|
-
query = User.merge(sub_query)
|
89
|
-
.joins("JOIN personal_id_one ON personal_id_one.id = users.id")
|
90
|
-
.to_sql
|
91
|
-
|
92
|
-
expect(query).to match_regex(with_recursive_personal_query)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
data/spec/support/models.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class ApplicationRecord < ActiveRecord::Base
|
4
|
-
self.abstract_class = true
|
5
|
-
end
|
6
|
-
|
7
|
-
class User < ApplicationRecord
|
8
|
-
has_many :groups_users, class_name: "GroupsUser"
|
9
|
-
has_many :groups, through: :groups_users, dependent: :destroy
|
10
|
-
has_many :hm_tags, class_name: "Tag"
|
11
|
-
has_one :profile_l, class_name: "ProfileL"
|
12
|
-
has_one :profile_r, class_name: "ProfileR"
|
13
|
-
# attributes
|
14
|
-
# t.string "tags", array: true
|
15
|
-
# t.integer "number", default: 0
|
16
|
-
# t.string "name"
|
17
|
-
# t.integer "personal_id"
|
18
|
-
# t.hstore "data"
|
19
|
-
# t.jsonb "jsonb_data"
|
20
|
-
# t.inet "ip"
|
21
|
-
# t.cidr "subnet"
|
22
|
-
#
|
23
|
-
end
|
24
|
-
|
25
|
-
class StiRecord < ApplicationRecord
|
26
|
-
# t.string "type"
|
27
|
-
end
|
28
|
-
|
29
|
-
class AdminSti < StiRecord; end
|
30
|
-
|
31
|
-
module Namespaced
|
32
|
-
def self.table_name_prefix
|
33
|
-
"namespaced_"
|
34
|
-
end
|
35
|
-
|
36
|
-
class Record < ApplicationRecord
|
37
|
-
# attributes
|
38
|
-
# t.inet :ip
|
39
|
-
# t.cidr :subnet
|
40
|
-
#
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class Tag < ApplicationRecord
|
45
|
-
belongs_to :user
|
46
|
-
# attributes: tag_number
|
47
|
-
end
|
48
|
-
|
49
|
-
class ProfileL < ApplicationRecord
|
50
|
-
belongs_to :user
|
51
|
-
has_one :version, as: :versionable, class_name: "VersionControl"
|
52
|
-
# attributes
|
53
|
-
# t.integer :likes
|
54
|
-
#
|
55
|
-
end
|
56
|
-
|
57
|
-
class ProfileR < ApplicationRecord
|
58
|
-
belongs_to :user
|
59
|
-
has_one :version, as: :versionable, class_name: "VersionControl"
|
60
|
-
# attributes
|
61
|
-
# t.integer :dislikes
|
62
|
-
#
|
63
|
-
end
|
64
|
-
|
65
|
-
class VersionControl < ApplicationRecord
|
66
|
-
belongs_to :versionable, polymorphic: true, optional: false
|
67
|
-
# attributes
|
68
|
-
# t.jsonb :source, default: {}, null: false
|
69
|
-
#
|
70
|
-
end
|
71
|
-
|
72
|
-
class Group < ApplicationRecord
|
73
|
-
has_many :groups_users, class_name: "GroupsUser"
|
74
|
-
has_many :users, through: :groups_users, dependent: :destroy
|
75
|
-
end
|
76
|
-
|
77
|
-
class GroupsUser < ApplicationRecord
|
78
|
-
belongs_to :user
|
79
|
-
belongs_to :group
|
80
|
-
end
|