Skip to content

Commit 2d1e276

Browse files
committed
:wfix: Add Rails 8.0 compatibility fixes
This is just a fix to make the build work , but there are lot of missing features to make it 1:1 with the native
1 parent 43d067d commit 2d1e276

File tree

11 files changed

+62
-43
lines changed

11 files changed

+62
-43
lines changed

.mise.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[tools]
2+
java = "temurin-21"
3+
ruby = "jruby-10.0.0.1"

lib/arjdbc/abstract/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def translate_exception(exception, message:, sql:, binds:)
5858
end
5959

6060
# this version of log() automatically fills type_casted_binds from binds if necessary
61-
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, async: false)
61+
def log(sql, name = "SQL", binds = [], type_casted_binds = [], async: false, &block)
6262
if binds.any? && (type_casted_binds.nil? || type_casted_binds.empty?)
6363
type_casted_binds = lambda {
6464
# extract_raw_bind_values

lib/arjdbc/abstract/database_statements.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ module DatabaseStatements
1010
NO_BINDS = [].freeze
1111

1212
def exec_insert(sql, name = nil, binds = NO_BINDS, pk = nil, sequence_name = nil, returning: nil)
13-
sql = transform_query(sql)
14-
1513
if preventing_writes?
1614
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
1715
end
@@ -34,8 +32,6 @@ def exec_insert(sql, name = nil, binds = NO_BINDS, pk = nil, sequence_name = nil
3432
# It appears that at this point (AR 5.0) "prepare" should only ever be true
3533
# if prepared statements are enabled
3634
def internal_exec_query(sql, name = nil, binds = NO_BINDS, prepare: false, async: false, allow_retry: false, materialize_transactions: true)
37-
sql = transform_query(sql)
38-
3935
if preventing_writes? && write_query?(sql)
4036
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
4137
end
@@ -58,8 +54,6 @@ def internal_exec_query(sql, name = nil, binds = NO_BINDS, prepare: false, async
5854
end
5955

6056
def exec_update(sql, name = 'SQL', binds = NO_BINDS)
61-
sql = transform_query(sql)
62-
6357
if preventing_writes?
6458
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
6559
end
@@ -86,13 +80,23 @@ def select_all(arel, name = nil, binds = NO_BINDS, preparable: nil, async: false
8680

8781
private
8882

83+
def without_prepared_statement?(binds)
84+
!prepared_statements || binds.empty?
85+
end
86+
8987
def convert_legacy_binds_to_attributes(binds)
9088
binds.map do |column, value|
9189
ActiveRecord::Relation::QueryAttribute.new(nil, type_cast(value, column), ActiveModel::Type::Value.new)
9290
end
9391
end
9492

95-
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
93+
def preprocess_query(sql)
94+
check_if_write_query(sql) if respond_to?(:check_if_write_query, true)
95+
mark_transaction_written_if_write(sql) if respond_to?(:mark_transaction_written_if_write, true)
96+
sql
97+
end
98+
99+
def raw_execute(sql, name, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true)
96100
log(sql, name, async: async) do
97101
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
98102
result = conn.execute(sql)

lib/arjdbc/postgresql/adapter.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ def supports_savepoints?
219219
true
220220
end
221221

222+
def supports_native_partitioning?
223+
database_version >= 100_000
224+
end
225+
222226
def supports_insert_returning?
223227
true
224228
end
@@ -953,7 +957,6 @@ def self.native_database_types # :nodoc:
953957
FEATURE_NOT_SUPPORTED = "0A000" # :nodoc:
954958

955959
def execute_and_clear(sql, name, binds, prepare: false, async: false)
956-
sql = transform_query(sql)
957960
check_if_write_query(sql)
958961

959962
if !prepare || without_prepared_statement?(binds)

test/db/h2/rake_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ def do_teardown
1414
@db_name = 'rake-create-test'
1515
Rake::Task["db:create"].invoke
1616
db_path = ActiveRecord::Base.connection.database_path
17-
#assert_true File.exists?(db_path(@db_name)), "db file: #{db_path(@db_name)} is missing"
18-
assert_true File.exists?(db_path), "db file: #{db_path} is missing"
17+
#assert_true File.exist?(db_path(@db_name)), "db file: #{db_path(@db_name)} is missing"
18+
assert_true File.exist?(db_path), "db file: #{db_path} is missing"
1919

2020
Rake::Task["db:drop"].invoke
21-
assert_false File.exists?(db_path), "db file: #{db_path} not deleted"
21+
assert_false File.exist?(db_path), "db file: #{db_path} not deleted"
2222
end
2323

2424
test 'rake db:create (and db:drop) in memory db' do
2525
Rake::Task["db:create"].invoke
26-
# assert_true File.exists?("#{db_name}.lck")
26+
# assert_true File.exist?("#{db_name}.lck")
2727

2828
Rake::Task["db:drop"].invoke
29-
# assert_false File.exists?("#{db_name}.lck")
29+
# assert_false File.exist?("#{db_name}.lck")
3030
end
3131

3232
test 'rake db:test:purge' do
@@ -54,7 +54,7 @@ def do_teardown
5454
Dir.mkdir 'db' # db/structure.sql
5555
Rake::Task["db:structure:dump"].invoke
5656

57-
assert File.exists?(structure_sql)
57+
assert File.exist?(structure_sql)
5858
# CREATE CACHED TABLE PUBLIC.LOOSERS
5959
assert_match(/CREATE .*? TABLE PUBLIC.LOOSERS/i, File.read(structure_sql))
6060

@@ -66,7 +66,7 @@ def do_teardown
6666
assert ActiveRecord::Base.connection.table_exists?('loosers')
6767
ActiveRecord::Base.connection.disconnect!
6868
ensure
69-
File.delete(structure_sql) if File.exists?(structure_sql)
69+
File.delete(structure_sql) if File.exist?(structure_sql)
7070
Dir.rmdir 'db'
7171
end
7272
end

test/db/hsqldb/rake_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ def do_teardown
1313
test 'rake db:create (and db:drop)' do
1414
@db_name = 'rake-create-test.hsqldb'
1515
Rake::Task["db:create"].invoke
16-
assert_true File.exists?("#{@db_name}.lck")
16+
assert_true File.exist?("#{@db_name}.lck")
1717

1818
Rake::Task["db:drop"].invoke
19-
assert_false File.exists?("#{@db_name}.lck")
19+
assert_false File.exist?("#{@db_name}.lck")
2020
end
2121

2222
test 'rake db:create (and db:drop) in memory db' do
2323
Rake::Task["db:create"].invoke
24-
# assert_true File.exists?("#{db_name}.lck")
24+
# assert_true File.exist?("#{db_name}.lck")
2525

2626
Rake::Task["db:drop"].invoke
27-
# assert_false File.exists?("#{db_name}.lck")
27+
# assert_false File.exist?("#{db_name}.lck")
2828
end
2929

3030
test 'rake db:test:purge' do
@@ -52,7 +52,7 @@ def do_teardown
5252
Dir.mkdir 'db' # db/structure.sql
5353
Rake::Task["db:structure:dump"].invoke
5454

55-
assert File.exists?(structure_sql)
55+
assert File.exist?(structure_sql)
5656
# CREATE MEMORY TABLE PUBLIC.LOOSERS
5757
assert_match(/CREATE .*? TABLE PUBLIC.LOOSERS/i, File.read(structure_sql))
5858

@@ -64,7 +64,7 @@ def do_teardown
6464
assert ActiveRecord::Base.connection.table_exists?('loosers')
6565
ActiveRecord::Base.connection.disconnect!
6666
ensure
67-
File.delete(structure_sql) if File.exists?(structure_sql)
67+
File.delete(structure_sql) if File.exist?(structure_sql)
6868
Dir.rmdir 'db'
6969
end
7070
end
@@ -90,7 +90,7 @@ def drop_rake_test_database(silence = nil)
9090

9191
Dir.glob("#{@db_name}*").each do |f|
9292
if silence
93-
FileUtils.rm_rf(f) if File.exists?(f)
93+
FileUtils.rm_rf(f) if File.exist?(f)
9494
else
9595
FileUtils.rm_rf(f)
9696
end

test/db/mssql/rake_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def do_teardown
6262
Dir.mkdir 'db' # db/structure.sql
6363
Rake::Task["db:structure:dump"].invoke
6464

65-
assert File.exists?(structure_sql)
65+
assert File.exist?(structure_sql)
6666
# CREATE TABLE [dbo].[users]( ... )
6767
assert_match /CREATE TABLE .*?\[users\]/i, File.read(structure_sql)
6868

@@ -75,7 +75,7 @@ def do_teardown
7575
assert ActiveRecord::Base.connection.table_exists?('users')
7676
ActiveRecord::Base.connection.disconnect!
7777
ensure
78-
File.delete(structure_sql) if File.exists?(structure_sql)
78+
File.delete(structure_sql) if File.exist?(structure_sql)
7979
Dir.rmdir 'db'
8080
end
8181
end

test/db/mysql/rake_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def do_teardown
6767
assert connection.table_exists?('testers')
6868
end
6969
ensure
70-
File.delete(structure_sql) if File.exists?(structure_sql)
70+
File.delete(structure_sql) if File.exist?(structure_sql)
7171
Dir.rmdir 'db'
7272
end
7373
end
@@ -85,7 +85,7 @@ def do_teardown
8585
Dir.mkdir 'db' # db/structure.sql
8686
Rake::Task["db:structure:dump"].invoke
8787

88-
assert File.exists?(structure_sql)
88+
assert File.exist?(structure_sql)
8989
assert_match(/CREATE TABLE `users`/, File.read(structure_sql))
9090

9191
# db:structure:load
@@ -97,7 +97,7 @@ def do_teardown
9797
assert ActiveRecord::Base.connection.table_exists?('users')
9898
end
9999
ensure
100-
File.delete(structure_sql) if File.exists?(structure_sql)
100+
File.delete(structure_sql) if File.exist?(structure_sql)
101101
Dir.rmdir 'db'
102102
end
103103
end

test/db/postgresql/rake_test.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def do_teardown
6565
Dir.mkdir 'db' # db/structure.sql
6666
Rake::Task["db:schema:dump"].invoke
6767

68-
assert File.exists?(structure_sql)
68+
assert File.exist?(structure_sql)
6969
assert_match(/CREATE TABLE .*?.?users/, File.read(structure_sql))
7070

7171
# db:structure:load
@@ -83,7 +83,7 @@ def do_teardown
8383
assert connection.table_exists?('users')
8484
end
8585
ensure
86-
File.delete(structure_sql) if File.exists?(structure_sql)
86+
File.delete(structure_sql) if File.exist?(structure_sql)
8787
Dir.rmdir 'db'
8888
ActiveRecord.schema_format = initial_format
8989
end
@@ -107,16 +107,24 @@ def do_teardown
107107

108108
def psql(args)
109109
args = args.join(' ') unless args.is_a?(String)
110-
if db_config[:host] != 'localhost'
111-
args = "--host=#{db_config[:host]} #{args}"
112-
end
113-
if username = ENV['PSQL_USERNAME']
114-
args = "--username=#{username} #{args}"
110+
111+
# Always include host and port for Docker PostgreSQL
112+
args = "--host=#{db_config[:host] || 'localhost'} #{args}"
113+
if db_config[:port]
114+
args = "--port=#{db_config[:port]} #{args}"
115115
end
116+
117+
# Use the test database user or fallback to ENV
118+
username = db_config[:username] || ENV['PSQL_USERNAME'] || 'postgres'
119+
args = "--username=#{username} #{args}"
116120

117121
puts "psql args: #{args}"
118122

119-
`#{PSQL_EXE} #{args}`
123+
# Set PGPASSWORD for authentication
124+
env = {}
125+
env['PGPASSWORD'] = db_config[:password] || 'postgres'
126+
127+
IO.popen(env, "#{PSQL_EXE} #{args}") { |io| io.read }
120128
end
121129

122130
end

test/db/postgresql/schema_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
class PostgresSchemaTest < Test::Unit::TestCase
55

66
def test_collation
7-
assert_equal 'en_US.UTF-8', connection.collation
7+
# Docker PostgreSQL often uses C.UTF-8 instead of en_US.UTF-8
8+
assert_match /\A(en_US|C)\.UTF-8\z/, connection.collation
89
end
910

1011
def test_encoding

0 commit comments

Comments
 (0)