otr-activerecord 1.3.1 → 2.0.1

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: be72e7cf2d04b19487c2d747929254323747a8c06e42374814a6ecbe8bc935a2
4
- data.tar.gz: f3e1bfa5b32199891de46dbd2a9a1d633d2512c53c5d6b20fd670dc4db3b93ee
3
+ metadata.gz: 9d318dcf63df4adf2773ecaea8663db39502c2a423e2367a1a0dc9552f277563
4
+ data.tar.gz: ea0483713e317f4eb0b1b4e45099e9db3de30098b207b7adf64a2d29a7370616
5
5
  SHA512:
6
- metadata.gz: 6ba9aa0f880717dac8b2eefbcc40bd2089240b60f9f1227be38b845f14180beb39e760c46d63c614cfd6f3822198724f3275dd1fe35cbc057bd307a411ef10be
7
- data.tar.gz: 263e3f0dde25f81beeecdc746951ee007470dc96e69fc869bde84d2557e150c50589d7df6c3d68ec7245fae0d221b62b9e6e905510b2182c96e924433b3251ef
6
+ metadata.gz: 8afe4f886901046296b816c50ecad5637280afbbf077a53a77c1f74a5da2073c8ba91c353fe8cf5df5239c03a5a955ca5ca13b9dd147c729d1dafe7b94f5b652
7
+ data.tar.gz: 412060fba9c524ccb59acd56f13cbe8d26fcc4f0662377dadc25663b22b3954b11f10a5a8d0593e12e9fbe11e0b6b884cb0c8ce7776d9ea529147d7fbd878277
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
6
- * ActiveRecord 5
7
5
  * ActiveRecord 6
6
+ * ActiveRecord 5
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:
27
29
 
28
- This middleware cleans up your database connections after each request. Add it to your `config.ru` file:
30
+ OTR::ActiveRecord.establish_connection!
29
31
 
32
+ If you're using multiple databases, call your base class(es) instead:
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,12 +26,32 @@ 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"
33
32
  def self.configure_from_url!(url)
34
- configure_from_hash! ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash
33
+ require 'uri'
34
+ uri = URI(url)
35
+ spec = {"adapter" => uri.scheme}
36
+
37
+ case spec["adapter"]
38
+ when /^sqlite/i
39
+ spec["database"] = url =~ /::memory:/ ? ":memory:" : "#{uri.host}#{uri.path}"
40
+ else
41
+ spec["host"] = uri.host if uri.host
42
+ spec["port"] = uri.port if uri.port
43
+ spec["database"] = uri.path.sub(/^\//, "")
44
+ spec["username"] = uri.user if uri.user
45
+ spec["password"] = uri.password if uri.password
46
+ end
47
+
48
+ if uri.query
49
+ opts_ary = URI.decode_www_form(uri.query)
50
+ opts = Hash[opts_ary]
51
+ spec.merge!(opts)
52
+ end
53
+
54
+ configure_from_hash! spec
35
55
  end
36
56
 
37
57
  # Connect to database with a yml file. Example: "config/database.yml"
@@ -40,10 +60,20 @@ module OTR
40
60
  ::ActiveRecord::Base.configurations =
41
61
  (YAML.load(ERB.new(File.read(path)).result) || {}).
42
62
  reduce({}) { |a, (env, config)|
43
- a[env] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge config
63
+ if config.has_key? "database"
64
+ a[env] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge config
65
+ elsif env == rack_env.to_s
66
+ config.each do |dbname, subconfig|
67
+ a[dbname.to_sym] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge subconfig
68
+ end
69
+ end
44
70
  a
45
71
  }
46
- ::ActiveRecord::Base.establish_connection(rack_env)
72
+ end
73
+
74
+ # Establish a connection to the given db (defaults to current rack env)
75
+ def self.establish_connection!(db = rack_env)
76
+ ::ActiveRecord::Base.establish_connection(db)
47
77
  end
48
78
 
49
79
  # The current Rack environment
@@ -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.1'
4
+ VERSION = '2.0.1'
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.1
4
+ version: 2.0.1
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-05-03 00:00:00.000000000 Z
11
+ date: 2021-06-24 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.2
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!'