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 67dfa6e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 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/1501

## 4.1.0 - 2023-10-23

### 🚀 Features
Expand Down
7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ Here's a quick guide:
bundle exec rake spec
```

The test suite runs by default with SQLite3. To run the test suite with PostgreSQL or MySQL, use:
The test suite runs by default with SQLite3. To run the test suite with PostgreSQL, MySQL (Mysql2 adapter), or MySQL (Trilogy adapter), use:

```sh
DB=pg bundle exec rake spec
DB=mysql bundle exec rake spec
DB=trilogy bundle exec rake spec
```

A one-liner to run all three

```sh
bundle exec rake spec && DB=pg bundle exec rake spec && DB=mysql bundle exec rake spec
bundle exec rake spec && DB=pg bundle exec rake spec && DB=mysql bundle exec rake spec && DB=trilogy bundle exec rake spec
```

For Postgres and MySQL, databases are expected to exist, called 'ransack'. To create use these commands (assuming OS X and Homebrew):
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 67dfa6e

Please sign in to comment.