Skip to content

Commit 9117182

Browse files
authored
Merge pull request #1169 from JesseChavez/rails_72_fixes_part2
More fixes to support Rails 7.2
2 parents f81626b + ea169fd commit 9117182

File tree

12 files changed

+385
-177
lines changed

12 files changed

+385
-177
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ adapters are available:
5656

5757
```yml
5858
development:
59-
adapter: mysql2 # or mysql
59+
adapter: mysql2
6060
database: blog_development
6161
username: blog
6262
password: 1234
@@ -80,7 +80,7 @@ or preferably using the *properties:* syntax:
8080
8181
```yml
8282
production:
83-
adapter: mysql
83+
adapter: mysql2
8484
username: blog
8585
password: blog
8686
url: "jdbc:mysql://localhost:3306/blog?profileSQL=true"

lib/arjdbc/abstract/core.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ def translate_exception(exception, message:, sql:, binds:)
6060
# this version of log() automatically fills type_casted_binds from binds if necessary
6161
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, async: false)
6262
if binds.any? && (type_casted_binds.nil? || type_casted_binds.empty?)
63-
type_casted_binds = ->{ binds.map(&:value_for_database) } # extract_raw_bind_values
63+
type_casted_binds = lambda {
64+
# extract_raw_bind_values
65+
binds.map do |bind|
66+
if bind.respond_to?(:value_for_database)
67+
bind.value_for_database
68+
else
69+
bind
70+
end
71+
end
72+
}
6473
end
6574
super
6675
end

lib/arjdbc/abstract/transaction_support.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def supports_transaction_isolation?
2626
def begin_db_transaction
2727
log('BEGIN', 'TRANSACTION') do
2828
with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
29-
conn.begin
29+
result = conn.begin
30+
verified!
31+
result
3032
end
3133
end
3234
end

lib/arjdbc/mysql/adapter_hash_config.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ def build_connection_config(config)
77

88
load_jdbc_driver
99

10-
config[:driver] ||= database_driver_name
10+
# don't set driver if it's explicitly set to false
11+
# allow Java's service discovery mechanism (with connector/j 8.0)
12+
config[:driver] ||= database_driver_name if config[:driver] != false
1113

1214
host = (config[:host] ||= "localhost")
1315
port = (config[:port] ||= 3306)
@@ -40,7 +42,7 @@ def database_driver_name
4042
def build_properties(config)
4143
properties = config[:properties] || {}
4244

43-
properties["zeroDateTimeBehavior"] ||= "CONVERT_TO_NULL"
45+
properties["zeroDateTimeBehavior"] ||= default_zero_date_time_behavior(config[:driver])
4446

4547
properties["jdbcCompliantTruncation"] ||= false
4648

@@ -88,6 +90,14 @@ def build_properties(config)
8890
properties
8991
end
9092

93+
def default_zero_date_time_behavior(driver)
94+
return "CONVERT_TO_NULL" if driver == false
95+
96+
return "CONVERT_TO_NULL" if driver.start_with?("com.mysql.cj.")
97+
98+
"convertToNull"
99+
end
100+
91101
# See https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html
92102
# to charset-name (characterEncoding=...)
93103
def convert_mysql_encoding(config)

lib/arjdbc/postgresql/adapter.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def enum_types
345345
type.typname AS name,
346346
type.OID AS oid,
347347
n.nspname AS schema,
348-
string_agg(enum.enumlabel, ',' ORDER BY enum.enumsortorder) AS value
348+
array_agg(enum.enumlabel ORDER BY enum.enumsortorder) AS value
349349
FROM pg_enum AS enum
350350
JOIN pg_type AS type ON (type.oid = enum.enumtypid)
351351
JOIN pg_namespace n ON type.typnamespace = n.oid
@@ -842,6 +842,15 @@ class PostgreSQLAdapter < AbstractAdapter
842842
# setting, you should immediately run <tt>bin/rails db:migrate</tt> to update the types in your schema.rb.
843843
class_attribute :datetime_type, default: :timestamp
844844

845+
##
846+
# :singleton-method:
847+
# Toggles automatic decoding of date columns.
848+
#
849+
# ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.select_value("select '2024-01-01'::date").class #=> String
850+
# ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.decode_dates = true
851+
# ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.select_value("select '2024-01-01'::date").class #=> Date
852+
class_attribute :decode_dates, default: false
853+
845854
# Try to use as much of the built in postgres logic as possible
846855
# maybe someday we can extend the actual adapter
847856
include ActiveRecord::ConnectionAdapters::PostgreSQL::ReferentialIntegrity
@@ -855,9 +864,12 @@ class PostgreSQLAdapter < AbstractAdapter
855864
include ArJdbc::Abstract::DatabaseStatements
856865
include ArJdbc::Abstract::StatementCache
857866
include ArJdbc::Abstract::TransactionSupport
858-
include ArJdbc::PostgreSQL
859867
include ArJdbc::PostgreSQLConfig
860868

869+
# NOTE: after AR refactor quote_column_name became class and instance method
870+
include ArJdbc::PostgreSQL
871+
extend ArJdbc::PostgreSQL
872+
861873
require 'arjdbc/postgresql/oid_types'
862874
include ::ArJdbc::PostgreSQL::OIDTypes
863875
include ::ArJdbc::PostgreSQL::DatabaseStatements

lib/arjdbc/postgresql/adapter_hash_config.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ def build_connection_config(config)
1313
port = (config[:port] ||= ENV["PGPORT"] || 5432)
1414
database = config[:database] || config[:dbname] || ENV["PGDATABASE"]
1515

16-
config[:url] ||= "jdbc:postgresql://#{host}:#{port}/#{database}"
16+
app = config[:application_name] || config[:appname] || config[:application]
17+
18+
config[:url] ||= if app
19+
"jdbc:postgresql://#{host}:#{port}/#{database}?ApplicationName=#{app}"
20+
else
21+
"jdbc:postgresql://#{host}:#{port}/#{database}"
22+
end
1723

1824
config[:url] << config[:pg_params] if config[:pg_params]
1925

0 commit comments

Comments
 (0)