Skip to content

Commit d4e8689

Browse files
authored
Merge pull request #1043 from dr-itz/rails61-dev
Rails 6.1 fixes
2 parents b6b504d + 5d644bb commit d4e8689

30 files changed

+165
-178
lines changed

.travis.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ install:
1515

1616
language: ruby
1717
rvm:
18-
- jruby-9.2.7.0
18+
- jruby-9.2.9.0
1919
jdk:
2020
- openjdk8
2121

@@ -108,18 +108,18 @@ matrix:
108108
env: DB=mariadb PREPARED_STATEMENTS=true
109109

110110
# Rails test-suite :
111-
- env: DB=mysql2 TEST_PREFIX="rails:" AR_VERSION="6-0-stable" # PS off by default
112-
- env: DB=mysql2 TEST_PREFIX="rails:" AR_VERSION="6-0-stable" PREPARED_STATEMENTS=true
113-
- env: DB=mysql2 TEST_PREFIX="rails:" AR_VERSION="6-0-stable" DRIVER=MariaDB
111+
- env: DB=mysql2 TEST_PREFIX="rails:" AR_VERSION="master" # PS off by default
112+
- env: DB=mysql2 TEST_PREFIX="rails:" AR_VERSION="master" PREPARED_STATEMENTS=true
113+
- env: DB=mysql2 TEST_PREFIX="rails:" AR_VERSION="master" DRIVER=MariaDB
114114

115115
- addons:
116116
postgresql: "10"
117-
env: DB=postgresql TEST_PREFIX="rails:" AR_VERSION="6-0-stable" # PS on by default
117+
env: DB=postgresql TEST_PREFIX="rails:" AR_VERSION="master" # PS on by default
118118
- addons:
119119
postgresql: "10"
120-
env: DB=postgresql TEST_PREFIX="rails:" AR_VERSION="6-0-stable" PREPARED_STATEMENTS=false
120+
env: DB=postgresql TEST_PREFIX="rails:" AR_VERSION="master" PREPARED_STATEMENTS=false
121121
- addons:
122122
postgresql: "9.4"
123-
env: DB=postgresql TEST_PREFIX="rails:" AR_VERSION="6-0-stable" # PS on by default
123+
env: DB=postgresql TEST_PREFIX="rails:" AR_VERSION="master" # PS on by default
124124

125-
- env: DB=sqlite3 TEST_PREFIX="rails:" AR_VERSION="6-0-stable"
125+
- env: DB=sqlite3 TEST_PREFIX="rails:" AR_VERSION="master"

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ end
5959

6060
group :rails do
6161
group :test do
62-
gem 'minitest', '~> 5.11.3', require: nil
62+
gem 'minitest', '~> 5.12.2', require: nil
6363
gem 'minitest-excludes', '~> 2.0.1', require: nil
6464
gem 'minitest-rg', require: nil
6565

bench/setup.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22

33
require 'rubygems' unless defined? Gem
44

5-
gem 'activerecord', ENV['AR_VERSION'] if ENV['AR_VERSION']
5+
if ENV['RAILS'] # Use local clone of Rails
6+
rails_dir = ENV['RAILS']
7+
8+
activerecord_dir = ::File.join(rails_dir, 'activerecord', 'lib')
9+
if !::File.exist?(rails_dir) && !::File.exist?(activerecord_dir)
10+
raise "ENV['RAILS'] set but does not point at a valid rails clone"
11+
end
12+
puts "--- Using AR from #{activerecord_dir}"
13+
14+
$LOAD_PATH << ::File.join(rails_dir, 'activesupport', 'lib')
15+
$LOAD_PATH << ::File.join(rails_dir, 'activemodel', 'lib')
16+
$LOAD_PATH << activerecord_dir
17+
elsif ENV['AR_VERSION']
18+
gem 'activerecord', ENV['AR_VERSION']
19+
end
20+
621
require 'active_record'
722
require 'active_record/version'
823

@@ -12,7 +27,7 @@
1227
require 'arjdbc'
1328
ArJdbc::VERSION
1429
else
15-
$LOAD_PATH << File.expand_path('../lib', File.dirname(__FILE__))
30+
$LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__))
1631
require 'arjdbc'
1732
"#{ArJdbc::VERSION} #{`git rev-parse HEAD`[0, 8]}"
1833
end

lib/arjdbc/abstract/transaction_support.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ def supports_transaction_isolation?
2424
# Starts a database transaction.
2525
# @override
2626
def begin_db_transaction
27-
log('BEGIN TRANSACTION', nil) { @connection.begin }
27+
log('BEGIN', 'TRANSACTION') { @connection.begin }
2828
end
2929

3030
# Starts a database transaction.
3131
# @param isolation the transaction isolation to use
3232
def begin_isolated_db_transaction(isolation)
33-
log("BEGIN ISOLATED TRANSACTION - #{isolation}", nil) { @connection.begin(isolation) }
33+
log("BEGIN ISOLATED - #{isolation}", 'TRANSACTION') { @connection.begin(isolation) }
3434
end
3535

3636
# Commits the current database transaction.
3737
# @override
3838
def commit_db_transaction
39-
log('COMMIT TRANSACTION', nil) { @connection.commit }
39+
log('COMMIT', 'TRANSACTION') { @connection.commit }
4040
end
4141

4242
# Rolls back the current database transaction.
4343
# Called from 'rollback_db_transaction' in the AbstractAdapter
4444
# @override
4545
def exec_rollback_db_transaction
46-
log('ROLLBACK TRANSACTION', nil) { @connection.rollback }
46+
log('ROLLBACK', 'TRANSACTION') { @connection.rollback }
4747
end
4848

4949
########################## Savepoint Interface ############################
@@ -55,7 +55,7 @@ def exec_rollback_db_transaction
5555
# @since 1.3.0
5656
# @extension added optional name parameter
5757
def create_savepoint(name = current_savepoint_name)
58-
log("SAVEPOINT #{name}", 'Savepoint') { @connection.create_savepoint(name) }
58+
log("SAVEPOINT #{name}", 'TRANSACTION') { @connection.create_savepoint(name) }
5959
end
6060

6161
# Transaction rollback to a given (previously created) save-point.
@@ -64,7 +64,7 @@ def create_savepoint(name = current_savepoint_name)
6464
# @param name the save-point name
6565
# @extension added optional name parameter
6666
def exec_rollback_to_savepoint(name = current_savepoint_name)
67-
log("ROLLBACK TO SAVEPOINT #{name}", 'Savepoint') { @connection.rollback_savepoint(name) }
67+
log("ROLLBACK TO SAVEPOINT #{name}", 'TRANSACTION') { @connection.rollback_savepoint(name) }
6868
end
6969

7070
# Release a previously created save-point.
@@ -73,7 +73,7 @@ def exec_rollback_to_savepoint(name = current_savepoint_name)
7373
# @param name the save-point name
7474
# @extension added optional name parameter
7575
def release_savepoint(name = current_savepoint_name)
76-
log("RELEASE SAVEPOINT #{name}", 'Savepoint') { @connection.release_savepoint(name) }
76+
log("RELEASE SAVEPOINT #{name}", 'TRANSACTION') { @connection.release_savepoint(name) }
7777
end
7878

7979
end

lib/arjdbc/mysql/adapter.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class Mysql2Adapter < AbstractMysqlAdapter
3333

3434
include ArJdbc::MySQL
3535

36-
def initialize(connection, logger, connection_parameters, config)
37-
super
36+
def initialize(connection, logger, connection_options, config)
37+
superclass_config = config.reverse_merge(prepared_statements: false)
38+
super(connection, logger, connection_options, superclass_config)
3839

39-
@prepared_statements = false unless config.key?(:prepared_statements)
4040
# configure_connection taken care of at ArJdbc::Abstract::Core
4141
end
4242

@@ -96,6 +96,15 @@ def write_query?(sql) # :nodoc:
9696
!READ_QUERY.match?(sql)
9797
end
9898

99+
def explain(arel, binds = [])
100+
sql = "EXPLAIN #{to_sql(arel, binds)}"
101+
start = Concurrent.monotonic_time
102+
result = exec_query(sql, "EXPLAIN", binds)
103+
elapsed = Concurrent.monotonic_time - start
104+
105+
MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
106+
end
107+
99108
# Reloading the type map in abstract/statement_cache.rb blows up postgres
100109
def clear_cache!
101110
reload_type_map
@@ -179,7 +188,7 @@ def jdbc_column_class
179188

180189
# defined in MySQL::DatabaseStatements which is not included
181190
def default_insert_value(column)
182-
Arel.sql("DEFAULT") unless column.auto_increment?
191+
super unless column.auto_increment?
183192
end
184193

185194
# FIXME: optimize insert_fixtures_set by using JDBC Statement.addBatch()/executeBatch()

lib/arjdbc/postgresql/adapter.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ def supports_optimizer_hints?
303303
@has_pg_hint_plan
304304
end
305305

306+
def supports_common_table_expressions?
307+
true
308+
end
309+
306310
def supports_lazy_transactions?
307311
true
308312
end
@@ -379,13 +383,19 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
379383
end
380384
end
381385

386+
def execute_batch(statements, name = nil)
387+
execute(combine_multi_statements(statements), name)
388+
end
389+
382390
def explain(arel, binds = [])
383391
sql, binds = to_sql_and_binds(arel, binds)
384392
ActiveRecord::ConnectionAdapters::PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query("EXPLAIN #{sql}", 'EXPLAIN', binds))
385393
end
386394

387395
# from ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements
388-
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :commit, :explain, :select, :set, :show, :release, :savepoint, :rollback) # :nodoc:
396+
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
397+
:begin, :commit, :explain, :select, :set, :show, :release, :savepoint, :rollback, :with
398+
) # :nodoc:
389399
private_constant :READ_QUERY
390400

391401
def write_query?(sql) # :nodoc:
@@ -434,8 +444,8 @@ def build_insert_sql(insert) # :nodoc:
434444
sql
435445
end
436446

437-
def build_truncate_statements(*table_names)
438-
"TRUNCATE TABLE #{table_names.map(&method(:quote_table_name)).join(", ")}"
447+
def build_truncate_statements(table_names)
448+
["TRUNCATE TABLE #{table_names.map(&method(:quote_table_name)).join(", ")}"]
439449
end
440450

441451
def all_schemas

lib/arjdbc/postgresql/column.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def extract_default_function(default_value, default)
4848
end
4949

5050
def has_default_function?(default_value, default)
51-
!default_value && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
51+
!default_value && default && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
5252
end
5353
end
5454

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ def supports_json?
9797
true
9898
end
9999

100+
def supports_common_table_expressions?
101+
database_version >= "3.8.3"
102+
end
103+
100104
def supports_insert_on_conflict?
101105
database_version >= "3.24.0"
102106
end
@@ -154,7 +158,9 @@ def disable_referential_integrity # :nodoc:
154158
# DATABASE STATEMENTS ======================================
155159
#++
156160

157-
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :commit, :explain, :select, :pragma, :release, :savepoint, :rollback) # :nodoc:
161+
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
162+
:begin, :commit, :explain, :select, :pragma, :release, :savepoint, :rollback, :with
163+
) # :nodoc:
158164
private_constant :READ_QUERY
159165

160166
def write_query?(sql) # :nodoc:
@@ -181,15 +187,15 @@ def last_inserted_id(result)
181187
#def execute(sql, name = nil) #:nodoc:
182188

183189
def begin_db_transaction #:nodoc:
184-
log("begin transaction", nil) { @connection.transaction }
190+
log("begin transaction", 'TRANSACTION') { @connection.transaction }
185191
end
186192

187193
def commit_db_transaction #:nodoc:
188-
log("commit transaction", nil) { @connection.commit }
194+
log("commit transaction", 'TRANSACTION') { @connection.commit }
189195
end
190196

191197
def exec_rollback_db_transaction #:nodoc:
192-
log("rollback transaction", nil) { @connection.rollback }
198+
log("rollback transaction", 'TRANSACTION') { @connection.rollback }
193199
end
194200

195201
# SCHEMA STATEMENTS ========================================
@@ -199,8 +205,8 @@ def primary_keys(table_name) # :nodoc:
199205
pks.sort_by { |f| f["pk"] }.map { |f| f["name"] }
200206
end
201207

202-
def remove_index(table_name, options = {}) #:nodoc:
203-
index_name = index_name_for_remove(table_name, options)
208+
def remove_index(table_name, column_name, options = {}) #:nodoc:
209+
index_name = index_name_for_remove(table_name, column_name, options)
204210
exec_query "DROP INDEX #{quote_column_name(index_name)}"
205211
end
206212

@@ -209,14 +215,16 @@ def remove_index(table_name, options = {}) #:nodoc:
209215
# Example:
210216
# rename_table('octopuses', 'octopi')
211217
def rename_table(table_name, new_name)
218+
schema_cache.clear_data_source_cache!(table_name.to_s)
219+
schema_cache.clear_data_source_cache!(new_name.to_s)
212220
exec_query "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
213221
rename_table_indexes(table_name, new_name)
214222
end
215223

216-
def add_column(table_name, column_name, type, options = {}) #:nodoc:
224+
def add_column(table_name, column_name, type, **options) #:nodoc:
217225
if invalid_alter_table_type?(type, options)
218226
alter_table(table_name) do |definition|
219-
definition.column(column_name, type, options)
227+
definition.column(column_name, type, **options)
220228
end
221229
else
222230
super
@@ -317,11 +325,8 @@ def get_database_version # :nodoc:
317325
SQLite3Adapter::Version.new(query_value("SELECT sqlite_version(*)"))
318326
end
319327

320-
def build_truncate_statements(*table_names)
321-
truncate_tables = table_names.map do |table_name|
322-
"DELETE FROM #{quote_table_name(table_name)}"
323-
end
324-
combine_multi_statements(truncate_tables)
328+
def build_truncate_statement(table_name)
329+
"DELETE FROM #{quote_table_name(table_name)}"
325330
end
326331

327332
def check_version
@@ -365,7 +370,7 @@ def alter_table(table_name, foreign_keys = foreign_keys(table_name), **options)
365370
fk.options[:column] = column
366371
end
367372
to_table = strip_table_name_prefix_and_suffix(fk.to_table)
368-
definition.foreign_key(to_table, fk.options)
373+
definition.foreign_key(to_table, **fk.options)
369374
end
370375

371376
yield definition if block_given?
@@ -387,11 +392,12 @@ def move_table(from, to, options = {}, &block)
387392
def copy_table(from, to, options = {})
388393
from_primary_key = primary_key(from)
389394
options[:id] = false
390-
create_table(to, options) do |definition|
395+
create_table(to, **options) do |definition|
391396
@definition = definition
392397
if from_primary_key.is_a?(Array)
393398
@definition.primary_keys from_primary_key
394399
end
400+
395401
columns(from).each do |column|
396402
column_name = options[:rename] ?
397403
(options[:rename][column.name] ||

lib/arjdbc/tasks/databases.rake

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,31 @@ module ActiveRecord::Tasks
1313
ActiveRecord::Base.configurations.configs_for(env_name: env).each do |db_config|
1414
next if spec_name && spec_name != db_config.spec_name
1515

16-
yield adapt_jdbc_config(db_config.config), db_config.spec_name, env unless db_config.config['database'].blank?
16+
if db_config.database
17+
yield adapt_jdbc_config(db_config), db_config.spec_name, env
18+
end
1719
end
1820
end
1921
end
2022

2123
# @override patched to adapt jdbc configuration
2224
def each_local_configuration
2325
ActiveRecord::Base.configurations.configs_for.each do |db_config|
24-
next unless db_config.config['database']
26+
next unless db_config.database
2527

26-
if local_database?(db_config.config)
27-
yield adapt_jdbc_config(db_config.config)
28+
if local_database?(db_config)
29+
yield adapt_jdbc_config(db_config)
2830
else
29-
$stderr.puts "This task only modifies local databases. #{db_config.config['database']} is on a remote host."
31+
$stderr.puts "This task only modifies local databases. #{db_config.database} is on a remote host."
3032
end
3133
end
3234
end
3335

3436
private
3537

36-
def adapt_jdbc_config(config)
37-
return config unless config['adapter']
38-
config.merge 'adapter' => config['adapter'].sub(/^jdbc/, '')
38+
def adapt_jdbc_config(db_config)
39+
db_config.configuration_hash[:adapter] = db_config.adapter.sub(/^jdbc/, '') if db_config.adapter
40+
db_config
3941
end
4042

4143
end

0 commit comments

Comments
 (0)