sequel-rails 1.0.1 → 1.2.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.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +121 -0
  3. data/.rubocop.yml +9 -3
  4. data/.rubocop_todo.yml +490 -0
  5. data/Gemfile +0 -1
  6. data/History.md +40 -0
  7. data/README.md +58 -11
  8. data/Rakefile +1 -4
  9. data/ci/{rails-5.0.gemfile → rails-5.2.gemfile} +5 -5
  10. data/ci/{rails-4.0.gemfile → rails-6.0.gemfile} +5 -4
  11. data/ci/{rails-4.1.gemfile → rails-6.1.gemfile} +5 -4
  12. data/ci/{rails-4.2.gemfile → rails-7.0.gemfile} +5 -4
  13. data/lib/action_dispatch/middleware/session/sequel_store.rb +21 -9
  14. data/lib/generators/sequel/migration/migration_generator.rb +1 -1
  15. data/lib/generators/sequel/migration/templates/migration.rb.erb +2 -0
  16. data/lib/generators/sequel/session_migration/session_migration_generator.rb +1 -1
  17. data/lib/sequel_rails/configuration.rb +5 -0
  18. data/lib/sequel_rails/db_config.rb +6 -2
  19. data/lib/sequel_rails/migrations.rb +16 -7
  20. data/lib/sequel_rails/railtie.rb +6 -9
  21. data/lib/sequel_rails/railties/database.rake +17 -0
  22. data/lib/sequel_rails/storage/abstract.rb +2 -0
  23. data/lib/sequel_rails/storage/mysql.rb +1 -0
  24. data/lib/sequel_rails/version.rb +1 -1
  25. data/sequel-rails.gemspec +7 -6
  26. data/spec/helpers/io.rb +5 -0
  27. data/spec/integration/sessions_controller_spec.rb +1 -1
  28. data/spec/internal/config/database.yml +3 -0
  29. data/spec/internal/db/schema.rb.init +2 -2
  30. data/spec/lib/generators/sequel/migration_spec.rb +9 -1
  31. data/spec/lib/generators/sequel/session_migration_spec.rb +1 -1
  32. data/spec/lib/sequel_rails/configuration_spec.rb +109 -10
  33. data/spec/lib/sequel_rails/migrations_spec.rb +27 -4
  34. data/spec/lib/sequel_rails/railtie_spec.rb +7 -3
  35. data/spec/lib/sequel_rails/railties/database_rake_spec.rb +42 -0
  36. data/spec/lib/sequel_rails/storage/mysql_spec.rb +1 -1
  37. data/spec/lib/sequel_rails/storage/postgres_spec.rb +11 -0
  38. data/spec/lib/sequel_rails/storage/sqlite_spec.rb +4 -1
  39. data/spec/spec_helper.rb +1 -1
  40. metadata +46 -41
  41. data/.travis.yml +0 -37
  42. data/ci/rails-5.1.gemfile +0 -28
  43. data/lib/sequel_rails/railties/spring_support.rb +0 -14
  44. data/rubocop-todo.yml +0 -39
data/History.md CHANGED
@@ -1,3 +1,43 @@
1
+ 1.2.0 (2022-04-15)
2
+ ==================
3
+
4
+ * Migrate CI to Github actions (Jonathan Tron)
5
+ * Add a new sequel-rails hook: `after_new_connection` which
6
+ sets `Sequel`'s `after_connect`, and is triggered for every
7
+ new connection (@kamilpavlicko)
8
+ [#186](https://github.com/TalentBox/sequel-rails/pull/186)
9
+ * Database drop fix for Sequel (>= 5.38.0) (@AnotherRegularDude)
10
+ [#184](https://github.com/TalentBox/sequel-rails/pull/184)
11
+ * Fix for simplified Spring integration (Janko Marohnić, Adrián Mugnolo)
12
+ [#181](https://github.com/TalentBox/sequel-rails/pull/181)
13
+ * Simplify Spring integration (Janko Marohnić)
14
+ [#180](https://github.com/TalentBox/sequel-rails/pull/180)
15
+
16
+ 1.1.1 (2020-06-08)
17
+ ==================
18
+
19
+ * When using SQL schema dump in MySQL, don't output the generation date in order
20
+ to have the same output if nothing changed. (Joseph Halter)
21
+ * Fix readme formatting (Ben Koshy)
22
+ [#175](https://github.com/TalentBox/sequel-rails/pull/175)
23
+ * Add frozen_string_literal to migration template (Semyon Pupkov)
24
+ [#174](https://github.com/TalentBox/sequel-rails/pull/174)
25
+ * Add allow_missing_migration_files option (Semyon Pupkov)
26
+ [#172](https://github.com/TalentBox/sequel-rails/pull/172)
27
+ [#173](https://github.com/TalentBox/sequel-rails/pull/173)
28
+
29
+ 1.1.0 (2019-10-29)
30
+ ==================
31
+
32
+ * Add test_connect option (p-leger)
33
+ [#165](https://github.com/TalentBox/sequel-rails/pull/165)
34
+ * Silence logger for sessions store (Michael Coyne)
35
+ [#171](https://github.com/TalentBox/sequel-rails/pull/171)
36
+ * Add db:sessions:clear and db:sessions:trim rake tasks (Michael Coyne)
37
+ [#170](https://github.com/TalentBox/sequel-rails/pull/170)
38
+ * Allow 'servers' to propagate to Sequel connection (Dustin Byrne)
39
+ [#169](https://github.com/TalentBox/sequel-rails/pull/169)
40
+
1
41
  1.0.1 (2018-02-27)
2
42
  ==================
3
43
 
data/README.md CHANGED
@@ -1,16 +1,15 @@
1
1
  # sequel-rails
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/sequel-rails.svg)][gem]
4
- [![Build Status](https://secure.travis-ci.org/TalentBox/sequel-rails.svg?branch=master)][travis]
4
+ ![Build Status](https://github.com/TalentBox/sequel-rails/actions/workflows/ci.yml/badge.svg)
5
5
  [![Code Climate](https://codeclimate.com/github/TalentBox/sequel-rails.svg)][codeclimate]
6
6
 
7
7
  [gem]: https://rubygems.org/gems/sequel-rails
8
- [travis]: http://travis-ci.org/TalentBox/sequel-rails
9
8
  [codeclimate]: https://codeclimate.com/github/TalentBox/sequel-rails
10
9
 
11
10
  This gem provides the railtie that allows
12
11
  [sequel](http://github.com/jeremyevans/sequel) to hook into
13
- [Rails (4.x and 5.x)](http://github.com/rails/rails) and thus behave like a
12
+ [Rails (5.2.x, 6.x, 7.x)](http://github.com/rails/rails) and thus behave like a
14
13
  rails framework component. Just like activerecord does in rails,
15
14
  [sequel-rails](http://github.com/talentbox/sequel-rails) uses the railtie API to
16
15
  hook into rails. The two are actually hooked into rails almost identically.
@@ -28,7 +27,7 @@ Since January 2013, we've become the official maintainers of the gem after
28
27
  Using sequel-rails
29
28
  ==================
30
29
 
31
- Using sequel with Rails (4.x or 5.x) requires a couple minor changes.
30
+ Using sequel with Rails (5.2.x, 6.x, 7.x) requires a couple minor changes.
32
31
 
33
32
  First, add the following to your Gemfile (after the `Rails` lines):
34
33
 
@@ -166,6 +165,9 @@ You can configure some options with the usual rails mechanism, in
166
165
  # Allowed options: :sql, :ruby.
167
166
  config.sequel.schema_format = :sql
168
167
 
168
+ # Allowed options: true, false, default false
169
+ config.sequel.allow_missing_migration_files = true
170
+
169
171
  # Whether to dump the schema after successful migrations.
170
172
  # Defaults to false in production and test, true otherwise.
171
173
  config.sequel.schema_dump = true
@@ -188,6 +190,21 @@ You can configure some options with the usual rails mechanism, in
188
190
  # This setting disabled the automatic connect after Rails init
189
191
  config.sequel.skip_connect = true
190
192
 
193
+ # Configure if Sequel should try to 'test' the database connection in order
194
+ # to fail early
195
+ config.sequel.test_connect = true
196
+
197
+ # Configure what should happend after SequelRails will create new connection with Sequel (applicable only for the first new connection)
198
+ # config.sequel.after_connect = proc do
199
+ # Sequel::Model.plugin :timestamps, update_on_create: true
200
+ # end
201
+
202
+ # Configure what should happend after new connection in connection pool is created (applicable only for all connections)
203
+ # to fail early
204
+ # config.sequel.after_new_connection = proc do |db|
205
+ # db.execute('SET statement_timeout = 30000;')
206
+ # end
207
+
191
208
  # If you want to use a specific logger
192
209
  config.sequel.logger = MyLogger.new($stdout)
193
210
  ```
@@ -246,11 +263,30 @@ Here's some examples:
246
263
 
247
264
  For in memory testing:
248
265
 
249
- ```yaml
250
- development:
266
+ ```yaml
267
+ development:
251
268
  adapter: sqlite # Also accept sqlite3
252
269
  database: ":memory:"
253
- ```
270
+ ```
271
+
272
+ after_connect hooks
273
+ ================
274
+
275
+ There are 2 options how to set after_connect hooks in `config/application.rb`
276
+
277
+ 1. `config.sequel.after_connect` will be called only on the first new connection. It can be used for enabling plugins or to set some global sequel settings.
278
+ ```ruby
279
+ config.sequel.after_connect = proc do
280
+ Sequel::Model.plugin :timestamps, update_on_create: true
281
+ end
282
+ ```
283
+
284
+ 2. `config.sequel.after_new_connection` will be called after every new connection in connection pool is created. It can be used to run some specific `SET` commands on every new connection. It's using default `after_connect` hook in sequel. https://sequel.jeremyevans.net/rdoc/classes/Sequel/ConnectionPool.html#attribute-i-after_connect
285
+ ```ruby
286
+ config.sequel.after_new_connection = proc do |db|
287
+ db.execute('SET statement_timeout = 30000;')
288
+ end
289
+ ```
254
290
 
255
291
  Enabling plugins
256
292
  ================
@@ -333,6 +369,8 @@ rake db:schema:load # Load a schema.rb file into the database
333
369
  rake db:seed # Load the seed data from db/seeds.rb
334
370
  rake db:setup # Create the database, load the schema, and initialize with the seed data
335
371
  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).
372
+ rake db:sessions:clear # Delete all sessions from the database
373
+ rake db:sessions:trim[threshold] # Delete all sessions older than `threshold` days (default to 30 days, eg: rake db:session:trim[10])
336
374
  ```
337
375
 
338
376
  Note on Patches/Pull Requests
@@ -349,15 +387,15 @@ Note on Patches/Pull Requests
349
387
  The sequel-rails team
350
388
  =====================
351
389
 
352
- * Jonathan Tron (JonathanTron) - Current maintainer
353
- * Joseph Halter (JosephHalter) - Current maintainer
390
+ * Jonathan Tron (@JonathanTron) - Current maintainer
391
+ * Joseph Halter (@JosephHalter) - Current maintainer
354
392
 
355
393
  Previous maintainer
356
394
  ===================
357
395
 
358
396
  [Original project](https://github.com/brasten/sequel-rails):
359
397
 
360
- * Brasten Sager (brasten) - Project creator
398
+ * Brasten Sager (@brasten) - Project creator
361
399
 
362
400
  Contributors
363
401
  ============
@@ -403,6 +441,15 @@ Improvements have been made by those awesome contributors:
403
441
  * ckoenig (@ckoenig)
404
442
  * Rolf Timmermans (@rolftimmermans)
405
443
  * Olivier Lacan (@olivierlacan)
444
+ * Dustin Byrne (@dsbyrne)
445
+ * Michael Coyne (@mjc-gh)
446
+ * p-leger (@p-leger)
447
+ * Semyon Pupkov (@artofhuman)
448
+ * Ben Koshy (@BKSpurgeon)
449
+ * Janko Marohnić (@janko)
450
+ * Adrián Mugnolo (@xymbol)
451
+ * Ivan (@AnotherRegularDude)
452
+ * kamilpavlicko (@kamilpavlicko)
406
453
 
407
454
  Credits
408
455
  =======
@@ -412,4 +459,4 @@ The [dm-rails](http://github.com/datamapper/dm-rails) team wrote most of the ori
412
459
  Copyright
413
460
  =========
414
461
 
415
- Copyright (c) 2010-2013 The sequel-rails team. See [LICENSE](http://github.com/brasten/sequel-rails/blob/master/LICENSE) for details.
462
+ Copyright (c) 2010-2022 The sequel-rails team. See [LICENSE](http://github.com/brasten/sequel-rails/blob/master/LICENSE) for details.
data/Rakefile CHANGED
@@ -19,6 +19,7 @@ begin
19
19
  %w(
20
20
  TEST_ADAPTER
21
21
  TEST_DATABASE
22
+ TEST_DATABASE_HOST
22
23
  TEST_OWNER
23
24
  TEST_USERNAME
24
25
  TEST_PASSWORD
@@ -33,10 +34,6 @@ begin
33
34
  'sqlite3' => { 'TEST_DATABASE' => File.join(File.expand_path('.'), 'spec/internal/db/database.sqlite3') },
34
35
  }
35
36
 
36
- configs.merge!(
37
- 'mysql' => { 'TEST_ENCODING' => 'utf8', 'TEST_USERNAME' => 'root' }
38
- ) if RUBY_VERSION < '2.4'
39
-
40
37
  configs.merge!(
41
38
  'mysql2' => { 'TEST_ENCODING' => 'utf8', 'TEST_USERNAME' => 'root', 'TEST_DATABASE' => 'sequel_rails_test_mysql2' }
42
39
  ) unless SequelRails.jruby?
@@ -1,7 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 5.0.6'
4
- gem 'activemodel', '~> 5.0.6'
3
+ gem 'railties', '~> 5.2.2'
4
+ gem 'activemodel', '~> 5.2.2'
5
+ gem 'actionpack', '~> 5.2.2'
5
6
 
6
7
  gemspec :path => '../'
7
8
 
@@ -9,12 +10,11 @@ gem 'sequel', "#{ENV['SEQUEL']}"
9
10
 
10
11
  gem 'fakefs', '0.11.2', :require => 'fakefs/safe'
11
12
 
13
+ gem 'rspec-rails', '~> 5.0'
14
+
12
15
  # MRI/Rubinius Adapter Dependencies
13
16
  platform :ruby do
14
17
  gem 'pg'
15
- if RUBY_VERSION < '2.4'
16
- gem 'mysql'
17
- end
18
18
  gem 'mysql2'
19
19
  gem 'sqlite3'
20
20
  end
@@ -1,6 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 4.0.13'
3
+ gem 'railties', '~> 6.0.0'
4
+ gem 'activemodel', '~> 6.0.0'
5
+ gem 'actionpack', '~> 6.0.0'
4
6
 
5
7
  gemspec :path => '../'
6
8
 
@@ -8,12 +10,11 @@ gem 'sequel', "#{ENV['SEQUEL']}"
8
10
 
9
11
  gem 'fakefs', '0.11.2', :require => 'fakefs/safe'
10
12
 
13
+ gem 'rspec-rails', '~> 5.0'
14
+
11
15
  # MRI/Rubinius Adapter Dependencies
12
16
  platform :ruby do
13
17
  gem 'pg'
14
- if RUBY_VERSION < '2.4'
15
- gem 'mysql'
16
- end
17
18
  gem 'mysql2'
18
19
  gem 'sqlite3'
19
20
  end
@@ -1,6 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 4.1.16'
3
+ gem 'railties', '~> 6.1.0'
4
+ gem 'activemodel', '~> 6.1.0'
5
+ gem 'actionpack', '~> 6.1.0'
4
6
 
5
7
  gemspec :path => '../'
6
8
 
@@ -8,12 +10,11 @@ gem 'sequel', "#{ENV['SEQUEL']}"
8
10
 
9
11
  gem 'fakefs', '0.11.2', :require => 'fakefs/safe'
10
12
 
13
+ gem 'rspec-rails', '~> 5.0'
14
+
11
15
  # MRI/Rubinius Adapter Dependencies
12
16
  platform :ruby do
13
17
  gem 'pg'
14
- if RUBY_VERSION < '2.4'
15
- gem 'mysql'
16
- end
17
18
  gem 'mysql2'
18
19
  gem 'sqlite3'
19
20
  end
@@ -1,6 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'railties', '~> 4.2.10'
3
+ gem 'railties', '~> 7.0.0'
4
+ gem 'activemodel', '~> 7.0.0'
5
+ gem 'actionpack', '~> 7.0.0'
4
6
 
5
7
  gemspec :path => '../'
6
8
 
@@ -8,12 +10,11 @@ gem 'sequel', "#{ENV['SEQUEL']}"
8
10
 
9
11
  gem 'fakefs', '0.11.2', :require => 'fakefs/safe'
10
12
 
13
+ gem 'rspec-rails', '~> 5.0'
14
+
11
15
  # MRI/Rubinius Adapter Dependencies
12
16
  platform :ruby do
13
17
  gem 'pg'
14
- if RUBY_VERSION < '2.4'
15
- gem 'mysql'
16
- end
17
18
  gem 'mysql2'
18
19
  gem 'sqlite3'
19
20
  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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Sequel.migration do
2
4
  <%- if use_change -%>
3
5
  change do
@@ -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
@@ -27,7 +27,9 @@ module SequelRails
27
27
  self.schema_dump = default_schema_dump
28
28
  self.load_database_tasks = true
29
29
  self.after_connect = nil
30
+ self.after_new_connection = nil
30
31
  self.skip_connect = nil
32
+ self.test_connect = true
31
33
  end
32
34
 
33
35
  def environment_for(name)
@@ -72,6 +74,9 @@ module SequelRails
72
74
 
73
75
  config['max_connections'] = max_connections if max_connections
74
76
  config['search_path'] = search_path if search_path
77
+ config['servers'] = servers if servers
78
+ config['test'] = test_connect
79
+ config['after_connect'] = after_new_connection if after_new_connection
75
80
 
76
81
  url = ENV['DATABASE_URL']
77
82
  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
 
@@ -6,6 +6,7 @@ module SequelRails
6
6
  def migrate(version = nil)
7
7
  opts = {}
8
8
  opts[:target] = version.to_i if version
9
+ opts[:allow_missing_migration_files] = !!SequelRails.configuration.allow_missing_migration_files
9
10
 
10
11
  if migrations_dir.directory?
11
12
  ::Sequel::Migrator.run(::Sequel::Model.db, migrations_dir, opts)
@@ -25,12 +26,10 @@ module SequelRails
25
26
  def dump_schema_information(opts = {})
26
27
  sql = opts.fetch :sql
27
28
  adapter = SequelRails::Storage.adapter_for(Rails.env)
28
- db = ::Sequel::Model.db
29
29
  res = ''
30
30
 
31
31
  if available_migrations?
32
- migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
33
- migrator = migrator_class.new db, migrations_dir
32
+ migrator = init_migrator
34
33
  res << adapter.schema_information_dump(migrator, sql)
35
34
  end
36
35
  res
@@ -43,8 +42,8 @@ module SequelRails
43
42
  def current_migration
44
43
  return unless available_migrations?
45
44
 
46
- migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
47
- migrator = migrator_class.new ::Sequel::Model.db, migrations_dir
45
+ migrator = init_migrator
46
+
48
47
  if migrator.respond_to?(:applied_migrations)
49
48
  migrator.applied_migrations.last
50
49
  elsif migrator.respond_to?(:current_version)
@@ -55,8 +54,8 @@ module SequelRails
55
54
  def previous_migration
56
55
  return unless available_migrations?
57
56
 
58
- migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
59
- migrator = migrator_class.new ::Sequel::Model.db, migrations_dir
57
+ migrator = init_migrator
58
+
60
59
  if migrator.respond_to?(:applied_migrations)
61
60
  migrator.applied_migrations[-2] || '0'
62
61
  elsif migrator.respond_to?(:current_version)
@@ -67,6 +66,16 @@ module SequelRails
67
66
  def available_migrations?
68
67
  File.exist?(migrations_dir) && Dir[File.join(migrations_dir, '*')].any?
69
68
  end
69
+
70
+ def init_migrator
71
+ migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
72
+
73
+ migrator_class.new(
74
+ ::Sequel::Model.db,
75
+ migrations_dir,
76
+ allow_missing_migration_files: !!SequelRails.configuration.allow_missing_migration_files
77
+ )
78
+ end
70
79
  end
71
80
  end
72
81
  end
@@ -15,7 +15,6 @@ require 'sequel_rails/configuration'
15
15
  require 'sequel_rails/migrations'
16
16
  require 'sequel_rails/railties/log_subscriber'
17
17
  require 'sequel_rails/railties/i18n_support'
18
- require 'sequel_rails/railties/spring_support'
19
18
  require 'sequel_rails/railties/controller_runtime'
20
19
  require 'sequel_rails/sequel/database/active_support_notification'
21
20
  require 'action_dispatch/middleware/session/sequel_store'
@@ -76,19 +75,17 @@ module SequelRails
76
75
  end
77
76
 
78
77
  initializer 'sequel.spring' do |_app|
79
- if defined?(::Spring::Application)
80
- class ::Spring::Application # rubocop:disable Style/ClassAndModuleChildren
81
- include ::SequelRails::SpringSupport
82
- alias_method :disconnect_database_without_sequel, :disconnect_database
83
- alias_method :disconnect_database, :disconnect_database_with_sequel
84
- end
85
- end
78
+ Spring.after_fork { Sequel::DATABASES.each(&:disconnect) } if defined?(Spring)
86
79
  end
87
80
 
88
81
  # Support overwriting crucial steps in subclasses
89
82
  def configure_sequel(app)
90
83
  rails_db_config = begin
91
84
  app.config.database_configuration
85
+ rescue RuntimeError => e
86
+ raise unless e.message =~ /Cannot load database configuration/
87
+
88
+ {} # will try to use DATABASE_URL
92
89
  rescue Errno::ENOENT
93
90
  {} # will try to use DATABASE_URL
94
91
  end
@@ -119,7 +116,7 @@ module SequelRails
119
116
  end
120
117
 
121
118
  def database_create_command?
122
- ["db:create", "db:create:all"].any? { |c| ARGV.include?(c) }
119
+ ['db:create', 'db:create:all'].any? { |c| ARGV.include?(c) }
123
120
  end
124
121
  end
125
122
  end
@@ -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"
@@ -15,6 +15,8 @@ module SequelRails
15
15
  end
16
16
 
17
17
  def drop
18
+ return if ::Sequel::DATABASES.size == 0
19
+
18
20
  ::Sequel::Model.db.disconnect
19
21
  res = _drop
20
22
  warn "[sequel] Dropped database '#{database}'" if res
@@ -13,6 +13,7 @@ module SequelRails
13
13
  commands = ['mysqldump']
14
14
  add_connection_settings commands
15
15
  add_flag commands, '--no-data'
16
+ add_flag commands, '--skip-dump-date'
16
17
  add_option commands, '--result-file', filename
17
18
  commands << database
18
19
  safe_exec commands
@@ -1,3 +1,3 @@
1
1
  module SequelRails
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
data/sequel-rails.gemspec CHANGED
@@ -29,12 +29,13 @@ Gem::Specification.new do |s|
29
29
  s.add_dependency 'actionpack', '>= 4.0.0'
30
30
  s.add_dependency 'sequel', ['>= 3.28', '< 6.0']
31
31
 
32
- s.add_development_dependency 'combustion'
32
+ s.add_development_dependency 'combustion', '~> 1.3'
33
33
  s.add_development_dependency 'generator_spec'
34
- s.add_development_dependency 'rake', '>= 0.8.7', '< 12.1'
35
- s.add_development_dependency 'rspec', '~> 3.7'
36
- s.add_development_dependency 'rspec-rails', '~> 3.7'
37
- s.add_development_dependency 'rubocop', '~> 0.52.0'
38
- s.add_development_dependency 'ammeter', '1.1.4'
34
+ s.add_development_dependency 'rake'
35
+ s.add_development_dependency 'rspec'
36
+ s.add_development_dependency 'rspec-rails'
37
+ s.add_development_dependency 'rubocop', '1.27.0'
38
+ s.add_development_dependency 'ammeter', '1.1.5'
39
39
  s.add_development_dependency 'test-unit'
40
+ s.add_development_dependency 'nokogiri', '>= 1.13.4'
40
41
  end
data/spec/helpers/io.rb CHANGED
@@ -4,5 +4,10 @@ module IOSpecHelper
4
4
  # if this isn't a good use for case equality I don't know what is
5
5
  pattern === a.first ? raise(Errno::ENOENT) : m.call(*a) # rubocop:disable CaseEquality
6
6
  end
7
+ # Rails > 6.1 uses Pathname#exists? instead of rescuing read error
8
+ allow(Pathname).to receive(:new).and_call_original
9
+ allow(Pathname).to receive(:new).with(pattern).and_return(
10
+ instance_double(Pathname, :exist? => false)
11
+ )
7
12
  end
8
13
  end
@@ -9,7 +9,7 @@ describe SessionsController, :type => :request do
9
9
  else
10
10
  {:status => 'logged_in'}
11
11
  end
12
- post '/session', opts
12
+ post '/session', **opts
13
13
  end
14
14
 
15
15
  describe '#create' do
@@ -1,6 +1,7 @@
1
1
  test:
2
2
  adapter: "<%= ENV["TEST_ADAPTER"] || "postgresql" %>"
3
3
  database: "<%= ENV["TEST_DATABASE"] || "sequel_rails_test" %>"
4
+ host: "<%= ENV["TEST_DATABASE_HOST"] %>"
4
5
  <% if ENV['TEST_ADAPTER'] != 'sqlite3' %>
5
6
  owner: "<%= ENV["TEST_OWNER"] || ENV["USER"] %>"
6
7
  user: "<%= ENV["TEST_USERNAME"] || ENV["USER"] %>"
@@ -8,6 +9,8 @@ test:
8
9
  <% end %>
9
10
  <% if ENV['TEST_ADAPTER'] =~ /mysql/ %>
10
11
  encoding: "<%= ENV["TEST_ENCODING"] || "utf8" %>"
12
+ port: "<%= ENV["TEST_DATABASE_PORT"] || "3306" %>"
11
13
  <% elsif ENV['TEST_ADAPTER'] =~ /postgres/ %>
12
14
  encoding: "<%= ENV["TEST_ENCODING"] || "unicode" %>"
15
+ port: "<%= ENV["TEST_DATABASE_PORT"] || "5432" %>"
13
16
  <% end %>
@@ -1,10 +1,10 @@
1
1
  Sequel.migration do
2
2
  change do
3
- create_table :users do
3
+ create_table? :users do
4
4
  primary_key :id
5
5
  String :email
6
6
  end
7
- create_table :sessions do
7
+ create_table? :sessions do
8
8
  primary_key :id
9
9
  String :session_id, :null => false, :unique => true, :index => true
10
10
  String :data, :text => true, :null => false