sequel-rails-cartodb 0.1.7

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 +15 -0
  4. data/Gemfile +21 -0
  5. data/LICENSE +20 -0
  6. data/README.rdoc +86 -0
  7. data/Rakefile +33 -0
  8. data/VERSION +1 -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 +1 -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 +27 -0
  29. data/lib/sequel-rails/storage.rb +251 -0
  30. data/sequel-rails.gemspec +93 -0
  31. data/spec/rcov.opts +6 -0
  32. data/spec/setup_spec.rb +7 -0
  33. data/spec/spec.opts +4 -0
  34. data/spec/spec_helper.rb +21 -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 +38 -0
  39. data/tasks/yard.rake +9 -0
  40. data/tasks/yardstick.rake +20 -0
  41. metadata +160 -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,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.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 => :environment do
6
+ Sequel.extension :schema_dumper
7
+ db = Sequel.connect(Rails.configuration.database_configuration[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'
@@ -0,0 +1,12 @@
1
+ module Rails
2
+ module Sequel
3
+
4
+ module I18nSupport
5
+ # Set the i18n scope to overwrite ActiveModel.
6
+ def i18n_scope #:nodoc:
7
+ :sequel
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module Sequel
2
+ module Railties
3
+
4
+ class LogSubscriber < ActiveSupport::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
+ ::Rails::Sequel.configuration.logger
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module Rails
2
+ module Sequel
3
+
4
+ class << self
5
+ def reset_runtime
6
+ @runtime ||= 0
7
+
8
+ rt, @runtime = @runtime, 0
9
+ rt
10
+ end
11
+ end
12
+
13
+ end
14
+ 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