Skip to content

Commit

Permalink
Second step in making thinking_sphinx JRuby compatible: specs with no…
Browse files Browse the repository at this point in the history
… failure
  • Loading branch information
lackac authored and pat committed Mar 30, 2009
1 parent 7a52722 commit 4fe7c93
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 35 deletions.
1 change: 0 additions & 1 deletion features/support/db/database.example.yml
@@ -1,4 +1,3 @@
adapter: mysql
username: root
host: localhost
password:
5 changes: 2 additions & 3 deletions features/support/db/mysql.rb
@@ -1,4 +1,3 @@
require 'active_record'
require 'active_record/connection_adapters/mysql_adapter'

Database = 'mysql'
Database = defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql'
require "active_record/connection_adapters/#{Database}_adapter"
5 changes: 2 additions & 3 deletions features/support/db/postgresql.rb
@@ -1,4 +1,3 @@
require 'active_record'
require 'active_record/connection_adapters/postgresql_adapter'

Database = 'postgresql'
Database = defined?(JRUBY_VERSION) ? 'jdbcpostgresql' : 'postgresql'
require "active_record/connection_adapters/#{Database}_adapter"
8 changes: 4 additions & 4 deletions lib/thinking_sphinx.rb
Expand Up @@ -129,10 +129,10 @@ def self.suppress_delta_output=(value)
# or if not using MySQL, this will return false.
#
def self.use_group_by_shortcut?
::ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter") &&
::ActiveRecord::Base.connection.is_a?(
::ActiveRecord::ConnectionAdapters::MysqlAdapter
) &&
!!(::ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter") &&
::ActiveRecord::Base.connection.is_a?(::ActiveRecord::ConnectionAdapters::MysqlAdapter) ||
defined?(JRUBY_VERSION) &&
::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql") &&
::ActiveRecord::Base.connection.select_all(
"SELECT @@global.sql_mode, @@session.sql_mode;"
).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? }
Expand Down
8 changes: 8 additions & 0 deletions lib/thinking_sphinx/adapters/abstract_adapter.rb
Expand Up @@ -16,6 +16,14 @@ def self.detect(model)
ThinkingSphinx::MysqlAdapter.new model
when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
ThinkingSphinx::PostgreSQLAdapter.new model
when "ActiveRecord::ConnectionAdapters::JdbcAdapter"
if model.connection.config[:adapter] == "jdbcmysql"
ThinkingSphinx::MysqlAdapter.new model
elsif model.connection.config[:adapter] == "jdbcpostgresql"
ThinkingSphinx::PostgreSQLAdapter.new model
else
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL"
end
else
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL, not #{model.connection.class.name}"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/thinking_sphinx/index.rb
Expand Up @@ -181,7 +181,7 @@ def initialize_from_builder(&block)
# and db:migrate). It's a bit hacky, but I can't think of a better way.
rescue StandardError => err
case err.class.name
when "Mysql::Error", "ActiveRecord::StatementInvalid"
when "Mysql::Error", "Java::JavaSql::SQLException", "ActiveRecord::StatementInvalid"
return
else
raise err
Expand Down Expand Up @@ -387,7 +387,7 @@ def to_sql(options={})
).join(", ") }
SQL

if @model.connection.class.name == "ActiveRecord::ConnectionAdapters::MysqlAdapter"
if @model.connection.class.name == "ActiveRecord::ConnectionAdapters::MysqlAdapter" or @model.respond_to?(:jdbcmysql_connection)
sql += " ORDER BY NULL"
end

Expand Down
7 changes: 3 additions & 4 deletions lib/thinking_sphinx/rails_additions.rb
Expand Up @@ -49,10 +49,9 @@ def quote_table_name(name) #:nodoc:
end
end

if ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter")
ActiveRecord::ConnectionAdapters::MysqlAdapter.send(
:include, ThinkingSphinx::MysqlQuotedTableName
) unless ActiveRecord::ConnectionAdapters::MysqlAdapter.instance_methods.include?("quote_table_name")
if ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter") or ActiveRecord::Base.respond_to?(:jdbcmysql_connection)
adapter = ActiveRecord::ConnectionAdapters.const_get(defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter)
adapter.send(:include, ThinkingSphinx::MysqlQuotedTableName) unless adapter.instance_methods.include?("quote_table_name")
end

module ThinkingSphinx
Expand Down
27 changes: 20 additions & 7 deletions spec/sphinx_helper.rb
@@ -1,9 +1,18 @@
require 'active_record'
require 'active_record/connection_adapters/mysql_adapter'
begin
require 'active_record/connection_adapters/postgresql_adapter'
rescue LoadError
# No postgres? no prob...
if defined?(JRUBY_VERSION)
require 'active_record/connection_adapters/jdbcmysql_adapter'
begin
require 'active_record/connection_adapters/jdbcpostgresql_adapter'
rescue LoadError
# No postgres? no prob...
end
else
require 'active_record/connection_adapters/mysql_adapter'
begin
require 'active_record/connection_adapters/postgresql_adapter'
rescue LoadError
# No postgres? no prob...
end
end
require 'yaml'

Expand All @@ -26,9 +35,13 @@ def initialize
@path = File.expand_path(File.dirname(__FILE__))
end

def mysql_adapter
defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql'
end

def setup_mysql
ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:adapter => mysql_adapter,
:database => 'thinking_sphinx',
:username => @username,
:password => @password,
Expand All @@ -52,7 +65,7 @@ def setup_sphinx
@configuration = ThinkingSphinx::Configuration.instance.reset
File.open("spec/fixtures/sphinx/database.yml", "w") do |file|
YAML.dump({@configuration.environment => {
:adapter => 'mysql',
:adapter => mysql_adapter,
:host => @host,
:database => "thinking_sphinx",
:username => @username,
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/thinking_sphinx/rails_additions_spec.rb
Expand Up @@ -53,7 +53,7 @@
describe ThinkingSphinx::AbstractQuotedTableName do
describe 'quote_table_name method' do
it 'calls quote_column_name' do
adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new('mysql')
adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql')
adapter.should_receive(:quote_column_name).with('messages')
adapter.quote_table_name('messages')
end
Expand All @@ -68,16 +68,16 @@

describe ThinkingSphinx::MysqlQuotedTableName do
describe "quote_table_name method" do
it 'calls quote_column_name' do
it 'correctly quotes' do
adapter = ActiveRecord::Base.connection
adapter.should_receive(:quote_column_name).with('messages').and_return('`message`')
adapter.quote_table_name('messages')
adapter.quote_table_name('thinking_sphinx.messages').should == "`thinking_sphinx`.`messages`"
end
end

describe "extends ActiveRecord::ConnectionAdapters::MysqlAdapter" do
it 'with quote_table_name' do
ActiveRecord::ConnectionAdapters::MysqlAdapter.instance_methods.include?("quote_table_name").should be_true
adapter = defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter
ActiveRecord::ConnectionAdapters.const_get(adapter).instance_methods.include?("quote_table_name").should be_true
end
end
end
Expand Down
16 changes: 10 additions & 6 deletions spec/unit/thinking_sphinx_spec.rb
Expand Up @@ -53,13 +53,15 @@

describe "use_group_by_shortcut? method" do
before :each do
unless ::ActiveRecord::ConnectionAdapters.const_defined?(:MysqlAdapter)
adapter = defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter
unless ::ActiveRecord::ConnectionAdapters.const_defined?(adapter)
pending "No MySQL"
return
end

@connection = ::ActiveRecord::ConnectionAdapters::MysqlAdapter.stub_instance(
:select_all => true
@connection = ::ActiveRecord::ConnectionAdapters.const_get(adapter).stub_instance(
:select_all => true,
:config => {:adapter => defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql'}
)
::ActiveRecord::Base.stub_method(
:connection => @connection
Expand Down Expand Up @@ -103,12 +105,14 @@

describe "if not using MySQL" do
before :each do
unless ::ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter)
adapter = defined?(JRUBY_VERSION) ? :JdbcAdapter : :PostgreSQLAdapter
unless ::ActiveRecord::ConnectionAdapters.const_defined?(adapter)
pending "No PostgreSQL"
return
end
@connection = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.stub_instance(
:select_all => true
@connection = ::ActiveRecord::ConnectionAdapters.const_get(adapter).stub_instance(
:select_all => true,
:config => {:adapter => defined?(JRUBY_VERSION) ? 'jdbcpostgresql' : 'postgresql'}
)
::ActiveRecord::Base.stub_method(
:connection => @connection
Expand Down

0 comments on commit 4fe7c93

Please sign in to comment.