sequel-rails 1.0.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 246de7c0ab5e5b678e5cc36cbd9e4b8244e1e508
4
- data.tar.gz: 2a1e078efd0934a6c93ac02772c0fcef672fbf78
2
+ SHA256:
3
+ metadata.gz: ba61c0752e0e4ebeea5a6c4b01f6cf85f94c0413866c43dad74af0cfa694950a
4
+ data.tar.gz: 30ea0b29dd1ffbe0438f1b2ed1626a176146348aa4c820de51b85335eec8d4f0
5
5
  SHA512:
6
- metadata.gz: 8507eb62d41126c59d9cf089476d87cc3a6da8cb25c2794a47665a157274604f203b9c59542a8612cd5907fa77387c7555cd766bb38e7e0c2ea3d21b8b54bc6d
7
- data.tar.gz: 59e78d025928669a0d92b5b4fa5bbd4f017e5ca5a1805a6459ebf7d3db30be31c0a459c0c23313c5e04325f660a8efd7efa536eb5d6fb000dc46f7d18a86bfcd
6
+ metadata.gz: 1455a020956c332f8d91d65fd11678ca3701ca404cfd32adc760e1f753ab531c5e29433d3a1189d12a07c76a779feea1935913e7cc65ef518b85caf210a01dc6
7
+ data.tar.gz: d8be6934a488b6a4aff2eb6ca6b6007833fc9614439809a8a20a691c398dbd79768f3eb62711ad50c41e427e3ecd41f4845ca546590389f0a2860a2ba41a76e4
data/.travis.yml CHANGED
@@ -1,12 +1,15 @@
1
1
  language: ruby
2
2
  cache: bundler
3
- sudo: false
3
+ services:
4
+ - mysql
5
+ - postgresql
4
6
  rvm:
5
7
  - 2.2.9
6
- - 2.3.6
7
- - 2.4.3
8
- - 2.5.0
9
- - jruby-9.1.16.0
8
+ - 2.3.8
9
+ - 2.4.9
10
+ - 2.5.7
11
+ - 2.6.5
12
+ - jruby-9.2.8.0
10
13
  before_script:
11
14
  - mysql -e 'create database sequel_rails_test;'
12
15
  - mysql -e 'create database sequel_rails_test_mysql2;'
@@ -22,15 +25,30 @@ gemfile:
22
25
  - ci/rails-4.2.gemfile
23
26
  - ci/rails-5.0.gemfile
24
27
  - ci/rails-5.1.gemfile
28
+ - ci/rails-5.2.gemfile
25
29
  matrix:
26
30
  exclude:
27
- - rvm: 2.4.3
31
+ - rvm: 2.4.9
28
32
  gemfile: ci/rails-4.0.gemfile
29
- - rvm: 2.4.3
33
+ - rvm: 2.4.9
30
34
  gemfile: ci/rails-4.1.gemfile
31
- - rvm: 2.5.0
35
+ - rvm: 2.5.7
32
36
  gemfile: ci/rails-4.0.gemfile
33
- - rvm: 2.5.0
37
+ - rvm: 2.5.7
38
+ gemfile: ci/rails-4.1.gemfile
39
+ - rvm: 2.6.5
40
+ gemfile: ci/rails-4.0.gemfile
41
+ - rvm: 2.6.5
42
+ gemfile: ci/rails-4.1.gemfile
43
+ - rvm: 2.6.5
44
+ gemfile: ci/rails-4.2.gemfile
45
+ - rvm: 2.6.5
46
+ gemfile: ci/rails-5.0.gemfile
47
+ - rvm: 2.6.5
48
+ gemfile: ci/rails-5.1.gemfile
49
+ - rvm: jruby-9.2.8.0
50
+ gemfile: ci/rails-4.0.gemfile
51
+ - rvm: jruby-9.2.8.0
34
52
  gemfile: ci/rails-4.1.gemfile
35
53
  notifications:
36
54
  email:
data/History.md CHANGED
@@ -1,3 +1,15 @@
1
+ 1.1.0 (2019-10-29)
2
+ ==================
3
+
4
+ * Add test_connect option (p-leger)
5
+ [#165](https://github.com/TalentBox/sequel-rails/pull/165)
6
+ * Silence logger for sessions store (Michael Coyne)
7
+ [#171](https://github.com/TalentBox/sequel-rails/pull/171)
8
+ * Add db:sessions:clear and db:sessions:trim rake tasks (Michael Coyne)
9
+ [#170](https://github.com/TalentBox/sequel-rails/pull/170)
10
+ * Allow 'servers' to propagate to Sequel connection (Dustin Byrne)
11
+ [#169](https://github.com/TalentBox/sequel-rails/pull/169)
12
+
1
13
  1.0.1 (2018-02-27)
2
14
  ==================
3
15
 
data/README.md CHANGED
@@ -188,6 +188,10 @@ You can configure some options with the usual rails mechanism, in
188
188
  # This setting disabled the automatic connect after Rails init
189
189
  config.sequel.skip_connect = true
190
190
 
191
+ # Configure if Sequel should try to 'test' the database connection in order
192
+ # to fail early
193
+ config.sequel.test_connect = true
194
+
191
195
  # If you want to use a specific logger
192
196
  config.sequel.logger = MyLogger.new($stdout)
193
197
  ```
@@ -333,6 +337,8 @@ rake db:schema:load # Load a schema.rb file into the database
333
337
  rake db:seed # Load the seed data from db/seeds.rb
334
338
  rake db:setup # Create the database, load the schema, and initialize with the seed data
335
339
  rake db:test:prepare # Prepare test database (ensure all migrations ran, drop and re-create database then load schema). This task can be run in the same invocation as other task (eg: rake db:migrate db:test:prepare).
340
+ rake db:sessions:clear # Delete all sessions from the database
341
+ rake db:sessions:trim[threshold] # Delete all sessions older than `threshold` days (default to 30 days, eg: rake db:session:trim[10])
336
342
  ```
337
343
 
338
344
  Note on Patches/Pull Requests
@@ -403,6 +409,9 @@ Improvements have been made by those awesome contributors:
403
409
  * ckoenig (@ckoenig)
404
410
  * Rolf Timmermans (@rolftimmermans)
405
411
  * Olivier Lacan (@olivierlacan)
412
+ * Dustin Byrne (@dsbyrne)
413
+ * Michael Coyne (@mjc-gh)
414
+ * p-leger (@p-leger)
406
415
 
407
416
  Credits
408
417
  =======
data/ci/rails-4.2.gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 4.2.10'
3
+ gem 'railties', '~> 4.2.11'
4
4
 
5
5
  gemspec :path => '../'
6
6
 
data/ci/rails-5.0.gemfile CHANGED
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 5.0.6'
4
- gem 'activemodel', '~> 5.0.6'
3
+ gem 'railties', '~> 5.0.7'
4
+ gem 'activemodel', '~> 5.0.7'
5
5
 
6
6
  gemspec :path => '../'
7
7
 
data/ci/rails-5.1.gemfile CHANGED
@@ -1,8 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 5.1.4'
4
- gem 'activemodel', '~> 5.1.4'
5
- gem 'actionpack', '~> 5.1.4'
3
+ gem 'railties', '~> 5.1.6'
4
+ gem 'activemodel', '~> 5.1.6'
5
+ gem 'actionpack', '~> 5.1.6'
6
6
 
7
7
  gemspec :path => '../'
8
8
 
@@ -0,0 +1,28 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'railties', '~> 5.2.2'
4
+ gem 'activemodel', '~> 5.2.2'
5
+ gem 'actionpack', '~> 5.2.2'
6
+
7
+ gemspec :path => '../'
8
+
9
+ gem 'sequel', "#{ENV['SEQUEL']}"
10
+
11
+ gem 'fakefs', '0.11.2', :require => 'fakefs/safe'
12
+
13
+ # MRI/Rubinius Adapter Dependencies
14
+ platform :ruby do
15
+ gem 'pg'
16
+ if RUBY_VERSION < '2.4'
17
+ gem 'mysql'
18
+ end
19
+ gem 'mysql2'
20
+ gem 'sqlite3'
21
+ end
22
+
23
+ # JRuby Adapter Dependencies
24
+ platform :jruby do
25
+ gem 'jdbc-sqlite3'
26
+ gem 'jdbc-mysql'
27
+ gem 'jdbc-postgres'
28
+ end
@@ -26,13 +26,17 @@ module ActionDispatch
26
26
  end
27
27
 
28
28
  def get_session(env, sid)
29
- session = load_from_store(sid)
30
- env[SESSION_RECORD_KEY] = session
31
- [session.session_id, session.data]
29
+ with_silenced_logger do
30
+ session = load_from_store(sid)
31
+ env[SESSION_RECORD_KEY] = session
32
+ [session.session_id, session.data]
33
+ end
32
34
  end
33
35
 
34
36
  def write_session(req, sid, session_data, options)
35
- set_session(req.env, sid, session_data, options)
37
+ with_silenced_logger do
38
+ set_session(req.env, sid, session_data, options)
39
+ end
36
40
  end
37
41
 
38
42
  def set_session(env, sid, session_data, options)
@@ -42,7 +46,9 @@ module ActionDispatch
42
46
  end
43
47
 
44
48
  def delete_session(req, sid, options)
45
- destroy_session(req.env, sid, options)
49
+ with_silenced_logger do
50
+ destroy_session(req.env, sid, options)
51
+ end
46
52
  end
47
53
 
48
54
  def destroy_session(env, sid, options)
@@ -53,10 +59,12 @@ module ActionDispatch
53
59
  end
54
60
 
55
61
  def get_session_model(env, sid)
56
- if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
57
- env[SESSION_RECORD_KEY] = load_from_store(sid)
58
- else
59
- env[SESSION_RECORD_KEY] ||= load_from_store(sid)
62
+ with_silenced_logger do
63
+ if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
64
+ env[SESSION_RECORD_KEY] = load_from_store(sid)
65
+ else
66
+ env[SESSION_RECORD_KEY] ||= load_from_store(sid)
67
+ end
60
68
  end
61
69
  end
62
70
 
@@ -65,6 +73,10 @@ module ActionDispatch
65
73
  klass.where(:session_id => sid).first ||
66
74
  klass.new(:session_id => generate_sid, :data => {})
67
75
  end
76
+
77
+ def with_silenced_logger
78
+ Rails.application.config.sequel.logger.silence { yield }
79
+ end
68
80
  end
69
81
  end
70
82
  end
@@ -61,7 +61,7 @@ module Sequel
61
61
  end
62
62
 
63
63
  def validate_file_name!
64
- raise IllegalMigrationNameError file_name unless file_name =~ /^[_a-z0-9]+$/
64
+ raise IllegalMigrationNameError.new(file_name) unless file_name =~ /^[_a-z0-9]+$/
65
65
  end
66
66
  end
67
67
  end
@@ -23,7 +23,7 @@ module Sequel
23
23
  end
24
24
 
25
25
  def validate_file_name!
26
- raise IllegalMigrationNameError file_name unless file_name =~ /^[_a-z0-9]+$/
26
+ raise IllegalMigrationNameError, file_name unless file_name =~ /^[_a-z0-9]+$/
27
27
  end
28
28
  end
29
29
  end
@@ -28,6 +28,7 @@ module SequelRails
28
28
  self.load_database_tasks = true
29
29
  self.after_connect = nil
30
30
  self.skip_connect = nil
31
+ self.test_connect = true
31
32
  end
32
33
 
33
34
  def environment_for(name)
@@ -72,6 +73,8 @@ module SequelRails
72
73
 
73
74
  config['max_connections'] = max_connections if max_connections
74
75
  config['search_path'] = search_path if search_path
76
+ config['servers'] = servers if servers
77
+ config['test'] = test_connect
75
78
 
76
79
  url = ENV['DATABASE_URL']
77
80
  config['url'] ||= url if url
@@ -73,8 +73,8 @@ module SequelRails
73
73
  return URI::Generic.build(:scheme => adapter, :opaque => database)
74
74
  end
75
75
 
76
- # these four are handled separately
77
- params = cfg.reject { |k, _| %w(adapter host port database).include? k }
76
+ # these are handled separately
77
+ params = cfg.reject { |k, _| non_params.include? k }
78
78
 
79
79
  if (v = params['search_path'])
80
80
  # make sure there's no whitespace
@@ -96,6 +96,10 @@ module SequelRails
96
96
  :query => q
97
97
  )
98
98
  end
99
+
100
+ def non_params
101
+ %w(adapter host port database servers)
102
+ end
99
103
  end
100
104
  end
101
105
 
@@ -213,6 +213,23 @@ namespace sequel_rails_namespace do
213
213
  Rails.env = previous_env
214
214
  end
215
215
  end
216
+
217
+ namespace :sessions do
218
+ desc 'Clear the sessions table'
219
+ task clear: :environment do
220
+ db_for_current_env.from(:sessions).truncate
221
+ end
222
+
223
+ desc 'Trim old sessions from the table (default: > 30 days)'
224
+ task :trim, [:threshold] => :environment do |_, args|
225
+ cutoff_period = (args.fetch(:threshold) { 30 }).to_i.days.ago
226
+
227
+ db_for_current_env
228
+ .from(:sessions)
229
+ .where { updated_at < cutoff_period }
230
+ .delete
231
+ end
232
+ end
216
233
  end
217
234
 
218
235
  task 'test:prepare' => "#{sequel_rails_namespace}:test:prepare"
@@ -1,3 +1,3 @@
1
1
  module SequelRails
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -21,7 +21,7 @@ describe Sequel::Generators::MigrationGenerator do
21
21
  it 'refuses to generate migration with invalid filename' do
22
22
  expect do
23
23
  run_generator ['add_something:datetime']
24
- end.to raise_error
24
+ end.to raise_error(Sequel::IllegalMigrationNameError)
25
25
  end
26
26
 
27
27
  context 'when name starts with create' do
@@ -251,5 +251,13 @@ describe Sequel::Generators::MigrationGenerator do
251
251
  }
252
252
  end
253
253
  end
254
+
255
+ context 'when invalid migration name' do
256
+ it 'raises error' do
257
+ expect { run_generator ['invalid:file:name'] }
258
+ .to raise_error(Sequel::IllegalMigrationNameError)
259
+ .with_message("Illegal name for migration file: invalid:file:name (only lower case letters, numbers, and '_' allowed)")
260
+ end
261
+ end
254
262
  end
255
263
  end
@@ -11,7 +11,7 @@ describe Sequel::Generators::SessionMigrationGenerator do
11
11
  it 'refuses to generate migration with invalid filename' do
12
12
  expect do
13
13
  run_generator ['add:sessions']
14
- end.to raise_error
14
+ end.to raise_error(Sequel::IllegalMigrationNameError)
15
15
  end
16
16
 
17
17
  it 'creates a new migration for sessions table' do
@@ -96,6 +96,22 @@ describe SequelRails::Configuration do
96
96
  end
97
97
  end
98
98
 
99
+ describe '#test_connect' do
100
+ it 'defaults to true' do
101
+ expect(subject.test_connect).to be true
102
+ end
103
+
104
+ it 'can be assigned' do
105
+ subject.test_connect = true
106
+ expect(subject.test_connect).to be true
107
+ end
108
+
109
+ it 'can be set from merging another hash' do
110
+ subject.merge!(:test_connect => true)
111
+ expect(subject.test_connect).to be true
112
+ end
113
+ end
114
+
99
115
  describe '#connect' do
100
116
  let(:environments) do
101
117
  {
@@ -135,6 +151,22 @@ describe SequelRails::Configuration do
135
151
  context 'when stubbing SequelRails.jruby?' do
136
152
  before { allow(SequelRails).to receive(:jruby?).and_return(is_jruby) }
137
153
 
154
+ shared_examples 'test_connect' do
155
+ context 'test_connect' do
156
+ it 'passes the value' do
157
+ expect(::Sequel).to receive(:connect) do |hash_or_url, *_|
158
+ if hash_or_url.is_a? Hash
159
+ expect(hash_or_url[:test]).to eq true
160
+ else
161
+ expect(hash_or_url).to include('test=true')
162
+ end
163
+ end
164
+
165
+ subject.connect environment
166
+ end
167
+ end
168
+ end
169
+
138
170
  shared_examples 'max_connections' do
139
171
  context 'with max_connections=7 config option' do
140
172
  let(:max_connections) { 31_337 }
@@ -172,6 +204,44 @@ describe SequelRails::Configuration do
172
204
  end
173
205
  end
174
206
 
207
+ shared_examples 'servers' do
208
+ context 'servers' do
209
+ let(:servers) { { read_only: { host: 'replica' } } }
210
+
211
+ shared_examples 'passes the value only through options hash' do
212
+ it 'passes the value only through options hash' do
213
+ expect(::Sequel).to receive(:connect) do |hash_or_url, *_|
214
+ if hash_or_url.is_a? Hash
215
+ servers.keys.each do |k|
216
+ expect(hash_or_url[:servers][k]).to eq servers[k]
217
+ end
218
+ else
219
+ puts hash_or_url
220
+ expect(hash_or_url).not_to include('servers=')
221
+ end
222
+ end
223
+ subject.connect environment
224
+ end
225
+ end
226
+
227
+ context 'with servers set in config' do
228
+ before do
229
+ subject.servers = servers
230
+ end
231
+
232
+ include_examples 'passes the value only through options hash'
233
+ end
234
+
235
+ context 'with servers set in environment' do
236
+ before do
237
+ environments[environment]['servers'] = servers
238
+ end
239
+
240
+ include_examples 'passes the value only through options hash'
241
+ end
242
+ end
243
+ end
244
+
175
245
  shared_examples 'with DATABASE_URL in ENV' do
176
246
  let(:database_url) { 'adapter://user:pass@host/db' }
177
247
  def with_database_url_env
@@ -248,8 +318,10 @@ describe SequelRails::Configuration do
248
318
  let(:environment) { 'development' }
249
319
 
250
320
  context 'in C-Ruby' do
321
+ include_examples 'test_connect'
251
322
  include_examples 'max_connections'
252
323
  include_examples 'search_path'
324
+ include_examples 'servers'
253
325
  include_examples 'with DATABASE_URL in ENV'
254
326
 
255
327
  let(:is_jruby) { false }
@@ -263,8 +335,10 @@ describe SequelRails::Configuration do
263
335
  end
264
336
 
265
337
  context 'in JRuby' do
338
+ include_examples 'test_connect'
266
339
  include_examples 'max_connections'
267
340
  include_examples 'search_path'
341
+ include_examples 'servers'
268
342
  include_examples 'with DATABASE_URL in ENV'
269
343
 
270
344
  let(:is_jruby) { true }
@@ -296,7 +370,9 @@ describe SequelRails::Configuration do
296
370
  let(:environment) { 'remote' }
297
371
 
298
372
  context 'in C-Ruby' do
373
+ include_examples 'test_connect'
299
374
  include_examples 'max_connections'
375
+ include_examples 'servers'
300
376
  include_examples 'with DATABASE_URL in ENV'
301
377
 
302
378
  let(:is_jruby) { false }
@@ -310,7 +386,9 @@ describe SequelRails::Configuration do
310
386
  end
311
387
 
312
388
  context 'in JRuby' do
389
+ include_examples 'test_connect'
313
390
  include_examples 'max_connections'
391
+ include_examples 'servers'
314
392
  include_examples 'with DATABASE_URL in ENV'
315
393
 
316
394
  let(:is_jruby) { true }
@@ -123,7 +123,7 @@ describe SequelRails::Railtie do
123
123
  it 'initializing the application fails' do
124
124
  expect do
125
125
  configure_sequel!
126
- end.to raise_error
126
+ end.to raise_error RuntimeError, "Database not configured.\nPlease create config/database.yml or set DATABASE_URL in environment."
127
127
  end
128
128
  end
129
129
  end
@@ -99,4 +99,46 @@ describe 'Database rake tasks', :no_transaction => true do
99
99
  end
100
100
  end
101
101
  end
102
+
103
+ describe 'db:sessions:clear' do
104
+ let(:sessions) { Sequel::Model.db.from(:sessions) }
105
+
106
+ after { sessions.delete }
107
+
108
+ it 'truncates sessions table' do
109
+ sessions.insert session_id: 'foo', data: ''
110
+ sessions.insert session_id: 'bar', data: ''
111
+
112
+ Dir.chdir app_root do
113
+ expect { `rake db:sessions:clear` }.to change { sessions.count }.by(-2)
114
+ end
115
+ end
116
+ end
117
+
118
+ describe 'db:sessions:trim' do
119
+ let(:sessions) { Sequel::Model.db.from(:sessions) }
120
+
121
+ before do
122
+ sessions.insert session_id: 'foo', data: '', updated_at: (Date.today - 60).to_time
123
+ sessions.insert session_id: 'bar', data: '', updated_at: Date.today.to_time
124
+ end
125
+
126
+ after { sessions.delete }
127
+
128
+ it 'delete sessions before cutoff' do
129
+ Dir.chdir app_root do
130
+ expect { `rake db:sessions:trim` }.to change { sessions.count }.by(-1)
131
+ end
132
+ end
133
+
134
+ context 'with threshold' do
135
+ it 'delete sessions before cutoff' do
136
+ sessions.insert session_id: 'baz', data: '', updated_at: (Date.today - 44).to_time
137
+
138
+ Dir.chdir app_root do
139
+ expect { `rake db:sessions:trim[45]` }.to change { sessions.count }.by(-1)
140
+ end
141
+ end
142
+ end
143
+ end
102
144
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brasten Sager (brasten)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-02-27 00:00:00.000000000 Z
12
+ date: 2019-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -215,6 +215,7 @@ files:
215
215
  - ci/rails-4.2.gemfile
216
216
  - ci/rails-5.0.gemfile
217
217
  - ci/rails-5.1.gemfile
218
+ - ci/rails-5.2.gemfile
218
219
  - config.ru
219
220
  - lib/action_dispatch/middleware/session/sequel_store.rb
220
221
  - lib/generators/sequel.rb
@@ -298,8 +299,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
298
299
  - !ruby/object:Gem::Version
299
300
  version: 1.8.11
300
301
  requirements: []
301
- rubyforge_project:
302
- rubygems_version: 2.6.14
302
+ rubygems_version: 3.0.3
303
303
  signing_key:
304
304
  specification_version: 4
305
305
  summary: Use Sequel with Rails (3.x and 4.x)