Skip to content

Commit

Permalink
feat: upgrade to newest API client version (#449)
Browse files Browse the repository at this point in the history
* chore: update algolia version

* WIP: fix tests until FowardToReplicas

* chore: fix SubReplicas tests

* chore: fix Book tests

* chore: fix pagination tests

* chore: fix disabled indexing tests

* fix tests

* ensure latest gem version

* fix: virtual replica test

* test matrix

* Update Gemfile

* Update config.yml

* Update Gemfile

* Update config.yml

* Update config.yml

* Update algoliasearch-rails.rb

* sanitizer

* Update algoliasearch-rails.rb

* Update algoliasearch-rails.rb

* Update algoliasearch-rails.rb

* Update pagy.rb

* Update integration_spec.rb

* Update pagy.rb

* Update integration_spec.rb

* Update pagy.rb

* Update pagy.rb

* Update integration_spec.rb

* Update integration_spec.rb

* sanitizer

* chore: clean up algoliasearch/configuration

* remove `index` method

* chore: handle todos

* Update algoliasearch-rails.gemspec

* Update spec_helper.rb

* address feedback

* feat: allow setting user agent

* pin version

* Update algoliasearch-rails.gemspec

* Update config.yml

* prep release

* feat: issue sync
  • Loading branch information
DevinCodes authored Oct 21, 2024
1 parent 9c1a0a4 commit 5788eda
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 342 deletions.
60 changes: 18 additions & 42 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ aliases:
curl -s https://algoliasearch-client-keygen.herokuapp.com | sh >> $BASH_ENV
fi
- &install_sqlite
name: Install SQLite
command: sudo apt-get update && sudo apt-get install -y sqlite3 libsqlite3-dev

- &check_bundler
name: Which bundler?
command: bundle -v
Expand Down Expand Up @@ -33,7 +37,7 @@ aliases:
references:
default_docker_ruby_executor: &default_docker_ruby_executor
image: circleci/ruby:<< parameters.version >>
image: cimg/ruby:<< parameters.version >>
environment:
BUNDLE_JOBS: 3
BUNDLE_RETRY: 3
Expand All @@ -57,6 +61,7 @@ jobs:
- *default_docker_ruby_executor
steps:
- checkout
- run: *install_sqlite
- run: *check_bundler
- restore_cache: *restore_cache
- run: *install_bundler
Expand All @@ -69,45 +74,16 @@ workflows:
ci:
jobs:
- test:
name: 'Rails 5.1 - Ruby 2.4'
version: '2.4'
rails-version: '5.1'
sequel-version: '5.0'
- test:
name: 'Rails 5.1 - Ruby 2.5'
version: '2.5'
rails-version: '5.1'
sequel-version: '5.0'
- test:
name: 'Rails 5.1 - Ruby 2.6'
version: '2.6'
rails-version: '5.1'
sequel-version: '5.0'


- test:
name: 'Rails 6.0 - Ruby 2.5'
version: '2.5'
rails-version: '6.0'
sequel-version: '5.0'
- test:
name: 'Rails 6.0 - Ruby 2.6'
version: '2.6'
rails-version: '6.0'
sequel-version: '5.0'
- test:
name: 'Rails 6.0 - Ruby 2.7'
version: '2.7'
rails-version: '6.0'
sequel-version: '5.0'

- test:
name: 'Rails 6.1 - Ruby 2.7'
version: '2.7'
rails-version: '6.1'
sequel-version: '5.0'
name: "Ruby << matrix.version >> - Rails << matrix.rails-version >>"
matrix:
parameters:
version: [ '2.5', '2.6', '2.7', '3.0', '3.1' ]
rails-version: [ '6.0', '6.1' ]
sequel-version: [ '5.0' ]
- test:
name: 'Rails 6.1 - Ruby 3.0'
version: '3.0'
rails-version: '6.1'
sequel-version: '5.0'
name: "Ruby << matrix.version >> - Rails << matrix.rails-version >>"
matrix:
parameters:
version: ['3.0', '3.1']
rails-version: ['7.0']
sequel-version: ['5.0']
55 changes: 55 additions & 0 deletions .github/workflows/issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: 'Issue sync with Jira'
on:
issues:
types: [opened]

permissions:
issues: write
contents: read

jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Create ticket
uses: actions/github-script@v7
with:
script: |
const action = context.payload.action;
if (action !== 'opened') {
return;
}
const title = context.payload.issue.title;
const body = context.payload.issue.body;
const res = await fetch('https://algolia.atlassian.net/rest/api/2/issue', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Basic ${{ secrets.JIRA_TOKEN }}`
},
body: JSON.stringify({
fields: {
description: `Issue created by ${context.actor} at ${context.payload.issue.html_url} \n\n${body}`,
issuetype: {
id: '10001'
},
parent: {
key: 'DI-1911'
},
project: {
id: '10118'
},
summary: `[GH-ISSUE] ${title}`
},
update: {}
})
});
if (!res.ok) {
throw new Error(`Failed to create ticket: ${res.statusText} (${res.status}) - ${await res.text()}`);
}
const data = await res.json();
console.log(`Created ticket: ${data.key}`);
6 changes: 5 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG

## [Unreleased](https://github.com/algolia/algoliasearch-rails/compare/2.2.1...master)
## [Unreleased](https://github.com/algolia/algoliasearch-rails/compare/3.0.0...master)

## [3.0.0](https://github.com/algolia/algoliasearch-rails/compare/2.3.2...3.0.0)
This new major version leverages the latest version of the Ruby Algolia API client. It also drops (official) support for Rails 5.x and Ruby versions older than 2.5
For a list of known breaking changes, please refer to [the upgrade guide](./UPGRADING_TO_V3.MD)

## [2.3.2](https://github.com/algolia/algoliasearch-rails/compare/2.3.1...2.3.2)
### Added
Expand Down
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "http://rubygems.org"

gem 'json', '>= 1.5.1'
gem 'algolia', '< 3.0.0'
gem 'algolia', '>= 3.5.2'

if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
gem 'rubysl', '~> 2.0', :platform => :rbx
Expand All @@ -16,7 +16,7 @@ group :test do
else
gem 'sqlite3', '< 1.4.0', :platform => [:rbx, :ruby]
end
gem 'rspec', '>= 2.5.0', '< 3.0'
gem 'rspec', '~> 3.0'
gem 'jdbc-sqlite3', :platform => :jruby
gem 'activerecord-jdbc-adapter', :platform => :jruby
gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
Expand All @@ -27,7 +27,7 @@ group :test do
end

group :development do
gem 'rake', '~> 10.1.0'
gem 'rake', '>= 10.1.0'
gem 'rdoc'
end

Expand Down
66 changes: 66 additions & 0 deletions UPGRADING_TO_V3.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Version 3 of the `algoliasearch-rails` gem replaces the Algolia API client version in use from version 2 to version 3.
These versions of the API client differ significantly, so you likely need to make code changes when updating.
We've tried keeping most of the changes internal, but there are still some breaking changes you need to be aware of when upgrading.

If you encounter any breaking changes to the Rail integration that are not listed here, please open a Pull Request to add them to this list.

## Breaking changes

`algolia_ensure_init` (this method is protected and shouldn't be called manually, but we list it here anyways): the method no longer returns an initialized `index` object as this is not part of the new API client. The method now returns nothing, but it still ensures the index exists and applies settings if needed.

---
`Model.search`, `Model.raw_search`: response keys in the new API client are no longer strings, but are *always* symbols. For example:
```ruby
# Before
results = Product.raw_search('shirt')
p results['hits']

# After
results = Product.raw_search('shirt')
p results[:hits]
```
---
`Model.search_for_facet_values`: this no longer returns an array of hashes, but an array of objects of type `Algolia::Search::FacetHits`:
```ruby
# Before
facets = Color.search_for_facet_values('short_name', 'bl', :query => 'black')
puts facets.first['value']

# After
facets = Color.search_for_facet_values('short_name', 'bl', :query => 'black')
facets.first.value
```

---
`Model.index_name` takes an additional, optional parameter. You can use this if you want to get the name of one of your replica indices, which ensures the index naming takes configuration that modifies the index name into account.
For example, if you have the `:per_environment` option set to true, it will automatically add the environment name in the index name.
```ruby
def Product
include AlgoliaSearch

algoliasearch({ per_environment: true }) do
add_replica 'Suits', per_environment: true do
# replica settings
end
end

end
main_index_name = Product.index_name
replica_index_name = Product.index_name('Suits')
```

---
`AlgoliaSearch::Configuration.client_opts`, `AlgoliaSearch::Configuration::REQUIRED_CONFIGURATION` and `AlgoliaSearch::SafeIndex` have been removed.
If you need to configure the API client other than the ways that are provided now, it's recommended to set up an instance manually.

---
`Model.index` and `Model.algolia_index` have been removed, as there is no notion of an `Index` object in the new version of the API clients.
Instead, you can use `Model.index_name` to get the name of the index to target, and use this on an instance of the API client directly.

```ruby
# Before
res = Product.index.search('shoe')

# After
res = AlgoliaSearch.client.search_single_index(Product.index_name, { query: 'shoe' })
```
6 changes: 3 additions & 3 deletions algoliasearch-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<json>, [">= 1.5.1"])
s.add_runtime_dependency(%q<algolia>, ["< 3.0.0"])
s.add_runtime_dependency(%q<algolia>, [">= 3.5.2"])
s.add_development_dependency(%q<will_paginate>, [">= 2.3.15"])
s.add_development_dependency(%q<kaminari>, [">= 0"])
s.add_development_dependency(%q<pagy>, [">= 0"])
s.add_development_dependency "rake"
s.add_development_dependency "rdoc"
else
s.add_dependency(%q<json>, [">= 1.5.1"])
s.add_dependency(%q<algolia>, ["< 3.0.0"])
s.add_dependency(%q<algolia>, [">= 3.5.2"])
end
else
s.add_dependency(%q<json>, [">= 1.5.1"])
s.add_dependency(%q<algolia>, ["< 3.0.0"])
s.add_dependency(%q<algolia>, [">= 3.5.2"])
end
end

Loading

0 comments on commit 5788eda

Please sign in to comment.