Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
java = "temurin-21"
ruby = "jruby-10.0.0.1"
2 changes: 1 addition & 1 deletion lib/arjdbc/abstract/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def translate_exception(exception, message:, sql:, binds:)
end

# this version of log() automatically fills type_casted_binds from binds if necessary
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, async: false)
def log(sql, name = "SQL", binds = [], type_casted_binds = [], async: false, &block)
if binds.any? && (type_casted_binds.nil? || type_casted_binds.empty?)
type_casted_binds = lambda {
# extract_raw_bind_values
Expand Down
18 changes: 11 additions & 7 deletions lib/arjdbc/abstract/database_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module DatabaseStatements
NO_BINDS = [].freeze

def exec_insert(sql, name = nil, binds = NO_BINDS, pk = nil, sequence_name = nil, returning: nil)
sql = transform_query(sql)

if preventing_writes?
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
end
Expand All @@ -34,8 +32,6 @@ def exec_insert(sql, name = nil, binds = NO_BINDS, pk = nil, sequence_name = nil
# It appears that at this point (AR 5.0) "prepare" should only ever be true
# if prepared statements are enabled
def internal_exec_query(sql, name = nil, binds = NO_BINDS, prepare: false, async: false, allow_retry: false, materialize_transactions: true)
sql = transform_query(sql)

if preventing_writes? && write_query?(sql)
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
end
Expand All @@ -58,8 +54,6 @@ def internal_exec_query(sql, name = nil, binds = NO_BINDS, prepare: false, async
end

def exec_update(sql, name = 'SQL', binds = NO_BINDS)
sql = transform_query(sql)

if preventing_writes?
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
end
Expand All @@ -86,13 +80,23 @@ def select_all(arel, name = nil, binds = NO_BINDS, preparable: nil, async: false

private

def without_prepared_statement?(binds)
!prepared_statements || binds.empty?
end

def convert_legacy_binds_to_attributes(binds)
binds.map do |column, value|
ActiveRecord::Relation::QueryAttribute.new(nil, type_cast(value, column), ActiveModel::Type::Value.new)
end
end

def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
def preprocess_query(sql)
check_if_write_query(sql) if respond_to?(:check_if_write_query, true)
mark_transaction_written_if_write(sql) if respond_to?(:mark_transaction_written_if_write, true)
sql
end

def raw_execute(sql, name, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true)
log(sql, name, async: async) do
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
result = conn.execute(sql)
Expand Down
5 changes: 4 additions & 1 deletion lib/arjdbc/postgresql/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ def supports_savepoints?
true
end

def supports_native_partitioning?
database_version >= 100_000
end

def supports_insert_returning?
true
end
Expand Down Expand Up @@ -953,7 +957,6 @@ def self.native_database_types # :nodoc:
FEATURE_NOT_SUPPORTED = "0A000" # :nodoc:

def execute_and_clear(sql, name, binds, prepare: false, async: false)
sql = transform_query(sql)
check_if_write_query(sql)

if !prepare || without_prepared_statement?(binds)
Expand Down
14 changes: 7 additions & 7 deletions test/db/h2/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ def do_teardown
@db_name = 'rake-create-test'
Rake::Task["db:create"].invoke
db_path = ActiveRecord::Base.connection.database_path
#assert_true File.exists?(db_path(@db_name)), "db file: #{db_path(@db_name)} is missing"
assert_true File.exists?(db_path), "db file: #{db_path} is missing"
#assert_true File.exist?(db_path(@db_name)), "db file: #{db_path(@db_name)} is missing"
assert_true File.exist?(db_path), "db file: #{db_path} is missing"

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

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

Rake::Task["db:drop"].invoke
# assert_false File.exists?("#{db_name}.lck")
# assert_false File.exist?("#{db_name}.lck")
end

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

assert File.exists?(structure_sql)
assert File.exist?(structure_sql)
# CREATE CACHED TABLE PUBLIC.LOOSERS
assert_match(/CREATE .*? TABLE PUBLIC.LOOSERS/i, File.read(structure_sql))

Expand All @@ -66,7 +66,7 @@ def do_teardown
assert ActiveRecord::Base.connection.table_exists?('loosers')
ActiveRecord::Base.connection.disconnect!
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
end
end
Expand Down
14 changes: 7 additions & 7 deletions test/db/hsqldb/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ def do_teardown
test 'rake db:create (and db:drop)' do
@db_name = 'rake-create-test.hsqldb'
Rake::Task["db:create"].invoke
assert_true File.exists?("#{@db_name}.lck")
assert_true File.exist?("#{@db_name}.lck")

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

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

Rake::Task["db:drop"].invoke
# assert_false File.exists?("#{db_name}.lck")
# assert_false File.exist?("#{db_name}.lck")
end

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

assert File.exists?(structure_sql)
assert File.exist?(structure_sql)
# CREATE MEMORY TABLE PUBLIC.LOOSERS
assert_match(/CREATE .*? TABLE PUBLIC.LOOSERS/i, File.read(structure_sql))

Expand All @@ -64,7 +64,7 @@ def do_teardown
assert ActiveRecord::Base.connection.table_exists?('loosers')
ActiveRecord::Base.connection.disconnect!
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
end
end
Expand All @@ -90,7 +90,7 @@ def drop_rake_test_database(silence = nil)

Dir.glob("#{@db_name}*").each do |f|
if silence
FileUtils.rm_rf(f) if File.exists?(f)
FileUtils.rm_rf(f) if File.exist?(f)
else
FileUtils.rm_rf(f)
end
Expand Down
4 changes: 2 additions & 2 deletions test/db/mssql/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def do_teardown
Dir.mkdir 'db' # db/structure.sql
Rake::Task["db:structure:dump"].invoke

assert File.exists?(structure_sql)
assert File.exist?(structure_sql)
# CREATE TABLE [dbo].[users]( ... )
assert_match /CREATE TABLE .*?\[users\]/i, File.read(structure_sql)

Expand All @@ -75,7 +75,7 @@ def do_teardown
assert ActiveRecord::Base.connection.table_exists?('users')
ActiveRecord::Base.connection.disconnect!
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/db/mysql/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def do_teardown
assert connection.table_exists?('testers')
end
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
end
end
Expand All @@ -85,7 +85,7 @@ def do_teardown
Dir.mkdir 'db' # db/structure.sql
Rake::Task["db:structure:dump"].invoke

assert File.exists?(structure_sql)
assert File.exist?(structure_sql)
assert_match(/CREATE TABLE `users`/, File.read(structure_sql))

# db:structure:load
Expand All @@ -97,7 +97,7 @@ def do_teardown
assert ActiveRecord::Base.connection.table_exists?('users')
end
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
end
end
Expand Down
24 changes: 16 additions & 8 deletions test/db/postgresql/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def do_teardown
Dir.mkdir 'db' # db/structure.sql
Rake::Task["db:schema:dump"].invoke

assert File.exists?(structure_sql)
assert File.exist?(structure_sql)
assert_match(/CREATE TABLE .*?.?users/, File.read(structure_sql))

# db:structure:load
Expand All @@ -83,7 +83,7 @@ def do_teardown
assert connection.table_exists?('users')
end
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
ActiveRecord.schema_format = initial_format
end
Expand All @@ -107,16 +107,24 @@ def do_teardown

def psql(args)
args = args.join(' ') unless args.is_a?(String)
if db_config[:host] != 'localhost'
args = "--host=#{db_config[:host]} #{args}"
end
if username = ENV['PSQL_USERNAME']
args = "--username=#{username} #{args}"

# Always include host and port for Docker PostgreSQL
args = "--host=#{db_config[:host] || 'localhost'} #{args}"
if db_config[:port]
args = "--port=#{db_config[:port]} #{args}"
end

# Use the test database user or fallback to ENV
username = db_config[:username] || ENV['PSQL_USERNAME'] || 'postgres'
args = "--username=#{username} #{args}"

puts "psql args: #{args}"

`#{PSQL_EXE} #{args}`
# Set PGPASSWORD for authentication
env = {}
env['PGPASSWORD'] = db_config[:password] || 'postgres'

IO.popen(env, "#{PSQL_EXE} #{args}") { |io| io.read }
end

end
3 changes: 2 additions & 1 deletion test/db/postgresql/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
class PostgresSchemaTest < Test::Unit::TestCase

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

def test_encoding
Expand Down
12 changes: 6 additions & 6 deletions test/db/sqlite3/rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class SQLite3RakeTest < Test::Unit::TestCase
def db_name; 'rake_test.sqlite3'; end

def do_setup
File.delete('rake_test.sqlite3') if File.exists?('rake_test.sqlite3')
File.delete('rake_test.sqlite3') if File.exist?('rake_test.sqlite3')
end

def do_teardown
File.delete('rake_test.sqlite3') if File.exists?('rake_test.sqlite3')
File.delete('rake_test.sqlite3') if File.exist?('rake_test.sqlite3')
end

test 'rake db:create (and db:drop)' do
Rake::Task["db:create"].invoke
assert_true File.exists?('rake_test.sqlite3')
assert_true File.exist?('rake_test.sqlite3')

Rake::Task["db:drop"].invoke
assert_false File.exists?('rake_test.sqlite3')
assert_false File.exist?('rake_test.sqlite3')
end

test 'rake db:test:purge' do
Expand Down Expand Up @@ -57,7 +57,7 @@ def do_teardown
Dir.mkdir 'db' # db/structure.sql
Rake::Task["db:structure:dump"].invoke

assert File.exists?(structure_sql)
assert File.exist?(structure_sql)
assert_match(/CREATE TABLE .*?users/, File.read(structure_sql))

# db:structure:load
Expand All @@ -68,7 +68,7 @@ def do_teardown
assert ActiveRecord::Base.connection.data_source_exists?('users')
ActiveRecord::Base.connection.disconnect!
ensure
File.delete(structure_sql) if File.exists?(structure_sql)
File.delete(structure_sql) if File.exist?(structure_sql)
Dir.rmdir 'db'
end
end
Expand Down
Loading