sequel-rails 0.9.16 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +39 -60
- data/Gemfile +4 -13
- data/History.md +54 -0
- data/README.md +28 -10
- data/Rakefile +1 -1
- data/ci/rails-4.0.gemfile +3 -11
- data/ci/rails-4.1.gemfile +3 -11
- data/ci/rails-4.2.gemfile +3 -11
- data/ci/rails-5.0.gemfile +3 -3
- data/ci/rails-5.1.gemfile +28 -0
- data/ci/rails-5.2.gemfile +28 -0
- data/ci/rails-6.0.gemfile +28 -0
- data/lib/action_dispatch/middleware/session/sequel_store.rb +21 -9
- data/lib/generators/sequel.rb +7 -7
- data/lib/generators/sequel/migration/migration_generator.rb +1 -1
- data/lib/generators/sequel/migration/templates/migration.rb.erb +2 -0
- data/lib/generators/sequel/model/model_generator.rb +2 -1
- data/lib/generators/sequel/session_migration/session_migration_generator.rb +1 -1
- data/lib/sequel_rails/configuration.rb +7 -4
- data/lib/sequel_rails/db_config.rb +8 -4
- data/lib/sequel_rails/migrations.rb +23 -7
- data/lib/sequel_rails/railtie.rb +21 -5
- data/lib/sequel_rails/railties/database.rake +31 -6
- data/lib/sequel_rails/sequel/plugins/rails_extensions.rb +1 -1
- data/lib/sequel_rails/storage.rb +2 -4
- data/lib/sequel_rails/storage/abstract.rb +17 -5
- data/lib/sequel_rails/storage/jdbc.rb +2 -2
- data/lib/sequel_rails/storage/mysql.rb +1 -0
- data/lib/sequel_rails/storage/postgres.rb +7 -3
- data/lib/sequel_rails/version.rb +1 -1
- data/rubocop-todo.yml +4 -1
- data/sequel-rails.gemspec +13 -21
- data/spec/helpers/io.rb +1 -5
- data/spec/integration/sessions_controller_spec.rb +6 -1
- data/spec/lib/generators/sequel/migration_spec.rb +9 -1
- data/spec/lib/generators/sequel/session_migration_spec.rb +1 -1
- data/spec/lib/sequel_rails/configuration_spec.rb +78 -0
- data/spec/lib/sequel_rails/migrations_spec.rb +21 -4
- data/spec/lib/sequel_rails/railtie_spec.rb +1 -1
- data/spec/lib/sequel_rails/railties/database_rake_spec.rb +42 -0
- data/spec/lib/sequel_rails/storage/mysql_spec.rb +1 -1
- data/spec/lib/sequel_rails/storage/postgres_spec.rb +1 -1
- data/spec/spec_helper.rb +5 -2
- metadata +29 -34
- data/ci/rails-3.2.gemfile +0 -42
@@ -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
|
@@ -0,0 +1,28 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'railties', '~> 6.0.0'
|
4
|
+
gem 'activemodel', '~> 6.0.0'
|
5
|
+
gem 'actionpack', '~> 6.0.0'
|
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
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
57
|
-
env[
|
58
|
-
|
59
|
-
|
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
|
data/lib/generators/sequel.rb
CHANGED
@@ -17,6 +17,13 @@ module Sequel
|
|
17
17
|
)
|
18
18
|
end
|
19
19
|
|
20
|
+
# Implement the required interface for Rails::Generators::Migration.
|
21
|
+
#
|
22
|
+
def self.next_migration_number(dirname) #:nodoc:
|
23
|
+
next_migration_number = current_migration_number(dirname) + 1
|
24
|
+
[Time.now.utc.strftime('%Y%m%d%H%M%S'), format('%.14d', next_migration_number)].max
|
25
|
+
end
|
26
|
+
|
20
27
|
protected
|
21
28
|
|
22
29
|
# Sequel does not care if migrations have the same name as long as
|
@@ -25,13 +32,6 @@ module Sequel
|
|
25
32
|
def migration_exists?(_dirname, _file_name) #:nodoc:
|
26
33
|
false
|
27
34
|
end
|
28
|
-
|
29
|
-
# Implement the required interface for Rails::Generators::Migration.
|
30
|
-
#
|
31
|
-
def self.next_migration_number(dirname) #:nodoc:
|
32
|
-
next_migration_number = current_migration_number(dirname) + 1
|
33
|
-
[Time.now.utc.strftime('%Y%m%d%H%M%S'), format('%.14d', next_migration_number)].max
|
34
|
-
end
|
35
35
|
end
|
36
36
|
|
37
37
|
class ActiveModel < ::Rails::Generators::ActiveModel #:nodoc:
|
@@ -12,10 +12,11 @@ module Sequel
|
|
12
12
|
class_option :parent, :type => :string, :desc => 'The parent class for the generated model'
|
13
13
|
|
14
14
|
def create_migration_file
|
15
|
+
return unless options[:migration]
|
15
16
|
migration_template(
|
16
17
|
'migration.rb.erb',
|
17
18
|
File.join('db', 'migrate', "create_#{table_name}.rb")
|
18
|
-
)
|
19
|
+
)
|
19
20
|
end
|
20
21
|
|
21
22
|
def create_model_file
|
@@ -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)
|
@@ -37,7 +38,7 @@ module SequelRails
|
|
37
38
|
def environments
|
38
39
|
@environments ||= raw.reduce(
|
39
40
|
# default config - use just the environment variable
|
40
|
-
Hash.new
|
41
|
+
Hash.new(normalize_repository_config({}))
|
41
42
|
) do |normalized, environment|
|
42
43
|
name = environment.first
|
43
44
|
config = environment.last
|
@@ -49,8 +50,8 @@ module SequelRails
|
|
49
50
|
def connect(environment)
|
50
51
|
normalized_config = environment_for environment
|
51
52
|
|
52
|
-
unless (normalized_config.keys & %w
|
53
|
-
|
53
|
+
unless (normalized_config.keys & %w[adapter url]).any?
|
54
|
+
raise "Database not configured.\n" \
|
54
55
|
'Please create config/database.yml or set DATABASE_URL in environment.'
|
55
56
|
end
|
56
57
|
|
@@ -64,7 +65,7 @@ module SequelRails
|
|
64
65
|
private
|
65
66
|
|
66
67
|
def default_schema_dump
|
67
|
-
!%w
|
68
|
+
!%w[test production].include? Rails.env
|
68
69
|
end
|
69
70
|
|
70
71
|
def normalize_repository_config(hash)
|
@@ -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
|
@@ -33,7 +33,7 @@ module SequelRails
|
|
33
33
|
ADAPTER_MAPPING = {
|
34
34
|
'sqlite3' => 'sqlite',
|
35
35
|
'postgresql' => 'postgres'
|
36
|
-
}
|
36
|
+
}.freeze
|
37
37
|
|
38
38
|
def normalize_adapter
|
39
39
|
self[:adapter] = ADAPTER_MAPPING[adapter.to_s] || adapter.to_s
|
@@ -59,7 +59,7 @@ module SequelRails
|
|
59
59
|
scheme, subadapter = adapter.split ':'
|
60
60
|
URI::Generic.build(
|
61
61
|
:scheme => scheme,
|
62
|
-
:opaque => build_url(to_hash.merge
|
62
|
+
:opaque => build_url(to_hash.merge('adapter' => subadapter)).to_s
|
63
63
|
)
|
64
64
|
else
|
65
65
|
build_url to_hash
|
@@ -73,8 +73,8 @@ module SequelRails
|
|
73
73
|
return URI::Generic.build(:scheme => adapter, :opaque => database)
|
74
74
|
end
|
75
75
|
|
76
|
-
# these
|
77
|
-
params = cfg.reject { |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,7 +6,14 @@ module SequelRails
|
|
6
6
|
def migrate(version = nil)
|
7
7
|
opts = {}
|
8
8
|
opts[:target] = version.to_i if version
|
9
|
-
|
9
|
+
opts[:allow_missing_migration_files] = !!SequelRails.configuration.allow_missing_migration_files
|
10
|
+
|
11
|
+
if migrations_dir.directory?
|
12
|
+
::Sequel::Migrator.run(::Sequel::Model.db, migrations_dir, opts)
|
13
|
+
else
|
14
|
+
relative_path_name = migrations_dir.relative_path_from(Rails.root).to_s
|
15
|
+
raise "The #{relative_path_name} directory doesn't exist, you need to create it."
|
16
|
+
end
|
10
17
|
end
|
11
18
|
alias_method :migrate_up!, :migrate
|
12
19
|
alias_method :migrate_down!, :migrate
|
@@ -23,8 +30,7 @@ module SequelRails
|
|
23
30
|
res = ''
|
24
31
|
|
25
32
|
if available_migrations?
|
26
|
-
|
27
|
-
migrator = migrator_class.new db, migrations_dir
|
33
|
+
migrator = init_migrator
|
28
34
|
res << adapter.schema_information_dump(migrator, sql)
|
29
35
|
end
|
30
36
|
res
|
@@ -37,8 +43,8 @@ module SequelRails
|
|
37
43
|
def current_migration
|
38
44
|
return unless available_migrations?
|
39
45
|
|
40
|
-
|
41
|
-
|
46
|
+
migrator = init_migrator
|
47
|
+
|
42
48
|
if migrator.respond_to?(:applied_migrations)
|
43
49
|
migrator.applied_migrations.last
|
44
50
|
elsif migrator.respond_to?(:current_version)
|
@@ -49,8 +55,8 @@ module SequelRails
|
|
49
55
|
def previous_migration
|
50
56
|
return unless available_migrations?
|
51
57
|
|
52
|
-
|
53
|
-
|
58
|
+
migrator = init_migrator
|
59
|
+
|
54
60
|
if migrator.respond_to?(:applied_migrations)
|
55
61
|
migrator.applied_migrations[-2] || '0'
|
56
62
|
elsif migrator.respond_to?(:current_version)
|
@@ -61,6 +67,16 @@ module SequelRails
|
|
61
67
|
def available_migrations?
|
62
68
|
File.exist?(migrations_dir) && Dir[File.join(migrations_dir, '*')].any?
|
63
69
|
end
|
70
|
+
|
71
|
+
def init_migrator
|
72
|
+
migrator_class = ::Sequel::Migrator.send(:migrator_class, migrations_dir)
|
73
|
+
|
74
|
+
migrator_class.new(
|
75
|
+
::Sequel::Model.db,
|
76
|
+
migrations_dir,
|
77
|
+
allow_missing_migration_files: !!SequelRails.configuration.allow_missing_migration_files
|
78
|
+
)
|
79
|
+
end
|
64
80
|
end
|
65
81
|
end
|
66
82
|
end
|
data/lib/sequel_rails/railtie.rb
CHANGED
@@ -38,12 +38,16 @@ module SequelRails
|
|
38
38
|
|
39
39
|
rake_tasks do |app|
|
40
40
|
load_tasks_config = app.config.sequel.load_database_tasks
|
41
|
+
|
41
42
|
SequelRails::TASK_NAMESPACE =
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
case load_tasks_config
|
44
|
+
when Symbol, String then load_tasks_config.to_sym
|
45
|
+
else :db
|
46
|
+
end
|
47
|
+
|
46
48
|
load 'sequel_rails/railties/database.rake' if load_tasks_config
|
49
|
+
|
50
|
+
check_skip_connect_conditions(app)
|
47
51
|
end
|
48
52
|
|
49
53
|
initializer 'sequel.load_hooks' do
|
@@ -68,7 +72,7 @@ module SequelRails
|
|
68
72
|
end
|
69
73
|
|
70
74
|
initializer 'sequel.connect' do |app|
|
71
|
-
::SequelRails.setup
|
75
|
+
::SequelRails.setup(::Rails.env) if database_connection_required?(app)
|
72
76
|
end
|
73
77
|
|
74
78
|
initializer 'sequel.spring' do |_app|
|
@@ -105,5 +109,17 @@ module SequelRails
|
|
105
109
|
require 'sequel_rails/railties/controller_runtime'
|
106
110
|
ActionController::Base.send :include, SequelRails::Railties::ControllerRuntime
|
107
111
|
end
|
112
|
+
|
113
|
+
def check_skip_connect_conditions(app)
|
114
|
+
app.config.sequel[:skip_connect] ||= database_create_command?
|
115
|
+
end
|
116
|
+
|
117
|
+
def database_connection_required?(app)
|
118
|
+
!app.config.sequel[:skip_connect]
|
119
|
+
end
|
120
|
+
|
121
|
+
def database_create_command?
|
122
|
+
["db:create", "db:create:all"].any? { |c| ARGV.include?(c) }
|
123
|
+
end
|
108
124
|
end
|
109
125
|
end
|
@@ -22,11 +22,17 @@ namespace sequel_rails_namespace do
|
|
22
22
|
task :dump => :environment do
|
23
23
|
db_for_current_env.extension :schema_dumper
|
24
24
|
filename = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
|
26
|
+
if Rails.root.join("db").exist?
|
27
|
+
File.open filename, 'w' do |file|
|
28
|
+
file << db_for_current_env.dump_schema_migration(:same_db => true)
|
29
|
+
file << SequelRails::Migrations.dump_schema_information(:sql => false)
|
30
|
+
end
|
31
|
+
|
32
|
+
Rake::Task["#{sequel_rails_namespace}:schema:dump"].reenable
|
33
|
+
else
|
34
|
+
abort "The db/ directory doesn't exist, please create it."
|
28
35
|
end
|
29
|
-
Rake::Task["#{sequel_rails_namespace}:schema:dump"].reenable
|
30
36
|
end
|
31
37
|
|
32
38
|
desc 'Load a schema.rb file into the database'
|
@@ -45,6 +51,7 @@ namespace sequel_rails_namespace do
|
|
45
51
|
namespace :structure do
|
46
52
|
desc 'Dump the database structure to db/structure.sql'
|
47
53
|
task :dump, [:env] => :environment do |_t, args|
|
54
|
+
db_for_current_env
|
48
55
|
args.with_defaults(:env => Rails.env)
|
49
56
|
|
50
57
|
filename = ENV['DB_STRUCTURE'] || File.join(Rails.root, 'db', 'structure.sql')
|
@@ -126,6 +133,7 @@ namespace sequel_rails_namespace do
|
|
126
133
|
namespace :migrate do
|
127
134
|
task :load => :environment do
|
128
135
|
require 'sequel_rails/migrations'
|
136
|
+
db_for_current_env
|
129
137
|
end
|
130
138
|
|
131
139
|
desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
|
@@ -145,7 +153,7 @@ namespace sequel_rails_namespace do
|
|
145
153
|
desc 'Runs the "up" for a given migration VERSION.'
|
146
154
|
task :up => :load do
|
147
155
|
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
148
|
-
|
156
|
+
raise 'VERSION is required' unless version
|
149
157
|
SequelRails::Migrations.migrate_up!(version)
|
150
158
|
Rake::Task["#{sequel_rails_namespace}:dump"].invoke if SequelRails.configuration.schema_dump
|
151
159
|
end
|
@@ -153,7 +161,7 @@ namespace sequel_rails_namespace do
|
|
153
161
|
desc 'Runs the "down" for a given migration VERSION.'
|
154
162
|
task :down => :load do
|
155
163
|
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
156
|
-
|
164
|
+
raise 'VERSION is required' unless version
|
157
165
|
SequelRails::Migrations.migrate_down!(version)
|
158
166
|
Rake::Task["#{sequel_rails_namespace}:dump"].invoke if SequelRails.configuration.schema_dump
|
159
167
|
end
|
@@ -205,6 +213,23 @@ namespace sequel_rails_namespace do
|
|
205
213
|
Rails.env = previous_env
|
206
214
|
end
|
207
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
|
208
233
|
end
|
209
234
|
|
210
235
|
task 'test:prepare' => "#{sequel_rails_namespace}:test:prepare"
|