nested_select 0.4.0 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b07e539c2e4dd0ab26ae3991c3af0cf5943601dae0c538774cff62ac9709db21
4
- data.tar.gz: af8cdda70c7dfff3b6fe1feba72d39b5c8bcf372c285ef6110d4f45fa471fbee
3
+ metadata.gz: 90b648bcd4371a277e27841b52d173fa0951c49a8fa89e1e3894ce1e2c4a0a02
4
+ data.tar.gz: effb7e1afb4b609ff812ca1aaf7803eb6e65b2729650399656335fc1adee35a9
5
5
  SHA512:
6
- metadata.gz: ebbc597b3ecf57cb80da6cff33852a7dff935cb8b8fcd30d2d5223dcfb54b31c415b80df0c05bb48a46ea6bd4f54d158e1a68c3516d3014622b3fc3ebaf81744
7
- data.tar.gz: 0aebc50d2cb3dcd2363dd6167f30dc479f0c415e6b5cc1786e623c0cb8be3544df9c4aae2426f77ed5c5fe335b1c2986a8587d5910ba7d8e30fe6cbb516ceb5d
6
+ metadata.gz: 2d21c76feffd32cfb7950edd49027e0082a91a6473ad6713eb109fdf2285f42091f8bbbf034ab1c4b5a37c5119ae27ce678fd983a73dc99002cb8c832203ef83
7
+ data.tar.gz: cac45499b8c4748a80bf91210538216764400ede85f84fcf9afc3f1308f73822edfa0c3cfb5a9dbbc1b45fc48b8bb7a8ea7f720a34a7731a0e19f2b5cb1c16ff
data/Appraisals ADDED
@@ -0,0 +1,15 @@
1
+ appraise "rails-7.0" do
2
+ gem "rails", "~> 7.0.0"
3
+ end
4
+
5
+ appraise "rails-7.1" do
6
+ gem "rails", "~> 7.1.3"
7
+ end
8
+
9
+ appraise "rails-7.2" do
10
+ gem "rails", "~> 7.2.0"
11
+ end
12
+
13
+ appraise "rails-8" do
14
+ gem "rails", ">= 8"
15
+ end
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## [0.4.4] - 2025-05-09
2
+ - fixed appraisals and local docker compose
3
+ -
4
+
5
+ ## [0.4.3] - 2025-05-09
6
+ - fixed regression on through scopes whenever nested_select_values are blank
7
+
8
+ ## [0.4.2] - 2025-03-01
9
+ - Refactoring
10
+ - Removed all commented out code
11
+
12
+ ## [0.4.1] - 2025-03-01
13
+ - Appraisal added as a development dependency
14
+ - added github CI
15
+
16
+ ## [0.4.0] - 2025-02-09
17
+ - through relations are now also supported and allowed to partial selection via reverse nested_selection tree
18
+ - README updated with more examples and corner cases
19
+ - nested_select will prevent multiple partial instantiation with different attributes
20
+
1
21
  ## [0.3.0] - 2025-01-25
2
22
 
3
23
  - nested_select belongs_to limitation now prevents accidental foreign_key absence
data/Dockerfile_apprsl ADDED
@@ -0,0 +1,25 @@
1
+ FROM ruby:3.2-bullseye
2
+
3
+ WORKDIR /app
4
+ RUN apt-get update && apt-get -y install lsb-release
5
+ #
6
+ RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
7
+ sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
8
+ apt-get update && apt-get -y install postgresql postgresql-client-14
9
+
10
+ RUN sh -c 'echo "local all all trust" > /etc/postgresql/$(ls /etc/postgresql)/main/pg_hba.conf' && \
11
+ service postgresql start && \
12
+ psql -U postgres -c 'CREATE DATABASE "niceql-test"'
13
+
14
+ RUN gem install bundler
15
+
16
+ COPY lib/nested_select/version.rb /app/lib/nested_select/version.rb
17
+ COPY nested_select.gemspec /app/
18
+ COPY Gemfil* /app/
19
+
20
+ #
21
+ RUN bundle install
22
+
23
+ COPY gemfiles /app/
24
+ COPY Appraisals /app/
25
+ RUN appraisal install
data/README.md CHANGED
@@ -1,18 +1,15 @@
1
- # WIP disclaimer
2
- The gem is under active development now.
3
- Use in prod with caution only if you are properly covered by your CI.
4
- Read **Safety** and **Limitations** sections before.
5
-
6
1
  # Nested select -- 7 times faster and 33 times less RAM on preloading relations with heavy columns!
7
- nested_select allows the partial selection of the relations attributes during preloading process, leading to less RAM and CPU usage.
8
- Here is a benchmark output for a [gist I've created](https://gist.github.com/alekseyl/5d08782808a29df6813f16965f70228a) to emulate real-life example: displaying a course with its structure.
2
+ nested_select allows the partial selection of the relations attributes during preloading process,
3
+ leading to less RAM and CPU usage.
4
+ Here is a benchmark output for a [gist I've created](https://gist.github.com/alekseyl/5d08782808a29df6813f16965f70228a) to run real-life example:
5
+ displaying a course with its structure.
9
6
 
10
7
  Given:
11
- - Models are Course, Topic, Lesson.
8
+ - Models are: Course, Topic, Lesson.
12
9
  - Their relations has a following structure: course has_many topics, each topic has_many lessons.
13
10
  - To display a single course you need its structure, minimum data needed: topic and lessons titles and ordering.
14
11
 
15
- **Single course**, a real prod set of data used by current UI (~ x33 times less RAM):
12
+ **Single course**, a real example against production data and a real flow (~ x33 times less RAM):
16
13
 
17
14
  ```
18
15
  irb(main):216:0>compare_nested_select(ids, 1, silence_ar_logger_for_memory_profiling: false)
@@ -26,7 +23,7 @@ simple includes 0.209188 0.058340 0.267528 ( 0.903893)
26
23
  # partial selection
27
24
  D, [2025-01-12T19:08:36.163282 #503] DEBUG -- : Topic Load (4.1ms) SELECT "topics"."id", "topics"."position", "topics"."title", "topics"."course_id" FROM "topics" WHERE "topics"."deleted_at" IS NULL AND "topics"."course_id" = $1 [["course_id", 1624]]
28
25
  D, [2025-01-12T19:08:36.168803 #503] DEBUG -- : Lesson Load (3.9ms) SELECT "lessons"."id", "lessons"."title", "lessons"."topic_id", "lessons"."position", "lessons"."topic_id" FROM "lessons" WHERE "lessons"."deleted_at" IS NULL AND "lessons"."topic_id" = $1 [["topic_id", 7297]]
29
- # includes in full
26
+ # selects in full
30
27
  D, [2025-01-12T19:08:37.220379 #503] DEBUG -- : Topic Load (4.2ms) SELECT "topics"."id", "topics"."position", "topics"."title", "topics"."course_id" FROM "topics" WHERE "topics"."deleted_at" IS NULL AND "topics"."course_id" = $1 [["course_id", 1624]]
31
28
  D, [2025-01-12T19:08:37.247484 #503] DEBUG -- : Lesson Load (25.7ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."deleted_at" IS NULL AND "lessons"."topic_id" = $1 [["topic_id", 7297]]
32
29
 
@@ -41,8 +38,8 @@ RAM ratio improvements x33.54678126442086 on retain objects
41
38
  RAM ratio improvements x15.002820281285949 on total_allocated objects
42
39
  ```
43
40
 
44
- **100 courses**, this is kinda a synthetic example (there is no UI for multiple courses display with their structure)
45
- on the real prod data, but the bigger than needed collection (x7 faster):
41
+ **100 courses**, this is kinda a synthetic example since there is no UI for multiple courses
42
+ display together with their structures. It executed against the real production data. (nested select serves x7 faster):
46
43
 
47
44
  ```
48
45
  irb(main):280:0> compare_nested_select(ids, 100)
@@ -66,9 +63,8 @@ RAM ratio improvements x15.57707431190517 on retain objects
66
63
  RAM ratio improvements x11.836000856510193 on total_allocated objects
67
64
  ```
68
65
 
69
- Despite this little click bait it's pretty obvious that it might not be even the biggest numbers,
70
- if you have heavy relations instantiation for heavy views or reports generation,
71
- and you want it to be less demanding in RAM and CPU -- you should try nested_select
66
+ **Summary:** if you have CPU/RAM bottlenecks, heavy relations instantiation for heavy views or reports generation,
67
+ and you want it to be less demanding in RAM and CPU -- you should try nested_select.
72
68
 
73
69
  ## Installation
74
70
 
@@ -102,10 +98,11 @@ end
102
98
  User.includes(:profile).select(profile: :photo_url).limit(10)
103
99
  ```
104
100
 
105
- ### Partial through relations preloading
106
- Whenever you are using through relations between models rails will fully load all intermediate objects under the hood,
107
- that is definitely wastes lots of RAM, CPU including those on the DB side.
108
- You can limit through objects only to relation columns.
101
+ ### Partial preloading of through relations
102
+ Whenever you are using `through` relations between models and running preload,
103
+ then rails will fully load all intermediate objects under the hood!
104
+ That is definitely wastes lots of RAM, CPU including those on the DB side.
105
+ With nested_select you can apply selections to `through` relations.
109
106
  Ex:
110
107
 
111
108
  ```ruby
@@ -115,11 +112,11 @@ class User
115
112
  end
116
113
 
117
114
  # pay attention user_profile relation, wasn't included explicitly,
118
- # but still rails needed to be preloaded to match and preload avatars here
115
+ # but still rails needed them to be preloaded to be able to match and preload avatars
119
116
  user = User.includes(:avatars)
120
117
  .select(avatars: [:img_url, { user_profile: [:zip_code] }]).first
121
118
 
122
- # Now user - loaded fully
119
+ # user - loaded fully
123
120
  # avatars - foreign and primary keys needed to establish relations + img_url
124
121
  # user_profile - foreign and primary keys + zip_code
125
122
  ```
@@ -135,15 +132,24 @@ class User
135
132
  has_many :through_avatar_images, through: :avatars, class_name: :Image, source: :images
136
133
  end
137
134
 
138
- # only through_avatar_images is matter here, and we want everything
135
+ # only through_avatar_images is matter here, and we want everything else to be as small as possible
139
136
  user = User.includes(:through_avatar_images)
140
- .select(through_avatar_images: ["images.*", avatars: [user_profile: [:id]]]).first
137
+ .select(through_avatar_images: [avatars: [:id, user_profile: [:id]]]).first
141
138
 
142
139
  # through_avatar_images -- loaded in full
143
140
  # avatars, user_profile -- only relations columns id, user_profile_id e.t.c
144
141
  ```
145
- **REM** As for version 0.4.0 for the earliest relation in a through chain you need to select something,
146
- otherwise nested_select will select everything ))
142
+
143
+ **REM** There was an idea for through relations use a skinny approach: no nested attributes means,
144
+ only relation keys should be loaded:
145
+
146
+ ```ruby
147
+ user = User.includes(:through_avatar_images)
148
+ .select(through_avatar_images: [avatars: :user_profile]).first
149
+ ```
150
+ but that could be easily confused with normal flow behaviour, so I stick to basic default: no nested attributes,
151
+ means default behaviour, i.e. all attributes.
152
+
147
153
 
148
154
  # Safety
149
155
  How safe is the partial model loading? Earlier version of rails and activerecord would return nil in the case,
@@ -177,10 +183,6 @@ This needs a lot of monkey patching, and for now I decided not to go this way.
177
183
  That means in case when nesting selects based on belongs_to reflections,
178
184
  you'll need to select their foreign keys **EXPLICITLY!**
179
185
 
180
- ## will not work with ar_lazy_preload
181
- Right now it will not work with ar_lazy_preload gem. nested_select relies on the includes_values definition
182
- of a relation. If you are doing it in a lazy way, there weren't any explicit includes, that means it will not extract any nested selection.
183
-
184
186
  ```ruby
185
187
  class Avatar < ApplicationRecord
186
188
  belongs_to user
@@ -200,6 +202,10 @@ Image.includes(avatar: :user).select(avatar: [:size, { user: [:email] }]).load #
200
202
  Image.includes(avatar: :user).select(avatar: [:size, :user_id, { user: [:email] }]).load
201
203
  ```
202
204
 
205
+ ## will not work with ar_lazy_preload
206
+ Right now it will not work with ar_lazy_preload gem. nested_select relies on the includes_values definition
207
+ of a relation. If you are doing it in a lazy way, there weren't any explicit includes, that means it will not extract any nested selection.
208
+
203
209
  ## Testing
204
210
 
205
211
  ```bash
@@ -212,7 +218,7 @@ docker compose run test
212
218
  - [x] Ensure primary key will be added
213
219
  - [-] Ensure belongs_to will add a foreign_key column (Too hard to manage :(, its definitely not a low hanging fruit)
214
220
  - [x] Optimize through relations ( since they loading a whole set of attributes )
215
- - [ ] Separated rails version testing
221
+ - [x] Separated rails version testing
216
222
  - [x] Merge multiple nested selections
217
223
  - [x] Don't apply any selection if blank ( allows to limit only part of subselection tree)
218
224
  - [x] Allows to use custom attributes
data/docker-compose.yml CHANGED
@@ -10,4 +10,13 @@ services:
10
10
  - './test:/app/test'
11
11
  - './Gemfile:/app/Gemfile'
12
12
  - './Gemfile.lock:/app/Gemfile.lock'
13
+ - './gemfiles:/app/gemfiles'
13
14
 
15
+ appraisal:
16
+ build:
17
+ context: .
18
+ dockerfile: Dockerfile_apprsl
19
+ image: nested_select_appraisal
20
+ command: /bin/bash -c 'appraisal install && appraisal rake test'
21
+ volumes:
22
+ - '.:/app'
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 13.0"
6
+ gem "minitest", "~> 5.16"
7
+ gem "rubocop", "~> 1.21"
8
+ gem "rails", "~> 7.0.0"
9
+
10
+ gemspec path: "../"
@@ -0,0 +1,219 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ nested_select (0.4.4)
5
+ activerecord (>= 7)
6
+ activesupport (>= 7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (7.0.8.7)
12
+ actionpack (= 7.0.8.7)
13
+ activesupport (= 7.0.8.7)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailbox (7.0.8.7)
17
+ actionpack (= 7.0.8.7)
18
+ activejob (= 7.0.8.7)
19
+ activerecord (= 7.0.8.7)
20
+ activestorage (= 7.0.8.7)
21
+ activesupport (= 7.0.8.7)
22
+ mail (>= 2.7.1)
23
+ net-imap
24
+ net-pop
25
+ net-smtp
26
+ actionmailer (7.0.8.7)
27
+ actionpack (= 7.0.8.7)
28
+ actionview (= 7.0.8.7)
29
+ activejob (= 7.0.8.7)
30
+ activesupport (= 7.0.8.7)
31
+ mail (~> 2.5, >= 2.5.4)
32
+ net-imap
33
+ net-pop
34
+ net-smtp
35
+ rails-dom-testing (~> 2.0)
36
+ actionpack (7.0.8.7)
37
+ actionview (= 7.0.8.7)
38
+ activesupport (= 7.0.8.7)
39
+ rack (~> 2.0, >= 2.2.4)
40
+ rack-test (>= 0.6.3)
41
+ rails-dom-testing (~> 2.0)
42
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
+ actiontext (7.0.8.7)
44
+ actionpack (= 7.0.8.7)
45
+ activerecord (= 7.0.8.7)
46
+ activestorage (= 7.0.8.7)
47
+ activesupport (= 7.0.8.7)
48
+ globalid (>= 0.6.0)
49
+ nokogiri (>= 1.8.5)
50
+ actionview (7.0.8.7)
51
+ activesupport (= 7.0.8.7)
52
+ builder (~> 3.1)
53
+ erubi (~> 1.4)
54
+ rails-dom-testing (~> 2.0)
55
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
+ activejob (7.0.8.7)
57
+ activesupport (= 7.0.8.7)
58
+ globalid (>= 0.3.6)
59
+ activemodel (7.0.8.7)
60
+ activesupport (= 7.0.8.7)
61
+ activerecord (7.0.8.7)
62
+ activemodel (= 7.0.8.7)
63
+ activesupport (= 7.0.8.7)
64
+ activestorage (7.0.8.7)
65
+ actionpack (= 7.0.8.7)
66
+ activejob (= 7.0.8.7)
67
+ activerecord (= 7.0.8.7)
68
+ activesupport (= 7.0.8.7)
69
+ marcel (~> 1.0)
70
+ mini_mime (>= 1.1.0)
71
+ activesupport (7.0.8.7)
72
+ concurrent-ruby (~> 1.0, >= 1.0.2)
73
+ i18n (>= 1.6, < 2)
74
+ minitest (>= 5.1)
75
+ tzinfo (~> 2.0)
76
+ amazing_print (1.7.2)
77
+ appraisal (2.5.0)
78
+ bundler
79
+ rake
80
+ thor (>= 0.14.0)
81
+ ast (2.4.3)
82
+ base64 (0.2.0)
83
+ builder (3.3.0)
84
+ byebug (12.0.0)
85
+ concurrent-ruby (1.3.5)
86
+ crass (1.0.6)
87
+ date (3.4.1)
88
+ erubi (1.13.1)
89
+ globalid (1.2.1)
90
+ activesupport (>= 6.1)
91
+ i18n (1.14.7)
92
+ concurrent-ruby (~> 1.0)
93
+ json (2.11.3)
94
+ language_server-protocol (3.17.0.4)
95
+ lint_roller (1.1.0)
96
+ loofah (2.24.0)
97
+ crass (~> 1.0.2)
98
+ nokogiri (>= 1.12.0)
99
+ mail (2.8.1)
100
+ mini_mime (>= 0.1.1)
101
+ net-imap
102
+ net-pop
103
+ net-smtp
104
+ marcel (1.0.4)
105
+ method_source (1.1.0)
106
+ mini_mime (1.1.5)
107
+ minitest (5.25.5)
108
+ net-imap (0.5.8)
109
+ date
110
+ net-protocol
111
+ net-pop (0.1.2)
112
+ net-protocol
113
+ net-protocol (0.2.2)
114
+ timeout
115
+ net-smtp (0.5.1)
116
+ net-protocol
117
+ niceql (0.6.1)
118
+ nio4r (2.7.4)
119
+ nokogiri (1.18.8-x86_64-linux-gnu)
120
+ racc (~> 1.4)
121
+ parallel (1.27.0)
122
+ parser (3.3.8.0)
123
+ ast (~> 2.4.1)
124
+ racc
125
+ prism (1.4.0)
126
+ racc (1.8.1)
127
+ rack (2.2.13)
128
+ rack-test (2.2.0)
129
+ rack (>= 1.3)
130
+ rails (7.0.8.7)
131
+ actioncable (= 7.0.8.7)
132
+ actionmailbox (= 7.0.8.7)
133
+ actionmailer (= 7.0.8.7)
134
+ actionpack (= 7.0.8.7)
135
+ actiontext (= 7.0.8.7)
136
+ actionview (= 7.0.8.7)
137
+ activejob (= 7.0.8.7)
138
+ activemodel (= 7.0.8.7)
139
+ activerecord (= 7.0.8.7)
140
+ activestorage (= 7.0.8.7)
141
+ activesupport (= 7.0.8.7)
142
+ bundler (>= 1.15.0)
143
+ railties (= 7.0.8.7)
144
+ rails-dom-testing (2.2.0)
145
+ activesupport (>= 5.0.0)
146
+ minitest
147
+ nokogiri (>= 1.6)
148
+ rails-html-sanitizer (1.6.2)
149
+ loofah (~> 2.21)
150
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
151
+ rails-i18n (7.0.10)
152
+ i18n (>= 0.7, < 2)
153
+ railties (>= 6.0.0, < 8)
154
+ rails_sql_prettifier (7.0.7)
155
+ activerecord (>= 7)
156
+ niceql (~> 0.6)
157
+ railties (7.0.8.7)
158
+ actionpack (= 7.0.8.7)
159
+ activesupport (= 7.0.8.7)
160
+ method_source
161
+ rake (>= 12.2)
162
+ thor (~> 1.0)
163
+ zeitwerk (~> 2.5)
164
+ rainbow (3.1.1)
165
+ rake (13.2.1)
166
+ regexp_parser (2.10.0)
167
+ rubocop (1.75.4)
168
+ json (~> 2.3)
169
+ language_server-protocol (~> 3.17.0.2)
170
+ lint_roller (~> 1.1.0)
171
+ parallel (~> 1.10)
172
+ parser (>= 3.3.0.2)
173
+ rainbow (>= 2.2.2, < 4.0)
174
+ regexp_parser (>= 2.9.3, < 3.0)
175
+ rubocop-ast (>= 1.44.0, < 2.0)
176
+ ruby-progressbar (~> 1.7)
177
+ unicode-display_width (>= 2.4.0, < 4.0)
178
+ rubocop-ast (1.44.1)
179
+ parser (>= 3.3.7.2)
180
+ prism (~> 1.4)
181
+ rubocop-shopify (2.17.0)
182
+ rubocop (~> 1.62)
183
+ ruby-progressbar (1.13.0)
184
+ sqlite3 (1.7.3-x86_64-linux)
185
+ stubberry (0.3.0)
186
+ thor (1.3.2)
187
+ timeout (0.4.3)
188
+ tzinfo (2.0.6)
189
+ concurrent-ruby (~> 1.0)
190
+ unicode-display_width (3.1.4)
191
+ unicode-emoji (~> 4.0, >= 4.0.4)
192
+ unicode-emoji (4.0.4)
193
+ websocket-driver (0.7.7)
194
+ base64
195
+ websocket-extensions (>= 0.1.0)
196
+ websocket-extensions (0.1.5)
197
+ zeitwerk (2.7.2)
198
+
199
+ PLATFORMS
200
+ x86_64-linux-gnu
201
+
202
+ DEPENDENCIES
203
+ amazing_print
204
+ appraisal
205
+ bundler (>= 1.11)
206
+ byebug
207
+ minitest (~> 5.16)
208
+ nested_select!
209
+ rails (~> 7.0.0)
210
+ rails-i18n (>= 4)
211
+ rails_sql_prettifier
212
+ rake (~> 13.0)
213
+ rubocop (~> 1.21)
214
+ rubocop-shopify
215
+ sqlite3
216
+ stubberry
217
+
218
+ BUNDLED WITH
219
+ 2.6.8
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rake", "~> 13.0"
6
+ gem "minitest", "~> 5.16"
7
+ gem "rubocop", "~> 1.21"
8
+ gem "rails", "~> 7.1.3"
9
+
10
+ gemspec path: "../"