Skip to content

Commit

Permalink
First pass at fixing JRuby tests
Browse files Browse the repository at this point in the history
See jruby/activerecord-jdbc-adapter#780.

Signed-off-by: Pierre-Alexandre Meyer <[email protected]>
  • Loading branch information
pierre committed Aug 11, 2017
1 parent c510b59 commit 60ffd0b
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 13 deletions.
114 changes: 114 additions & 0 deletions lib/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# https://github.com/jruby/activerecord-jdbc-adapter/issues/780
# https://github.com/rails/rails/commit/ae39b1a03d0a859be9d5342592c8936f89fcbacf

require 'arjdbc'
require 'arjdbc/mysql/adapter.rb'
require 'arjdbc/mysql/schema_creation.rb'

module ArJdbc
module MySQL

if defined? ::ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation
class SchemaCreation < ::ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation

def visit_ChangeColumnDefinition(o)
column = o.column
options = o.options
sql_type = type_to_sql(o.type, options)
change_column_sql = "CHANGE #{quote_column_name(column.name)} #{quote_column_name(options[:name])} #{sql_type}"
add_column_options!(change_column_sql, options.merge(:column => column))
add_column_position!(change_column_sql, options)
end

def column_options(o)
super
end
end

# @override
def add_column(table_name, column_name, type, options = {})
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options)}"
add_column_options!(add_column_sql, options)
add_column_position!(add_column_sql, options)
execute(add_column_sql)
end unless const_defined? :SchemaCreation

# @override
def change_column(table_name, column_name, type, options = {})
column = column_for(table_name, column_name)

unless options_include_default?(options)
# NOTE: no defaults for BLOB/TEXT columns with MySQL
options[:default] = column.default if type != :text && type != :binary
end

unless options.has_key?(:null)
options[:null] = column.null
end

change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options)}"
add_column_options!(change_column_sql, options)
add_column_position!(change_column_sql, options)
execute(change_column_sql)
end

# Maps logical Rails types to MySQL-specific data types.
def type_to_sql(type, limit: nil, precision: nil, scale: nil, **)
case type.to_s
when 'binary'
case limit
when 0..0xfff; "varbinary(#{limit})"
when nil; "blob"
when 0x1000..0xffffffff; "blob(#{limit})"
else raise(ActiveRecordError, "No binary type has character length #{limit}")
end
when 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)' # compatibility with MySQL default
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
when 'text'
case limit
when 0..0xff; 'tinytext'
when nil, 0x100..0xffff; 'text'
when 0x10000..0xffffff; 'mediumtext'
when 0x1000000..0xffffffff; 'longtext'
else raise(ActiveRecordError, "No text type has character length #{limit}")
end
when 'datetime'
return super unless precision

case precision
when 0..6; "datetime(#{precision})"
else raise(ActiveRecordError, "No datetime type has precision of #{precision}. The allowed range of precision is from 0 to 6.")
end
else
super
end
end
end

def prepare_column_options(column)
spec = super
spec.delete(:limit) if column.type == :boolean
spec
end

def translate_exception(exception, message)
return super unless exception.respond_to?(:errno)

case exception.errno
when 1062
::ActiveRecord::RecordNotUnique.new(message)
when 1452
::ActiveRecord::InvalidForeignKey.new(message)
else
super
end
end
end
end
4 changes: 4 additions & 0 deletions lib/kaui.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#lib_dir = File.expand_path("..", __FILE__)
#$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)

if defined?(JRUBY_VERSION)
require 'core_ext'
end

require "kaui/engine"

module Kaui
Expand Down
22 changes: 9 additions & 13 deletions test/dummy/config/database.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
development:
default: &default
adapter: mysql2
encoding: utf8
database: kaui
username: <%= ENV.fetch('MYSQL_USER', 'root') %>
password: <%= ENV.fetch('MYSQL_PASSWORD', 'root') %>
host: 127.0.0.1
port: 3306
pool: 25
timeout: 5000

development:
<<: *default
database: kaui

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
<<: *default
database: kaui_test
username: <%= ENV.fetch('MYSQL_USER', 'root') %>
password: <%= ENV.fetch('MYSQL_PASSWORD', 'root') %>
host: 127.0.0.1
port: 3306
pool: 25
timeout: 5000
8 changes: 8 additions & 0 deletions test/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# arjdbc is very broken on Rails 5
# https://github.com/jruby/activerecord-jdbc-adapter/issues/780
# https://github.com/rails/rails/commit/ae39b1a03d0a859be9d5342592c8936f89fcbacf
if defined?(JRUBY_VERSION)
config.active_record.migration_error = false
config.active_record.maintain_test_schema = false
end
end

0 comments on commit 60ffd0b

Please sign in to comment.