Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wildcard escaping with PostGIS adapter (fixes #850) #1541

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions .github/workflows/cronjob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ jobs:
- name: Run tests
run: bundle exec rspec

postgis:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
ruby:
- 3.2.2
env:
DB: postgis
RAILS: main
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
DATABASE_HOST: 127.0.0.1
services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: trust
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Setup databases
run: |
psql -h localhost -p 5432 -W postgres -c 'create database ransack;' -U postgres;
- name: Install dependencies
run: bundle install
- name: Run tests
run: bundle exec rspec

postgres:
runs-on: ubuntu-22.04
strategy:
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ jobs:
- name: Run tests
run: bundle exec rspec

postgis:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
rails:
- 7-2-stable
- v7.1.0
- v7.0.3
- v6.1.6
ruby:
- 3.2.2
- 3.1.4
env:
DB: postgis
RAILS: ${{ matrix.rails }}
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: postgres
DATABASE_HOST: 127.0.0.1
services:
postgres:
image: postgres
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: trust
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Setup databases
run: |
psql -h localhost -p 5432 -W postgres -c 'create database ransack;' -U postgres;
- name: Run tests
run: bundle exec rspec

postgres:
runs-on: ubuntu-22.04
strategy:
Expand Down
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

* Fix wildcard escaping with PostGIS adapter.

## 4.2.1 - 2024-8-11

* Fix Rails 7.1.x compatibility
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rails_version = case rails
gem 'faker'
gem 'sqlite3', '~> 1.4'
gem 'pg'
gem 'activerecord-postgis-adapter'
gem 'pry'
gem 'byebug'

Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def escape_wildcards(unescaped)
when "Mysql2".freeze
# Necessary for MySQL
unescaped.to_s.gsub(/([\\%_])/, '\\\\\\1')
when "PostgreSQL".freeze
when "PostGIS".freeze, "PostgreSQL".freeze
# Necessary for PostgreSQL
unescaped.to_s.gsub(/([\\%_.])/, '\\\\\\1')
else
Expand Down
24 changes: 16 additions & 8 deletions spec/ransack/predicate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ module Ransack

describe 'cont' do
it_has_behavior 'wildcard escaping', :name_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
(case ActiveRecord::Base.connection.adapter_name
when "PostGIS", "PostgreSQL"
/"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
when "Mysql2"
/`people`.`name` LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/"people"."name" LIKE '%%._\\%'/
Expand All @@ -177,9 +178,10 @@ module Ransack

describe 'not_cont' do
it_has_behavior 'wildcard escaping', :name_not_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
(case ActiveRecord::Base.connection.adapter_name
when "PostGIS", "PostgreSQL"
/"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
when "Mysql2"
/`people`.`name` NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/"people"."name" NOT LIKE '%%._\\%'/
Expand All @@ -196,9 +198,12 @@ module Ransack

describe 'i_cont' do
it_has_behavior 'wildcard escaping', :name_i_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
(case ActiveRecord::Base.connection.adapter_name
when "PostGIS"
/LOWER\("people"."name"\) ILIKE '%\\%\\.\\_\\\\%'/
when "PostgreSQL"
/"people"."name" ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
when "Mysql2"
/LOWER\(`people`.`name`\) LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/LOWER\("people"."name"\) LIKE '%%._\\%'/
Expand All @@ -215,9 +220,12 @@ module Ransack

describe 'not_i_cont' do
it_has_behavior 'wildcard escaping', :name_not_i_cont,
(if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
(case ActiveRecord::Base.connection.adapter_name
when "PostGIS"
/LOWER\("people"."name"\) NOT ILIKE '%\\%\\.\\_\\\\%'/
when "PostgreSQL"
/"people"."name" NOT ILIKE '%\\%\\.\\_\\\\%'/
elsif ActiveRecord::Base.connection.adapter_name == "Mysql2"
when "Mysql2"
/LOWER\(`people`.`name`\) NOT LIKE '%\\\\%.\\\\_\\\\\\\\%'/
else
/LOWER\("people"."name"\) NOT LIKE '%%._\\%'/
Expand Down
12 changes: 12 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_record'
require 'activerecord-postgis-adapter'

case ENV['DB'].try(:downcase)
when 'mysql', 'mysql2'
Expand All @@ -20,6 +21,17 @@
host: ENV.fetch("DATABASE_HOST") { "localhost" },
min_messages: 'warning'
)
when 'postgis'
# To test with PostGIS: `DB=postgis bundle exec rake spec`
ActiveRecord::Base.establish_connection(
adapter: 'postgis',
postgis_extension: 'postgis',
database: 'ransack',
username: ENV.fetch("DATABASE_USERNAME") { "postgres" },
password: ENV.fetch("DATABASE_PASSWORD") { "" },
host: ENV.fetch("DATABASE_HOST") { "localhost" },
min_messages: 'warning'
)
else
# Otherwise, assume SQLite3: `bundle exec rake spec`
ActiveRecord::Base.establish_connection(
Expand Down