Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #126 from lomba/jruby
Browse files Browse the repository at this point in the history
* Get specs to run with JRuby + Rails 3.2 + PostgreSQL.
  • Loading branch information
donv committed Sep 20, 2013
2 parents df7a925 + c16a094 commit 3f91215
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 26 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rvm:
- 1.9.3
- 2.0.0
- jruby
gemfile:
- gemfiles/rails-3.2/Gemfile.postgresql
- gemfiles/rails-3.2/Gemfile.sqlite3
Expand All @@ -26,3 +27,31 @@ matrix:
- gemfile: gemfiles/rails-edge/Gemfile.postgresql
- gemfile: gemfiles/rails-edge/Gemfile.sqlite3
- gemfile: gemfiles/rails-edge/Gemfile.mysql2
exclude:
- rvm: jruby
gemfile: gemfiles/rails-3.2/Gemfile.sqlite3
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-3.2/Gemfile.mysql
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-3.2/Gemfile.mysql2
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-4.0/Gemfile.postgresql
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-4.0/Gemfile.sqlite3
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-4.0/Gemfile.mysql2
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-edge/Gemfile.postgresql
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-edge/Gemfile.sqlite3
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
- rvm: jruby
gemfile: gemfiles/rails-edge/Gemfile.mysql2
env: 'POSTGRES_DB_USER=postgres MYSQL_DB_USER=""'
8 changes: 7 additions & 1 deletion gemfiles/rails-3.2/Gemfile.mysql
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "mysql", "~> 2.8.1"
platform :ruby do
gem "mysql", "~> 2.8.1"
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-3.2/Gemfile.mysql2
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "mysql2"
platform :ruby do
gem "mysql2"
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-3.2/Gemfile.postgresql
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "pg"
platform :ruby do
gem "pg"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-3.2/Gemfile.sqlite3
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "sqlite3"
platform :ruby do
gem "sqlite3"
end

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-4.0/Gemfile.mysql2
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "mysql2"
platform :ruby do
gem "mysql2"
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-4.0/Gemfile.postgresql
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "pg"
platform :ruby do
gem "pg"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-4.0/Gemfile.sqlite3
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "sqlite3"
platform :ruby do
gem "sqlite3"
end

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-edge/Gemfile.mysql2
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "mysql2"
platform :ruby do
gem "mysql2"
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-edge/Gemfile.postgresql
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "pg"
platform :ruby do
gem "pg"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
8 changes: 7 additions & 1 deletion gemfiles/rails-edge/Gemfile.sqlite3
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

gem "sqlite3"
platform :ruby do
gem "sqlite3"
end

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ module PostgreSQLColumn
# Extracts the value from a PostgreSQL column default definition.
def self.included(base) #:nodoc:
base.extend ClassMethods
base.class_eval do
class << self
alias_method_chain :extract_value_from_default, :schema_plus
if defined?(JRUBY_VERSION)
base.alias_method_chain :default_value, :schema_plus
else
base.class_eval do
class << self
alias_method_chain :extract_value_from_default, :schema_plus
end
end
end
end
Expand All @@ -23,16 +27,22 @@ def initialize(name, default, sql_type = nil, null = true)
super(name, default, sql_type, null)
end

def default_value_with_schema_plus(default)
value = default_value_without_schema_plus(default)
self.class.convert_default_value(default, value)
end

module ClassMethods
def extract_value_from_default_with_schema_plus(default)


value = extract_value_from_default_without_schema_plus(default)
convert_default_value(default, value)
end

# in some cases (e.g. if change_column_default(table, column,
# nil) is used), postgresql will return NULL::xxxxx (rather
# than nil) for a null default -- make sure we treat it as nil,
# not as a function.
# in some cases (e.g. if change_column_default(table, column,
# nil) is used), postgresql will return NULL::xxxxx (rather
# than nil) for a null default -- make sure we treat it as nil,
# not as a function.
def convert_default_value(default, value)
default = nil if value.nil? && default =~ /\ANULL::(?:character varying|bpchar|text)\z/m

if value.nil? && !default.nil?
Expand All @@ -49,11 +59,11 @@ module PostgresqlAdapter

def self.included(base) #:nodoc:
base.class_eval do
if ::ActiveRecord::VERSION::MAJOR.to_i < 4
if ::ActiveRecord::VERSION::MAJOR.to_i < 4 && !defined?(JRUBY_VERSION)
remove_method :indexes
end
alias_method_chain :rename_table, :schema_plus
alias_method_chain :exec_cache, :schema_plus
alias_method_chain :exec_cache, :schema_plus unless defined?(JRUBY_VERSION)
end
::ActiveRecord::ConnectionAdapters::PostgreSQLColumn.send(:include, PostgreSQLColumn) unless ::ActiveRecord::ConnectionAdapters::PostgreSQLColumn.include?(PostgreSQLColumn)
end
Expand Down Expand Up @@ -130,7 +140,7 @@ def indexes(table_name, name = nil) #:nodoc:
result = query(<<-SQL, name)
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,
m.amname, pg_get_expr(d.indpred, t.oid), pg_get_expr(d.indexprs, t.oid)
m.amname, pg_get_expr(d.indpred, t.oid) as conditions, pg_get_expr(d.indexprs, t.oid) as expression
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
INNER JOIN pg_class i ON d.indexrelid = i.oid
Expand All @@ -147,7 +157,7 @@ def indexes(table_name, name = nil) #:nodoc:
index_keys = indkey.split(" ")

columns = Hash[query(<<-SQL, "Columns for index #{index_name} on #{table_name}")]
SELECT a.attnum, a.attname
SELECT CAST(a.attnum as VARCHAR), a.attname
FROM pg_attribute a
WHERE a.attrelid = #{oid}
AND a.attnum IN (#{index_keys.join(",")})
Expand All @@ -168,7 +178,7 @@ def indexes(table_name, name = nil) #:nodoc:
case_sensitive = false
end
end

# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
orders = desc_order_columns.any? ? Hash[column_names.map {|column| [column, desc_order_columns.include?(column) ? :desc : :asc]}] : {}
Expand All @@ -184,6 +194,10 @@ def indexes(table_name, name = nil) #:nodoc:
end
end

def query(*args)
select(*args).map(&:values)
end if defined?(JRUBY_VERSION)

def rename_table_with_schema_plus(oldname, newname) #:nodoc:
rename_table_without_schema_plus(oldname, newname)
rename_indexes_and_foreign_keys(oldname, newname)
Expand Down
4 changes: 2 additions & 2 deletions spec/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ::Comment < ActiveRecord::Base ; end
if SchemaPlusHelpers.postgresql?
it "should dump the default hash expr as now()" do
with_additional_column Post, :posted_at, :datetime, :default => :now do
dump_posts.should match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"now\(\)"\s*\}})
dump_posts.should match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"now\(\)"\s*\}})
end
end

Expand All @@ -103,7 +103,7 @@ class ::Comment < ActiveRecord::Base ; end

it "can dump a complex default expression" do
with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do
dump_posts.should match(%r{t\.string\s+"name",\s*(?:default:|:default =>)\s*{\s*(?:expr:|:expr =>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
dump_posts.should match(%r{t\.string\s+"name",\s*(?:default:|:default\s*=>)\s*{\s*(?:expr:|:expr\s*=>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
end
end
end
Expand Down

0 comments on commit 3f91215

Please sign in to comment.