chaotic_order 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 220de14116403b35c368efe5aaf9cb436820fddf6775022d935ccc1db0078c34
4
+ data.tar.gz: 68c6559a4ff1dd7eecd696990e96329e0d9697c03c3b69c07bcba0dbde98fe57
5
+ SHA512:
6
+ metadata.gz: 629d7d0be79fb53b14c058c0eb5e84f20e5eb07c661f169aa205a6f84201370303eded527922c4ff05e5517a425d57aff1268c3209963775e604e2551387b1ed
7
+ data.tar.gz: 549e41e62010cc51c9868bff1824d17e97fdd08c5fec28f34bc29b9b522397db677a8f10df61fe37b438d17defe5eea3942e9b25a0adfd162dbd1e838c1e16f5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Santiago Bartesaghi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # ChaoticOrder
2
+
3
+ Chaotic Order helps you detect queries that are missing an order.
4
+
5
+ Sometimes we forget to add an order to our queries, or we don't add it because we assume that by default they will be returned ordered by `id`, but that is not guaranteed by the DBMS. By forcing a randomic order to the unordered queries, we can help you detect them more easily.
6
+
7
+ This gem can be useful helping detect intermittent tests that otherwise are hard to replicate.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ group :development, :test do
15
+ gem 'chaotic_order'
16
+ end
17
+ ```
18
+
19
+ Note that this gem is not recommended to be used in production.
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install chaotic_order
28
+
29
+ ## Usage
30
+
31
+ By default the gem is turned off, and there are two ways to turn it on:
32
+ 1. By setting an environment variable `CHAOTIC_ORDER=true`
33
+ 1. By calling `ChaoticOrder::Config.enable!` in a initializer
34
+
35
+ In either option, make sure that you are only enabling it in development and/or test environments, depending on your needs.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/santib/chaotic_order. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/santib/chaotic_order/blob/master/CODE_OF_CONDUCT.md).
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
50
+
51
+ ## Code of Conduct
52
+
53
+ Everyone interacting in the ChaoticOrder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/santib/chaotic_order/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChaoticOrder
4
+ class Config
5
+ def self.enable!
6
+ @enabled = true
7
+ end
8
+
9
+ def self.disable!
10
+ @enabled = false
11
+ end
12
+
13
+ def self.enabled?
14
+ !!@enabled
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChaoticOrder
4
+ class Manager
5
+ attr_reader :arel, :adapter_name
6
+
7
+ def initialize(arel, adapter_name)
8
+ @arel = arel
9
+ @adapter_name = adapter_name
10
+ end
11
+
12
+ def set_random_order
13
+ return unless set_random_order?
14
+
15
+ arel.order(random_for_adapter)
16
+ end
17
+
18
+ private
19
+
20
+ def set_random_order?
21
+ !order_set? && !distinct_set?
22
+ end
23
+
24
+ def order_set?
25
+ arel.orders.present?
26
+ end
27
+
28
+ def distinct_set?
29
+ arel.to_sql =~ /^SELECT DISTINCT/i
30
+ end
31
+
32
+ def random_for_adapter
33
+ if adapter_name =~ /mysql/i
34
+ "RAND()"
35
+ else
36
+ "RANDOM()"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChaoticOrder
4
+ class Railtie < Rails::Railtie
5
+ config.to_prepare do
6
+ if ChaoticOrder::Config.enabled? || ENV["CHAOTIC_ORDER"] == "true"
7
+ ::ActiveRecord::Relation.prepend(
8
+ Module.new do
9
+ def exec_queries(*)
10
+ ChaoticOrder::Manager.new(arel, connection.adapter_name).set_random_order
11
+ super
12
+ end
13
+ end
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChaoticOrder
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require_relative "chaotic_order/config"
5
+ require_relative "chaotic_order/manager"
6
+ require_relative "chaotic_order/railtie"
7
+ require_relative "chaotic_order/version"
8
+
9
+ module ChaoticOrder
10
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chaotic_order
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Santiago Bartesaghi
8
+ - Martin Jaime
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2021-12-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '5.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '5.0'
28
+ description:
29
+ email:
30
+ - santib@hey.com
31
+ - martinmoron7@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE.txt
37
+ - README.md
38
+ - lib/chaotic_order.rb
39
+ - lib/chaotic_order/config.rb
40
+ - lib/chaotic_order/manager.rb
41
+ - lib/chaotic_order/railtie.rb
42
+ - lib/chaotic_order/version.rb
43
+ homepage: https://github.com/santib/chaotic_order
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/santib/chaotic_order
48
+ source_code_uri: https://github.com/santib/chaotic_order
49
+ bug_tracker_uri: https://github.com/santib/chaotic_order/issues
50
+ changelog_uri: https://github.com/santib/chaotic_order/releases
51
+ rubygems_mfa_required: 'true'
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.5.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.1.2
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Add random order to queries that do not have a specific order
71
+ test_files: []