otr-activerecord 1.3.0 → 2.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
  SHA256:
3
- metadata.gz: 8e901d95b96b53e3558c490bde1012d91a55b86db6ef1ad2158104c7c339c086
4
- data.tar.gz: f0eb40274e92d6b61f1802c8344c80db158727df3e227bb415d083b2138d18dc
3
+ metadata.gz: b7cab0f2557ace7ed091eb02c7090a595d0cfb586313b9f03b4727172e9fd670
4
+ data.tar.gz: 5dcc6d2ee8e6d99612e6999549c944820b7485735947e80ee953498f2c5cf829
5
5
  SHA512:
6
- metadata.gz: d4ce3da9776d7c3771ef4f62fca66618036630cdc54ad3487bedf8b3901f2b224f6fba0124c3407b17ff3a11ac169e30ed9642b2b41c5865638db2a45901d9b2
7
- data.tar.gz: 845f66293799de390aea7c7ad731f02a7ffb050a4219c45f4af7c60073f25fe16b01ebab450ed7abb2cd238285116728ad2b9e0ddc4b912f4ab6d1ab9b67435d
6
+ metadata.gz: 71561f7bf678621e8061be51fd5a833c47ac219eb7f7ba010f8a210ba45c108d3f389ab743b87ab4bbc9d6f6aba95bfbd1694bb6f6c9319fa6e17880cd9fa54f
7
+ data.tar.gz: 2c596c55ae278c66b5081a4d06aabfa43f83e8fd162675c2517299f120e231dd56f8d91a7fa0e7d1d7d0e7390022c825db71d5e71013b4d68b98b8daaf89ce86
data/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  An easy way to use ActiveRecord "off the rails." Works with Grape, Sinatra, plain old Rack, or even in a boring little script!. The defaults are all very Railsy (`config/database.yml`, `db/seeds.rb`, `db/migrate`, etc.), but you can easily change them. (Formerly known as `grape-activerecord`.) Supports:
4
4
 
5
- * ActiveRecord 4
5
+ * ActiveRecord 6
6
6
  * ActiveRecord 5
7
- * ActiveRecord 6 (experimental)
7
+ * ActiveRecord 4
8
8
 
9
9
  ## How to use
10
10
 
@@ -23,13 +23,29 @@ After loading your gems, tell `OTR::ActiveRecord` about your database config usi
23
23
  **Important note**: `configure_from_file!` won't work as expected if you have already `DATABASE_URL` set as part of your environment variables.
24
24
  This is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration.
25
25
 
26
- #### 3. Enable ActiveRecord connection management in Rack apps
26
+ #### 3. Connect to your database(s)
27
+
28
+ If you have a single database (most apps), use this helper:
29
+
30
+ OTR::ActiveRecord.establish_connection!
27
31
 
28
- This middleware cleans up your database connections after each request. Add it to your `config.ru` file:
32
+ If you're using multiple databases, call your base class(es) instead:
29
33
 
34
+ MyBase.establish_connection :primary
35
+ MyBase.establish_connection :primary_replica
36
+ ...
37
+
38
+ #### 4. Enable middleware for Rack apps
39
+
40
+ Add these middlewares in `config.ru`:
41
+
42
+ # Clean up database connections after every request (required)
30
43
  use OTR::ActiveRecord::ConnectionManagement
31
44
 
32
- #### 4. Import ActiveRecord tasks into your Rakefile
45
+ # Enable ActiveRecord's QueryCache for every request (optional)
46
+ use OTR::ActiveRecord::QueryCache
47
+
48
+ #### 5. Import ActiveRecord tasks into your Rakefile
33
49
 
34
50
  This will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`.
35
51
 
@@ -2,5 +2,6 @@ require 'active_record'
2
2
  require 'hashie-forbidden_attributes'
3
3
  require 'otr-activerecord/version'
4
4
  require 'otr-activerecord/activerecord'
5
- require 'otr-activerecord/middleware/connection_management.rb'
5
+ require 'otr-activerecord/middleware/connection_management'
6
+ require 'otr-activerecord/middleware/query_cache'
6
7
  require 'otr-activerecord/defaults'
@@ -26,7 +26,6 @@ module OTR
26
26
  def self.configure_from_hash!(spec)
27
27
  config = spec.stringify_keys.merge("migrations_paths" => ::OTR::ActiveRecord.migrations_paths)
28
28
  ::ActiveRecord::Base.configurations = {rack_env.to_s => config}
29
- ::ActiveRecord::Base.establish_connection(rack_env)
30
29
  end
31
30
 
32
31
  # Connect to database with a DB URL. Example: "postgres://user:pass@localhost/db"
@@ -40,10 +39,20 @@ module OTR
40
39
  ::ActiveRecord::Base.configurations =
41
40
  (YAML.load(ERB.new(File.read(path)).result) || {}).
42
41
  reduce({}) { |a, (env, config)|
43
- a[env] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge config
42
+ if config.has_key? "database"
43
+ a[env] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge config
44
+ elsif env == rack_env.to_s
45
+ config.each do |dbname, subconfig|
46
+ a[dbname.to_sym] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge subconfig
47
+ end
48
+ end
44
49
  a
45
50
  }
46
- ::ActiveRecord::Base.establish_connection(rack_env)
51
+ end
52
+
53
+ # Establish a connection to the given db (defaults to current rack env)
54
+ def self.establish_connection!(db = rack_env)
55
+ ::ActiveRecord::Base.establish_connection(db)
47
56
  end
48
57
 
49
58
  # The current Rack environment
@@ -34,10 +34,3 @@ module OTR
34
34
  end
35
35
  end
36
36
  end
37
-
38
- # https://github.com/rails/rails/issues/35902
39
- module ActiveRecord::Tasks::DatabaseTasks
40
- def for_each
41
- return
42
- end
43
- end
@@ -0,0 +1,33 @@
1
+ module OTR
2
+ module ActiveRecord
3
+ #
4
+ # Rack middleware to enable ActiveRecord's query cache for each request.
5
+ #
6
+ class QueryCache
7
+ def initialize(app)
8
+ @handler = case ::ActiveRecord::VERSION::MAJOR
9
+ when 4 then ::ActiveRecord::QueryCache.new(app)
10
+ when 5, 6 then ActionDispatchHandler.new(app)
11
+ end
12
+ end
13
+
14
+ def call(env)
15
+ @handler.call(env)
16
+ end
17
+
18
+ class ActionDispatchHandler
19
+ def initialize(app)
20
+ @app = app
21
+ end
22
+
23
+ def call(env)
24
+ state = nil
25
+ state = ::ActiveRecord::QueryCache.run
26
+ @app.call(env)
27
+ ensure
28
+ ::ActiveRecord::QueryCache.complete(state) if state
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,6 +1,6 @@
1
1
  module OTR
2
2
  module ActiveRecord
3
3
  # Gem version
4
- VERSION = '1.3.0'
4
+ VERSION = '2.0.0'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: otr-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-08 00:00:00.000000000 Z
11
+ date: 2021-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '4.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.1'
22
+ version: '6.3'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '4.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.1'
32
+ version: '6.3'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: hashie-forbidden_attributes
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +60,7 @@ files:
60
60
  - lib/otr-activerecord/compatibility_6.rb
61
61
  - lib/otr-activerecord/defaults.rb
62
62
  - lib/otr-activerecord/middleware/connection_management.rb
63
+ - lib/otr-activerecord/middleware/query_cache.rb
63
64
  - lib/otr-activerecord/version.rb
64
65
  - lib/tasks/otr-activerecord.rake
65
66
  homepage: https://github.com/jhollinger/otr-activerecord
@@ -81,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.7.6
85
+ rubygems_version: 3.0.3
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: 'Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!'