talentbox-sequel-rails 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.document +5 -0
  2. data/.gitignore +29 -0
  3. data/CHANGELOG +21 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +114 -0
  6. data/LICENSE +20 -0
  7. data/README.rdoc +86 -0
  8. data/Rakefile +9 -0
  9. data/autotest/discover.rb +1 -0
  10. data/lib/generators/sequel.rb +83 -0
  11. data/lib/generators/sequel/migration/migration_generator.rb +30 -0
  12. data/lib/generators/sequel/migration/templates/migration.rb +16 -0
  13. data/lib/generators/sequel/model/model_generator.rb +23 -0
  14. data/lib/generators/sequel/model/templates/model.rb +3 -0
  15. data/lib/generators/sequel/observer/observer_generator.rb +19 -0
  16. data/lib/generators/sequel/observer/templates/observer.rb +7 -0
  17. data/lib/sequel-rails.rb +7 -0
  18. data/lib/sequel-rails/configuration.rb +63 -0
  19. data/lib/sequel-rails/migrations.rb +30 -0
  20. data/lib/sequel-rails/railtie.rb +90 -0
  21. data/lib/sequel-rails/railties/benchmarking_mixin.rb +23 -0
  22. data/lib/sequel-rails/railties/controller_runtime.rb +43 -0
  23. data/lib/sequel-rails/railties/database.rake +167 -0
  24. data/lib/sequel-rails/railties/i18n_support.rb +12 -0
  25. data/lib/sequel-rails/railties/log_subscriber.rb +31 -0
  26. data/lib/sequel-rails/runtime.rb +14 -0
  27. data/lib/sequel-rails/session_store.rb +82 -0
  28. data/lib/sequel-rails/setup.rb +17 -0
  29. data/lib/sequel-rails/storage.rb +221 -0
  30. data/lib/sequel-rails/version.rb +5 -0
  31. data/sequel-rails.gemspec +29 -0
  32. data/spec/rcov.opts +6 -0
  33. data/spec/spec.opts +4 -0
  34. data/spec/spec_helper.rb +7 -0
  35. data/tasks/ci.rake +1 -0
  36. data/tasks/clean.rake +6 -0
  37. data/tasks/metrics.rake +37 -0
  38. data/tasks/spec.rake +30 -0
  39. data/tasks/yard.rake +9 -0
  40. data/tasks/yardstick.rake +20 -0
  41. metadata +177 -0
@@ -0,0 +1,3 @@
1
+ class <%= class_name %><%= options[:parent] ? " < #{options[:parent].classify}" : " < Sequel::Model" %>
2
+
3
+ end
@@ -0,0 +1,19 @@
1
+ require 'generators/sequel'
2
+
3
+ module Sequel
4
+ module Generators
5
+
6
+ class ObserverGenerator < Base
7
+
8
+ check_class_collision :suffix => "Observer"
9
+
10
+ def create_observer_file
11
+ template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
12
+ end
13
+
14
+ hook_for :test_framework
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class <%= class_name %>Observer
2
+
3
+ include Sequel::Observer
4
+
5
+ observe <%= class_name %>
6
+
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'sequel-rails/railtie'
2
+
3
+ module Rails
4
+ module Sequel
5
+ end
6
+ end
7
+
@@ -0,0 +1,63 @@
1
+ require 'active_support/core_ext/hash/except'
2
+ require 'active_support/core_ext/class/attribute_accessors'
3
+
4
+ module Rails
5
+ module Sequel
6
+
7
+ mattr_accessor :configuration
8
+
9
+ class Configuration
10
+
11
+ def self.for(root, database_yml_hash)
12
+ Rails::Sequel.configuration ||= new(root, database_yml_hash)
13
+ end
14
+
15
+ attr_reader :root, :raw
16
+ attr_accessor :logger
17
+ attr_accessor :migration_dir
18
+
19
+ def environment_for(name)
20
+ environments[name.to_s] || environments[name.to_sym]
21
+ end
22
+
23
+ def environments
24
+ @environments ||= @raw.inject({}) do |normalized, environment|
25
+ name, config = environment.first, environment.last
26
+ normalized[name] = normalize_repository_config(config)
27
+ normalized
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def initialize(root, database_yml_hash)
34
+ @root, @raw = root, database_yml_hash
35
+ end
36
+
37
+ def normalize_repository_config(hash)
38
+ config = {}
39
+ hash.each do |key, value|
40
+ config[key.to_s] =
41
+ if key.to_s == 'port'
42
+ value.to_i
43
+ elsif key.to_s == 'adapter' && value == 'sqlite3'
44
+ 'sqlite'
45
+ elsif key.to_s == 'database' && (hash['adapter'] == 'sqlite3' ||
46
+ hash['adapter'] == 'sqlite' ||
47
+ hash[:adapter] == 'sqlite3' ||
48
+ hash[:adapter] == 'sqlite')
49
+ value == ':memory:' ? value : File.expand_path((hash['database'] || hash[:database]), root)
50
+ elsif key.to_s == 'adapter' && value == 'postgresql'
51
+ 'postgres'
52
+ else
53
+ value
54
+ end
55
+ end
56
+
57
+ config
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
@@ -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,90 @@
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
+ ::Sequel::Railties::LogSubscriber.attach_to :sequel
25
+
26
+ config.app_generators.orm :sequel, :migration => true
27
+ config.rails_fancy_pants_logging = true
28
+
29
+ rake_tasks do
30
+ load 'sequel-rails/railties/database.rake'
31
+ end
32
+
33
+ initializer 'sequel.configuration' do |app|
34
+ configure_sequel(app)
35
+ end
36
+
37
+ initializer 'sequel.logger' do |app|
38
+ setup_logger(app, Rails.logger)
39
+ end
40
+
41
+ initializer 'sequel.i18n_support' do |app|
42
+ setup_i18n_support(app)
43
+ end
44
+
45
+ # Expose database runtime to controller for logging.
46
+ initializer "sequel.log_runtime" do |app|
47
+ setup_controller_runtime(app)
48
+ end
49
+
50
+ initializer "sequel.connect" do |app|
51
+ Rails::Sequel.setup(Rails.env)
52
+ end
53
+
54
+ # Run setup code after_initialize to make sure all config/initializers
55
+ # are in effect once we setup the connection. This is especially necessary
56
+ # for the cascaded adapter wrappers that need to be declared before setup.
57
+
58
+ config.after_initialize do |app|
59
+ ::Sequel::Model.plugin :active_model
60
+ ::Sequel::Model.plugin :validation_helpers
61
+
62
+ ::Sequel::Model.raise_on_save_failure = false
63
+ end
64
+
65
+
66
+ # Support overwriting crucial steps in subclasses
67
+
68
+ def configure_sequel(app)
69
+ app.config.sequel = Rails::Sequel::Configuration.for(
70
+ Rails.root, app.config.database_configuration
71
+ )
72
+ end
73
+
74
+ def setup_i18n_support(app)
75
+ ::Sequel::Model.send :include, Rails::Sequel::I18nSupport
76
+ end
77
+
78
+ def setup_controller_runtime(app)
79
+ require "sequel-rails/railties/controller_runtime"
80
+ ActionController::Base.send :include, Rails::Sequel::Railties::ControllerRuntime
81
+ end
82
+
83
+ def setup_logger(app, logger)
84
+ app.config.sequel.logger=logger
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+ 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,43 @@
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
+ db_rt_before_render = ::Rails::Sequel.reset_runtime
17
+ runtime = super
18
+ db_rt_after_render = ::Rails::Sequel.reset_runtime
19
+ self.db_runtime = db_rt_before_render + db_rt_after_render
20
+ runtime - db_rt_after_render
21
+ end
22
+
23
+ def append_info_to_payload(payload)
24
+ super
25
+ payload[:db_runtime] = db_runtime
26
+ end
27
+
28
+
29
+ module ClassMethods
30
+
31
+ def log_process_action(payload)
32
+ messages, db_runtime = super, payload[:db_runtime]
33
+ messages << ("Models: %.1fms" % db_runtime.to_f) if db_runtime
34
+ messages
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,167 @@
1
+ # TODO: DRY these up
2
+ namespace :db do
3
+ namespace :schema do
4
+ desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel"
5
+ task :dump do
6
+ Sequel.extension :schema_dumper
7
+ db = Sequel.connect(::Rails::Sequel.configuration.environment_for(Rails.env))
8
+ File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
9
+ file.write(db.dump_schema_migration)
10
+ end
11
+ Rake::Task["db:schema:dump"].reenable
12
+ end
13
+
14
+ desc "Load a schema.rb file into the database"
15
+ task :load => :environment do
16
+ require 'sequel-rails/storage'
17
+ Rails::Sequel::Storage.new(Rails.env).create
18
+
19
+ file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
20
+ if File.exists?(file)
21
+ load(file)
22
+ else
23
+ abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/boot.rb to limit the frameworks that will be loaded}
24
+ end
25
+ end
26
+ end
27
+
28
+ namespace :create do
29
+ desc 'Create all the local databases defined in config/database.yml'
30
+ task :all => :environment do
31
+ require 'sequel-rails/storage'
32
+ Rails::Sequel::Storage.create_all
33
+ end
34
+ end
35
+
36
+ desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
37
+ task :create, [:env] => :environment do |t, args|
38
+ args.with_defaults(:env => Rails.env)
39
+
40
+ require 'sequel-rails/storage'
41
+ Rails::Sequel::Storage.new(args.env).create
42
+
43
+ if Rails.env.development? && Rails.configuration.database_configuration['test']
44
+ Rails::Sequel::Storage.new('test').create
45
+ end
46
+ end
47
+
48
+ namespace :drop do
49
+ desc 'Drops all the local databases defined in config/database.yml'
50
+ task :all => :environment do
51
+ require 'sequel-rails/storage'
52
+ Rails::Sequel::Storage.drop_all
53
+ end
54
+ end
55
+
56
+ desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?"
57
+ task :drop, [:env] => :environment do |t, args|
58
+ args.with_defaults(:env => Rails.env)
59
+
60
+ require 'sequel-rails/storage'
61
+ Rails::Sequel::Storage.new(args.env).drop
62
+
63
+ if Rails.env.development? && Rails.configuration.database_configuration['test']
64
+ Rails::Sequel::Storage.new('test').drop
65
+ end
66
+ end
67
+
68
+ namespace :migrate do
69
+ task :load => :environment do
70
+ require 'sequel-rails/migrations'
71
+ end
72
+
73
+ 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.'
74
+ task :redo => :load do
75
+ if ENV["VERSION"]
76
+ Rake::Task["db:migrate:down"].invoke
77
+ Rake::Task["db:migrate:up"].invoke
78
+ else
79
+ Rake::Task["db:rollback"].invoke
80
+ Rake::Task["db:migrate"].invoke
81
+ end
82
+ end
83
+
84
+
85
+ desc 'Resets your database using your migrations for the current environment'
86
+ task :reset => ["db:drop", "db:create", "db:migrate"]
87
+
88
+ desc 'Runs the "up" for a given migration VERSION.'
89
+ task :up => :load do
90
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
91
+ raise "VERSION is required" unless version
92
+ Rails::Sequel::Migrations.migrate_up!(version)
93
+ Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
94
+ end
95
+
96
+ desc 'Runs the "down" for a given migration VERSION.'
97
+ task :down => :load do
98
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
99
+ raise "VERSION is required" unless version
100
+ Rails::Sequel::Migrations.migrate_down!(version)
101
+ Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
102
+ end
103
+ end
104
+
105
+ desc 'Migrate the database to the latest version'
106
+ task :migrate => :'migrate:load' do
107
+ Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
108
+ Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
109
+ end
110
+
111
+ desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
112
+ task :rollback => :'migrate:load' do
113
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
114
+ Sequel::Migrator.rollback('db/migrate/', step)
115
+ Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
116
+ end
117
+
118
+ desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n'
119
+ task :forward => :'migrate:load' do
120
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
121
+ Sequel::Migrator.forward('db/migrate/', step)
122
+ Rake::Task["db:schema:dump"].invoke if Rails.env != 'test'
123
+ end
124
+
125
+ desc 'Load the seed data from db/seeds.rb'
126
+ task :seed => :environment do
127
+ seed_file = File.join(Rails.root, 'db', 'seeds.rb')
128
+ load(seed_file) if File.exist?(seed_file)
129
+ end
130
+
131
+ desc 'Create the database, load the schema, and initialize with the seed data'
132
+ task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]
133
+
134
+ desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
135
+ task :reset => [ 'db:drop', 'db:setup' ]
136
+
137
+ desc 'Forcibly close any open connections to the test database'
138
+ task :force_close_open_connections => :environment do
139
+ if Rails.env.test?
140
+ db_config = Rails.configuration.database_configuration[Rails.env].symbolize_keys
141
+ begin
142
+ #Will only work on Postgres > 8.4
143
+ Sequel::Model.db.execute <<-SQL.gsub(/^\s{9}/,'')
144
+ SELECT COUNT(pg_terminate_backend(procpid))
145
+ FROM pg_stat_activity
146
+ WHERE datname = '#{db_config[:database]}';
147
+ SQL
148
+ rescue => e
149
+ #Will raise an error as it kills existing process running this command
150
+ #Seems to be only way to ensure *all* test connections are closed
151
+ end
152
+ end
153
+ end
154
+
155
+ namespace :test do
156
+ task :prepare do
157
+ Rails.env = 'test'
158
+ Rake::Task['db:force_close_open_connections'].invoke()
159
+ Rake::Task['db:reset'].invoke()
160
+ Sequel::DATABASES.each do |db|
161
+ db.disconnect
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+ task 'test:prepare' => 'db:test:prepare'