sequel-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +28 -0
- data/Gemfile +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +86 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/autotest/discover.rb +1 -0
- data/lib/generators/sequel.rb +82 -0
- data/lib/generators/sequel/migration/migration_generator.rb +30 -0
- data/lib/generators/sequel/migration/templates/migration.rb +16 -0
- data/lib/generators/sequel/model/model_generator.rb +23 -0
- data/lib/generators/sequel/model/templates/model.rb +3 -0
- data/lib/generators/sequel/observer/observer_generator.rb +19 -0
- data/lib/generators/sequel/observer/templates/observer.rb +7 -0
- data/lib/sequel-rails.rb +1 -0
- data/lib/sequel-rails/configuration.rb +54 -0
- data/lib/sequel-rails/migrations.rb +30 -0
- data/lib/sequel-rails/railtie.rb +111 -0
- data/lib/sequel-rails/railties/benchmarking_mixin.rb +23 -0
- data/lib/sequel-rails/railties/controller_runtime.rb +45 -0
- data/lib/sequel-rails/railties/database.rake +109 -0
- data/lib/sequel-rails/railties/i18n_support.rb +12 -0
- data/lib/sequel-rails/railties/log_subscriber.rb +31 -0
- data/lib/sequel-rails/session_store.rb +82 -0
- data/lib/sequel-rails/setup.rb +18 -0
- data/lib/sequel-rails/storage.rb +165 -0
- data/sequel-rails.gemspec +91 -0
- data/spec/rcov.opts +6 -0
- data/spec/setup_spec.rb +7 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +21 -0
- data/tasks/ci.rake +1 -0
- data/tasks/clean.rake +6 -0
- data/tasks/metrics.rake +37 -0
- data/tasks/spec.rake +38 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +20 -0
- metadata +164 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'sequel/extensions/migration'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Sequel
|
5
|
+
class Migrations
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def migrate_up!(version=nil)
|
10
|
+
opts = {}
|
11
|
+
opts[:target] = version.to_i if version
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
::Sequel::Migrator.run(::Sequel::Model.db, "db/migrate", opts)
|
16
|
+
end
|
17
|
+
|
18
|
+
def migrate_down!(version=nil)
|
19
|
+
opts = {}
|
20
|
+
opts[:target] = version.to_i if version
|
21
|
+
|
22
|
+
::Sequel::Migrator.run(::Sequel::Model.db, "db/migrate", opts)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
require 'rails'
|
4
|
+
require 'active_model/railtie'
|
5
|
+
|
6
|
+
# Comment taken from active_record/railtie.rb
|
7
|
+
#
|
8
|
+
# For now, action_controller must always be present with
|
9
|
+
# rails, so let's make sure that it gets required before
|
10
|
+
# here. This is needed for correctly setting up the middleware.
|
11
|
+
# In the future, this might become an optional require.
|
12
|
+
require 'action_controller/railtie'
|
13
|
+
|
14
|
+
require 'sequel-rails/setup'
|
15
|
+
require "sequel-rails/railties/log_subscriber"
|
16
|
+
require "sequel-rails/railties/i18n_support"
|
17
|
+
|
18
|
+
|
19
|
+
module Rails
|
20
|
+
module Sequel
|
21
|
+
|
22
|
+
class Railtie < Rails::Railtie
|
23
|
+
|
24
|
+
log_subscriber :sequel, ::Sequel::Railties::LogSubscriber.new
|
25
|
+
|
26
|
+
config.generators.orm :sequel, :migration => true
|
27
|
+
|
28
|
+
rake_tasks do
|
29
|
+
load 'sequel-rails/railties/database.rake'
|
30
|
+
end
|
31
|
+
|
32
|
+
initializer 'sequel.configuration' do |app|
|
33
|
+
configure_sequel(app)
|
34
|
+
end
|
35
|
+
|
36
|
+
initializer 'sequel.logger' do |app|
|
37
|
+
setup_logger(app, Rails.logger)
|
38
|
+
end
|
39
|
+
|
40
|
+
initializer 'sequel.i18n_support' do |app|
|
41
|
+
setup_i18n_support(app)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Expose database runtime to controller for logging.
|
45
|
+
initializer "sequel.log_runtime" do |app|
|
46
|
+
setup_controller_runtime(app)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Run setup code after_initialize to make sure all config/initializers
|
50
|
+
# are in effect once we setup the connection. This is especially necessary
|
51
|
+
# for the cascaded adapter wrappers that need to be declared before setup.
|
52
|
+
|
53
|
+
config.after_initialize do |app|
|
54
|
+
setup_sequel(app)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
# Support overwriting crucial steps in subclasses
|
61
|
+
|
62
|
+
|
63
|
+
def configure_sequel(app)
|
64
|
+
app.config.sequel = Rails::Sequel::Configuration.for(
|
65
|
+
Rails.root, app.config.database_configuration
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
def setup_i18n_support(app)
|
70
|
+
::Sequel::Model.send :include, Rails::Sequel::I18nSupport
|
71
|
+
end
|
72
|
+
|
73
|
+
def setup_controller_runtime(app)
|
74
|
+
require "sequel-rails/railties/controller_runtime"
|
75
|
+
ActionController::Base.send :include, Rails::Sequel::Railties::ControllerRuntime
|
76
|
+
end
|
77
|
+
|
78
|
+
def setup_logger(app, logger)
|
79
|
+
app.config.sequel.logger=logger
|
80
|
+
end
|
81
|
+
|
82
|
+
module Setup
|
83
|
+
|
84
|
+
def setup_sequel(app)
|
85
|
+
# preload_lib(app)
|
86
|
+
# preload_models(app)
|
87
|
+
Rails::Sequel.setup(Rails.env)
|
88
|
+
end
|
89
|
+
|
90
|
+
# def preload_lib(app)
|
91
|
+
# app.config.paths.lib.each do |path|
|
92
|
+
# Dir.glob("#{path}/**/*.rb").sort.each do |file|
|
93
|
+
# require_dependency file unless file.match(/#{path}\/generators\/*/)
|
94
|
+
# end
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# def preload_models(app)
|
99
|
+
# app.config.paths.app.models.each do |path|
|
100
|
+
# Dir.glob("#{path}/**/*.rb").sort.each { |file| require_dependency file }
|
101
|
+
# end
|
102
|
+
# end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
extend Setup
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Adapters
|
3
|
+
module Benchmarking
|
4
|
+
|
5
|
+
%w[ create read update delete ].each do |method|
|
6
|
+
class_eval <<-RUBY, __FILE__, __LINE__
|
7
|
+
def #{method}(*args, &block)
|
8
|
+
result = nil
|
9
|
+
@runtime ||= 0
|
10
|
+
@runtime += Benchmark.ms { result = super(*args, &block) }
|
11
|
+
result
|
12
|
+
end
|
13
|
+
RUBY
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset_runtime
|
17
|
+
rt, @runtime = @runtime, 0
|
18
|
+
rt.to_f
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/core_ext/module/attr_internal'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Sequel
|
5
|
+
module Railties
|
6
|
+
|
7
|
+
module ControllerRuntime
|
8
|
+
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
protected
|
12
|
+
|
13
|
+
attr_internal :db_runtime
|
14
|
+
|
15
|
+
def cleanup_view_runtime
|
16
|
+
# TODO add checks if Sequel is connected to a repository.
|
17
|
+
# If it is, do this, if it isn't, just delegate to super
|
18
|
+
db_rt_before_render = ::Sequel.repository.adapter.reset_runtime
|
19
|
+
runtime = super
|
20
|
+
db_rt_after_render = ::Sequel.repository.adapter.reset_runtime
|
21
|
+
self.db_runtime = db_rt_before_render + db_rt_after_render
|
22
|
+
runtime - db_rt_after_render
|
23
|
+
end
|
24
|
+
|
25
|
+
def append_info_to_payload(payload)
|
26
|
+
super
|
27
|
+
payload[:db_runtime] = db_runtime
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
|
33
|
+
def log_process_action(payload)
|
34
|
+
messages, db_runtime = super, payload[:db_runtime]
|
35
|
+
messages << ("Models: %.1fms" % db_runtime.to_f) if db_runtime
|
36
|
+
messages
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'sequel-rails/setup'
|
2
|
+
require 'sequel-rails/storage'
|
3
|
+
|
4
|
+
namespace :db do
|
5
|
+
|
6
|
+
task :load_models => :environment do
|
7
|
+
FileList["app/models/**/*.rb"].each { |model| load model }
|
8
|
+
end
|
9
|
+
|
10
|
+
desc 'Create the database, load the schema, and initialize with the seed data'
|
11
|
+
task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]
|
12
|
+
|
13
|
+
namespace :test do
|
14
|
+
task :prepare => ['db:setup']
|
15
|
+
end
|
16
|
+
|
17
|
+
namespace :create do
|
18
|
+
desc 'Create all the local databases defined in config/database.yml'
|
19
|
+
task :all => :environment do
|
20
|
+
Rails::Sequel.storage.create_all
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Create the database(s) defined in config/database.yml for the current Rails.env - also creates the test database(s) if Rails.env.development?"
|
25
|
+
task :create => :environment do
|
26
|
+
Rails::Sequel.storage.create_environment(Rails::Sequel.configuration.environments[Rails.env])
|
27
|
+
if Rails.env.development? && Rails::Sequel.configuration.environments['test']
|
28
|
+
Rails::Sequel.storage.create_environment(Rails::Sequel.configuration.environments['test'])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
namespace :drop do
|
33
|
+
desc 'Drop all the local databases defined in config/database.yml'
|
34
|
+
task :all => :environment do
|
35
|
+
Rails::Sequel.storage.drop_all
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?"
|
40
|
+
task :drop => :environment do
|
41
|
+
Rails::Sequel.storage.drop_environment(Rails::Sequel.configuration.environments[Rails.env])
|
42
|
+
if Rails.env.development? && Rails::Sequel.configuration.environments['test']
|
43
|
+
Rails::Sequel.storage.drop_environment(Rails::Sequel.configuration.environments['test'])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
desc 'Load the seed data from db/seeds.rb'
|
48
|
+
task :seed => :environment do
|
49
|
+
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
|
50
|
+
load(seed_file) if File.exist?(seed_file)
|
51
|
+
end
|
52
|
+
|
53
|
+
namespace :migrate do
|
54
|
+
task :load => :environment do
|
55
|
+
# FileList['db/migrate/*.rb'].each do |migration|
|
56
|
+
# load migration
|
57
|
+
# end
|
58
|
+
end
|
59
|
+
|
60
|
+
desc 'Migrate up using migrations'
|
61
|
+
task :up, :version, :needs => :load do |t, args|
|
62
|
+
require 'sequel-rails/migrations'
|
63
|
+
::Rails::Sequel::Migrations.migrate_up!(args[:version])
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'Migrate down using migrations'
|
67
|
+
task :down, :version, :needs => :load do |t, args|
|
68
|
+
require 'sequel-rails/migrations'
|
69
|
+
::Rails::Sequel::Migrations.migrate_down!(args[:version])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# TODO: deal with this at some point
|
75
|
+
#
|
76
|
+
# namespace :schema do
|
77
|
+
# desc 'Create a db/schema.rb file that can be portably used against any DB supported by Sequel'
|
78
|
+
# task :dump => :environment do
|
79
|
+
# File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
|
80
|
+
# file.puts(::Sequel::Model.db.schema())
|
81
|
+
# end
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# desc 'Load a schema.rb file into the database'
|
85
|
+
# task :load => :environment do
|
86
|
+
#
|
87
|
+
# end
|
88
|
+
# end
|
89
|
+
|
90
|
+
desc 'Migrate the database to the latest version'
|
91
|
+
task :migrate => 'db:migrate:up'
|
92
|
+
|
93
|
+
namespace :sessions do
|
94
|
+
desc "Creates the sessions table for SequelStore"
|
95
|
+
task :create => :environment do
|
96
|
+
require 'sequel-rails/session_store'
|
97
|
+
Rails::Sequel::SessionStore::Session.auto_migrate!
|
98
|
+
puts "Created '#{::Rails::Sequel.configuration.environments[Rails.env]['database']}.sessions'"
|
99
|
+
end
|
100
|
+
|
101
|
+
desc "Clear the sessions table for SequelStore"
|
102
|
+
task :clear => :environment do
|
103
|
+
require 'sequel-rails/session_store'
|
104
|
+
Rails::Sequel::SessionStore::Session.delete()
|
105
|
+
puts "Deleted entries from '#{::Rails::Sequel.configuration.environments[Rails.env]['database']}.sessions'"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Sequel
|
2
|
+
module Railties
|
3
|
+
|
4
|
+
class LogSubscriber < Rails::LogSubscriber
|
5
|
+
|
6
|
+
def sql(event)
|
7
|
+
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
8
|
+
sql = event.payload[:sql].squeeze(' ')
|
9
|
+
|
10
|
+
if odd?
|
11
|
+
name = color(name, :cyan, true)
|
12
|
+
sql = color(sql, nil, true)
|
13
|
+
else
|
14
|
+
name = color(name, :magenta, true)
|
15
|
+
end
|
16
|
+
|
17
|
+
debug " #{name} #{sql}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def odd?
|
21
|
+
@odd_or_even = !@odd_or_even
|
22
|
+
end
|
23
|
+
|
24
|
+
def logger
|
25
|
+
::Sequel.logger
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
# Implements Sequel-specific session store.
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
module Sequel
|
7
|
+
|
8
|
+
class SessionStore < ActionDispatch::Session::AbstractStore
|
9
|
+
|
10
|
+
class Session < ::Sequel::Model
|
11
|
+
|
12
|
+
# property :id, Serial
|
13
|
+
# property :session_id, String, :required => true, :unique => true, :unique_index => true
|
14
|
+
# property :data, Object, :required => true, :default => ActiveSupport::Base64.encode64(Marshal.dump({}))
|
15
|
+
# property :updated_at, DateTime, :required => false, :index => true
|
16
|
+
|
17
|
+
class << self
|
18
|
+
|
19
|
+
def auto_migrate!
|
20
|
+
self.db.create_table :sessions do
|
21
|
+
primary_key :id
|
22
|
+
column :session_id, String,
|
23
|
+
:null => false,
|
24
|
+
:unique => true,
|
25
|
+
:index => true
|
26
|
+
|
27
|
+
column :data, :text,
|
28
|
+
:null => false
|
29
|
+
|
30
|
+
column :updated_at, DateTime,
|
31
|
+
:null => true,
|
32
|
+
:index => true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.name
|
39
|
+
'session'
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
SESSION_RECORD_KEY = 'rack.session.record'.freeze
|
45
|
+
|
46
|
+
cattr_accessor :session_class
|
47
|
+
self.session_class = Session
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def get_session(env, sid)
|
52
|
+
sid ||= generate_sid
|
53
|
+
session = find_session(sid)
|
54
|
+
env[SESSION_RECORD_KEY] = session
|
55
|
+
[ sid, session.data ]
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_session(env, sid, session_data)
|
59
|
+
session = get_session_resource(env, sid)
|
60
|
+
session.data = session_data
|
61
|
+
session.updated_at = Time.now if session.dirty?
|
62
|
+
session.save
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_session_resource(env, sid)
|
66
|
+
if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
|
67
|
+
env[SESSION_RECORD_KEY] = find_session(sid)
|
68
|
+
else
|
69
|
+
env[SESSION_RECORD_KEY] ||= find_session(sid)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def find_session(sid)
|
74
|
+
klass = self.class.session_class
|
75
|
+
|
76
|
+
klass.where(:session_id => sid).first || klass.new(:session_id => sid)
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|