otr-activerecord 1.4.2 → 2.0.3

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: c8dad01fcadb48149aac6d50f0e66507bab5bb35bfa42af74d5c4f35a6706ef4
4
- data.tar.gz: 2abf82a804042bfb986cef869c7be5f700c9d4ed6a5e952213608706440c7eac
3
+ metadata.gz: 304d3cdd120ed69673c1d19b4be9fc6398929ba82c9fe3a8999815177a84ae98
4
+ data.tar.gz: 4ce504f5faa3ceb7ea4145dc4205e41a0ac3484409ef51253c278ce61ffb1f3e
5
5
  SHA512:
6
- metadata.gz: fe19cf7af34c499bfd4d1811f8cc40d1e62e2f5df42f3ad47568e412864491aaa2dc709076ccfe5007509d9ffd9a94ba994e4cfbfc778eef52f89b6dc392c90f
7
- data.tar.gz: 4404dd0fe82faf3d813b3ff767a2d53219ea339dbd5f5e416f115f3b4a0cb4dacb3dff75885a093a5025aece1b3d5d17e8a77d3f1f4cf1d67e8e8e929dfb11e8
6
+ metadata.gz: 1327e816c901a33452b6f76a0ec362af3e9f7fd454dca9b522672acebe911b59e4f7ef2e20dbe566b4085c100bae9df392985ad7db0d5ace866d13392d942c8f
7
+ data.tar.gz: 64d38211a199cca1c93f5e1da576e69c17fbdda4333b6cf6e595027f851a478bf63b5c77ca6b9bf918c6da1418187fdebd2feec1e835a8e03179267bb6af4ae5
data/README.md CHANGED
@@ -2,6 +2,7 @@
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 7.0.0.alpha2 (new AR features, like encryption, not tested)
5
6
  * ActiveRecord 6
6
7
  * ActiveRecord 5
7
8
  * ActiveRecord 4
@@ -23,7 +24,19 @@ After loading your gems, tell `OTR::ActiveRecord` about your database config usi
23
24
  **Important note**: `configure_from_file!` won't work as expected if you have already `DATABASE_URL` set as part of your environment variables.
24
25
  This is because in ActiveRecord when that env variable is set it will merge its properties into the current connection configuration.
25
26
 
26
- #### 3. Enable middleware for Rack apps
27
+ #### 3. Connect to your database(s)
28
+
29
+ If you have a single database (most apps), use this helper:
30
+
31
+ OTR::ActiveRecord.establish_connection!
32
+
33
+ If you're using multiple databases, call your base class(es) instead:
34
+
35
+ MyBase.establish_connection :primary
36
+ MyBase.establish_connection :primary_replica
37
+ ...
38
+
39
+ #### 4. Enable middleware for Rack apps
27
40
 
28
41
  Add these middlewares in `config.ru`:
29
42
 
@@ -33,7 +46,7 @@ Add these middlewares in `config.ru`:
33
46
  # Enable ActiveRecord's QueryCache for every request (optional)
34
47
  use OTR::ActiveRecord::QueryCache
35
48
 
36
- #### 4. Import ActiveRecord tasks into your Rakefile
49
+ #### 5. Import ActiveRecord tasks into your Rakefile
37
50
 
38
51
  This will give you most of the standard `db:` tasks you get in Rails. Add it to your `Rakefile`.
39
52
 
@@ -7,6 +7,7 @@ module OTR
7
7
  autoload :Compatibility4, 'otr-activerecord/compatibility_4'
8
8
  autoload :Compatibility5, 'otr-activerecord/compatibility_5'
9
9
  autoload :Compatibility6, 'otr-activerecord/compatibility_6'
10
+ autoload :Compatibility7, 'otr-activerecord/compatibility_7'
10
11
 
11
12
  class << self
12
13
  # Relative path to the "db" dir
@@ -26,24 +27,57 @@ module OTR
26
27
  def self.configure_from_hash!(spec)
27
28
  config = spec.stringify_keys.merge("migrations_paths" => ::OTR::ActiveRecord.migrations_paths)
28
29
  ::ActiveRecord::Base.configurations = {rack_env.to_s => config}
29
- ::ActiveRecord::Base.establish_connection(rack_env)
30
30
  end
31
31
 
32
32
  # Connect to database with a DB URL. Example: "postgres://user:pass@localhost/db"
33
33
  def self.configure_from_url!(url)
34
- configure_from_hash! ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(url).to_hash
34
+ require 'uri'
35
+ uri = URI(url)
36
+ spec = {"adapter" => uri.scheme}
37
+
38
+ case spec["adapter"]
39
+ when /^sqlite/i
40
+ spec["database"] = url =~ /::memory:/ ? ":memory:" : "#{uri.host}#{uri.path}"
41
+ else
42
+ spec["host"] = uri.host if uri.host
43
+ spec["port"] = uri.port if uri.port
44
+ spec["database"] = uri.path.sub(/^\//, "")
45
+ spec["username"] = uri.user if uri.user
46
+ spec["password"] = uri.password if uri.password
47
+ end
48
+
49
+ if uri.query
50
+ opts_ary = URI.decode_www_form(uri.query)
51
+ opts = Hash[opts_ary]
52
+ spec.merge!(opts)
53
+ end
54
+
55
+ configure_from_hash! spec
35
56
  end
36
57
 
37
58
  # Connect to database with a yml file. Example: "config/database.yml"
38
59
  def self.configure_from_file!(path)
39
60
  raise "#{path} does not exist!" unless File.file? path
40
- ::ActiveRecord::Base.configurations =
41
- (YAML.load(ERB.new(File.read(path)).result) || {}).
42
- reduce({}) { |a, (env, config)|
43
- a[env] = {"migrations_paths" => ::OTR::ActiveRecord.migrations_paths}.merge config
44
- a
45
- }
46
- ::ActiveRecord::Base.establish_connection(rack_env)
61
+ result =(YAML.safe_load(ERB.new(File.read(path)).result, [], [], true) || {})
62
+ ::ActiveRecord::Base.configurations = begin
63
+ result.each do |_env, config|
64
+ if config.all? { |_, v| v.is_a?(Hash) }
65
+ config.each { |_, v| append_migration_path(v) }
66
+ else
67
+ append_migration_path(config)
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ # Establish a connection to the given db (defaults to current rack env)
74
+ def self.establish_connection!(db = rack_env)
75
+ ::ActiveRecord::Base.establish_connection(db)
76
+ end
77
+
78
+ def self.append_migration_path(config)
79
+ config['migrations_paths'] = ::OTR::ActiveRecord.migrations_paths unless config.key?('migrations_paths')
80
+ config
47
81
  end
48
82
 
49
83
  # The current Rack environment
@@ -23,7 +23,7 @@ module OTR
23
23
 
24
24
  # Basename of migration classes
25
25
  def migration_base_class_name
26
- version = "6.#{::ActiveRecord::VERSION::MINOR}"
26
+ version = "#{@major_version}.#{::ActiveRecord::VERSION::MINOR}"
27
27
  "ActiveRecord::Migration[#{version}]"
28
28
  end
29
29
 
@@ -0,0 +1,12 @@
1
+ module OTR
2
+ module ActiveRecord
3
+ # Compatibility layer for ActiveRecord 7
4
+ class Compatibility7 < Compatibility6
5
+ def initialize
6
+ @major_version = 7
7
+ ::ActiveRecord.default_timezone = :utc
8
+ ::ActiveRecord::Base.logger = Logger.new(STDOUT)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -4,8 +4,10 @@ OTR::ActiveRecord.db_dir = 'db'
4
4
  OTR::ActiveRecord.migrations_paths = %w(db/migrate)
5
5
  OTR::ActiveRecord.fixtures_path = 'test/fixtures'
6
6
  OTR::ActiveRecord.seed_file = 'seeds.rb'
7
- OTR::ActiveRecord._normalizer = case ::ActiveRecord::VERSION::MAJOR
8
- when 4 then OTR::ActiveRecord::Compatibility4.new
9
- when 5 then OTR::ActiveRecord::Compatibility5.new
10
- when 6 then OTR::ActiveRecord::Compatibility6.new
11
- end
7
+ OTR::ActiveRecord._normalizer =
8
+ case ::ActiveRecord::VERSION::MAJOR
9
+ when 4 then OTR::ActiveRecord::Compatibility4.new
10
+ when 5 then OTR::ActiveRecord::Compatibility5.new
11
+ when 6 then OTR::ActiveRecord::Compatibility6.new
12
+ when 7 then OTR::ActiveRecord::Compatibility7.new
13
+ end
@@ -1,6 +1,6 @@
1
1
  module OTR
2
2
  module ActiveRecord
3
3
  # Gem version
4
- VERSION = '1.4.2'
4
+ VERSION = '2.0.3'
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.4.2
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-03 00:00:00.000000000 Z
11
+ date: 2021-10-22 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.3'
22
+ version: '7.1'
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.3'
32
+ version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: hashie-forbidden_attributes
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -58,6 +58,7 @@ files:
58
58
  - lib/otr-activerecord/compatibility_4.rb
59
59
  - lib/otr-activerecord/compatibility_5.rb
60
60
  - lib/otr-activerecord/compatibility_6.rb
61
+ - lib/otr-activerecord/compatibility_7.rb
61
62
  - lib/otr-activerecord/defaults.rb
62
63
  - lib/otr-activerecord/middleware/connection_management.rb
63
64
  - lib/otr-activerecord/middleware/query_cache.rb
@@ -67,7 +68,7 @@ homepage: https://github.com/jhollinger/otr-activerecord
67
68
  licenses:
68
69
  - MIT
69
70
  metadata: {}
70
- post_install_message:
71
+ post_install_message:
71
72
  rdoc_options: []
72
73
  require_paths:
73
74
  - lib
@@ -82,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
84
85
  requirements: []
85
- rubygems_version: 3.0.3
86
- signing_key:
86
+ rubygems_version: 3.1.6
87
+ signing_key:
87
88
  specification_version: 4
88
89
  summary: 'Off The Rails: Use ActiveRecord with Grape, Sinatra, Rack, or anything else!'
89
90
  test_files: []