Skip to content

Commit

Permalink
Add support for Trilogy database adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
navels committed Jun 29, 2024
1 parent 489bf8f commit 45a1cb9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Add support for Trilogy database adapter by @navels in https://github.com/activerecord-hackery/ransack/pull/1500

## 4.1.0 - 2023-10-23

### 🚀 Features
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ else
end
end
gem 'mysql2'
gem 'trilogy'

group :test do
gem 'machinist', '~> 1.0.6'
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module Constants
# replace % \ to \% \\
def escape_wildcards(unescaped)
case ActiveRecord::Base.connection.adapter_name
when "Mysql2".freeze
when "Mysql2".freeze, "Trilogy".freeze
# Necessary for MySQL
unescaped.to_s.gsub(/([\\%_])/, '\\\\\\1')
when "PostgreSQL".freeze
Expand Down
2 changes: 1 addition & 1 deletion spec/ransack/adapters/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def without_application_record_method(method)
end

def rails7_and_mysql
::ActiveRecord::VERSION::MAJOR >= 7 && ENV['DB'] == 'mysql'
::ActiveRecord::VERSION::MAJOR >= 7 && ['mysql', 'trilogy'].include?(ENV['DB'])
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module Ransack
it_has_behavior 'wildcard escaping', :name_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name)
/`people`.`name` LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/"people"."name" LIKE '%%._\\%'/
Expand All @@ -179,7 +179,7 @@ module Ransack
it_has_behavior 'wildcard escaping', :name_not_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name)
/`people`.`name` NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/"people"."name" NOT LIKE '%%._\\%'/
Expand All @@ -198,7 +198,7 @@ module Ransack
it_has_behavior 'wildcard escaping', :name_i_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name)
/LOWER\(`people`.`name`\) LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/LOWER\("people"."name"\) LIKE '%%._\\%'/
Expand All @@ -217,7 +217,7 @@ module Ransack
it_has_behavior 'wildcard escaping', :name_not_i_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
/"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
elsif ["Mysql2", "Trilogy"].include?(ActiveRecord::Base.connection.adapter_name)
/LOWER\(`people`.`name`\) NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/LOWER\("people"."name"\) NOT LIKE '%%._\\%'/
Expand Down
11 changes: 11 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
database: 'ransack',
username: ENV.fetch("MYSQL_USERNAME") { "root" },
password: ENV.fetch("MYSQL_PASSWORD") { "" },
host: ENV.fetch("MYSQL_HOST") { "localhost" },
encoding: 'utf8'
)
when 'trilogy'
# To test with trilogy: `DB=trilogy bundle exec rake spec`
ActiveRecord::Base.establish_connection(
adapter: 'trilogy',
database: 'ransack',
username: ENV.fetch("MYSQL_USERNAME") { "root" },
password: ENV.fetch("MYSQL_PASSWORD") { "" },
host: ENV.fetch("MYSQL_HOST") { "localhost" },
encoding: 'utf8'
)
when 'pg', 'postgres', 'postgresql'
Expand Down

0 comments on commit 45a1cb9

Please sign in to comment.