Skip to content

Commit

Permalink
Normal adapter names work for future adapter lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
leoasis committed May 23, 2013
1 parent 04ef9d8 commit 846cff8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.bundle
.config
.yardoc
*.sqlite3
Gemfile.lock
InstalledFiles
_yardoc
Expand Down
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ language: ruby
rvm:
- 1.9.3
env:
- ADAPTER=future_enabled_mysql2 activerecord=3.2.11
- ADAPTER=future_enabled_mysql2 activerecord=3.2.12
- ADAPTER=future_enabled_mysql2 activerecord=3.2.13
- ADAPTER=mysql2 activerecord=3.2.11
- ADAPTER=mysql2 activerecord=3.2.12
- ADAPTER=mysql2 activerecord=3.2.13
- ADAPTER=future_enabled_postgresql activerecord=3.2.11
- ADAPTER=future_enabled_postgresql activerecord=3.2.12
- ADAPTER=future_enabled_postgresql activerecord=3.2.13
- ADAPTER=future_enabled_mysql2 activerecord=3.2.13
- ADAPTER=postgresql activerecord=3.2.11
- ADAPTER=postgresql activerecord=3.2.12
- ADAPTER=postgresql activerecord=3.2.13
- ADAPTER=future_enabled_postgresql activerecord=3.2.13
- ADAPTER=sqlite3 activerecord=3.2.11
- ADAPTER=sqlite3 activerecord=3.2.12
- ADAPTER=sqlite3 activerecord=3.2.13

before_script:
- mysql -e 'create database activerecord_futures_test;'
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RSpec::Core::RakeTask.new(:spec)

task :default => :spec

ADAPTERS = %w(future_enabled_mysql2 future_enabled_postgresql postgresql mysql2)
ADAPTERS = %w(future_enabled_postgresql future_enabled_mysql2 postgresql mysql2 sqlite3)

desc "Runs the specs with all databases"
task :all do
Expand Down
1 change: 1 addition & 0 deletions activerecord-futures.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rspec-spies'
gem.add_development_dependency 'mysql2', '>= 0.3.12.b1'
gem.add_development_dependency 'pg'
gem.add_development_dependency 'sqlite3'
end
27 changes: 27 additions & 0 deletions lib/activerecord-futures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,31 @@ class Relation
class Base
extend Futures::Delegation
end
end

class ActiveRecord::Base::ConnectionSpecification
class Resolver
def spec_with_futures
spec = spec_without_futures
begin
config = spec.config
future_adapter_name = "future_enabled_#{config[:adapter]}"

# Try to load the future version of the adapter
require "active_record/connection_adapters/#{future_adapter_name}_adapter"

config[:adapter] = future_adapter_name
adapter_method = "future_enabled_#{spec.adapter_method}"

# Return the specification with the future adapter instead
ActiveRecord::Base::ConnectionSpecification.new(config, adapter_method)
rescue LoadError
# No future version of the adapter, or the adapter was already a future
# one. Keep going as usual...
spec
end
end

alias_method_chain :spec, :futures
end
end
7 changes: 7 additions & 0 deletions spec/in_action/future_find_execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
arel.to_sql
end

let(:future_sql_sqlite3) do
arel = relation.arel
arel.constraints.unshift(Arel.sql('"posts"."id" = ?'))
arel.limit = 1
arel.to_sql
end

before do
Post.create(published_at: Time.new(2012, 12, 10))
Post.create(published_at: Time.new(2012, 6, 23))
Expand Down
23 changes: 16 additions & 7 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
require 'activerecord-futures'

configs = {
future_enabled_postgresql: {
adapter: "postgresql",
database: "activerecord_futures_test",
username: "postgres"
},
future_enabled_mysql2: {
adapter: "future_enabled_mysql2",
adapter: "mysql2",
database: "activerecord_futures_test",
username: "root",
encoding: "utf8"
},
future_enabled_postgresql: {
adapter: "future_enabled_postgresql",
database: "activerecord_futures_test",
username: "postgres"
},
postgresql: {
adapter: "postgresql",
database: "activerecord_futures_test",
Expand All @@ -25,6 +25,10 @@
database: "activerecord_futures_test",
username: "root",
encoding: "utf8"
},
sqlite3: {
adapter: "sqlite3",
database: ':memory:'
}
}

Expand All @@ -38,6 +42,8 @@
ActiveRecord::Base.connection.respond_to?(:supports_futures?) &&
ActiveRecord::Base.connection.supports_futures?

puts "Supports futures!" if supports_futures

require 'db/schema'
Dir['./spec/models/**/*.rb'].each { |f| require f }

Expand All @@ -50,7 +56,10 @@
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.filter_run_excluding(supports_futures ? :not_supporting_adapter : :supporting_adapter)
config.filter_run_excluding(postgresql: config_key.to_s.include?("postgresql") ? false : true)

%w(postgresql mysql2 sqlite3).each do |adapter|
config.filter_run_excluding(adapter.to_sym => !config_key.to_s.include?(adapter))
end
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
Expand Down
7 changes: 6 additions & 1 deletion spec/support/futurized_method_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
calling_future.should exec(1).query
end

specify(nil, postgresql: false) do
specify(nil, postgresql: false, sqlite3: false) do
calling_future.should exec_query(future_sql)
end

Expand All @@ -18,6 +18,11 @@
calling_future.should exec_query(sql)
end

specify(nil, sqlite3: true) do
sql = respond_to?(:future_sql_sqlite3) ? future_sql_sqlite3 : future_sql
calling_future.should exec_query(sql)
end

specify { future.send(exec_trigger).should eq relation_result }

context "after executing the future" do
Expand Down

0 comments on commit 846cff8

Please sign in to comment.