Skip to content

Commit

Permalink
build!: Upgrade ruflin/elastica to v8 (#194)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `JoliCode\Elastically\Transport\HttpClientTransport`
has been removed,

Closes #186

---------

Co-authored-by: Julien Mariller <[email protected]>
  • Loading branch information
GaryPEGEOT and Julien Mariller authored Nov 13, 2024
1 parent d504a7f commit cbe1ea8
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 353 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## 2.0.0

### Added

- Updated `ruflin/elastica` to v8 (Breaking Change)
- Dropped `HttpClientTransport` (Breaking Change)

## 1.9.1

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test: ## Run test suite
./vendor/bin/simple-phpunit

start: ## Start testing tools (Elasticsearch)
docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch-oss:7.8.0
docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -e "action.destructive_requires_name=false" -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.14.2

start_opensearch: ## Start testing tools (OpenSearch)
docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" -e "plugins.security.disabled=true" opensearchproject/opensearch:2.3.0
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ elastically:
client:
host: '%env(ELASTICSEARCH_HOST)%'
# If you want to use the Symfony HttpClient (you MUST create this service)
#transport: 'JoliCode\Elastically\Transport\HttpClientTransport'
# Path to the mapping directory (in YAML)
mapping_directory: '%kernel.project_dir%/config/elasticsearch'
Expand Down
14 changes: 14 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Upgrade guide

## From v1.9.0 to v2.0.0

HttpClientTransport has been removed:
```diff
JoliCode\Elastically\Client:
arguments:
$config:
host: '%env(ELASTICSEARCH_HOST)%'
port: '%env(ELASTICSEARCH_PORT)%'
- transport: '@JoliCode\Elastically\Transport\HttpClientTransport'
+ transport_client:
+ client: '@my_custom_psr18_client' # An instance of Symfony\Component\HttpClient\Psr18Client (Or any PSR 18 compliant one)
```

## From v1.3.0 to v1.4.0

If you're using Symfony, here are the changes to apply:
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"nyholm/psr7": "^1.8",
"phpdocumentor/reflection-docblock": "^4.4|^5.0",
"ruflin/elastica": "^7.0",
"ruflin/elastica": "^8.0",
"symfony/deprecation-contracts": "^2.4 || ^3.0",
"symfony/property-access": "^5.4 || ^6.0 || ^7.0",
"symfony/property-info": "^5.4 || ^6.0 || ^7.0",
"symfony/psr-http-message-bridge": "^7.1",
"symfony/serializer": "^5.4 || ^6.0 || ^7.0",
"symfony/yaml": "^5.4 || ^6.0 || ^7.0"
},
Expand Down
1 change: 0 additions & 1 deletion src/Bridge/Symfony/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('All options for the Elastica client constructor')
->example([
'host' => '%env(ELASTICSEARCH_HOST)%',
'transport' => 'JoliCode\Elastically\Transport\HttpClientTransport',
])
->normalizeKeys(false)
->defaultValue([])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private function buildConnection(string $name, array $config, bool $isDefaultCon
$container->setDefinition("elastically.{$name}.result_set_builder", $resultSetBuilder);

$client = new ChildDefinition('elastically.abstract.client');
if (\array_key_exists('client', $config) && \array_key_exists('transport', $config['client'])) {
$config['client']['transport'] = new Reference($config['client']['transport']);
if ($transportClient = $config['client']['transport_config']['http_client'] ?? null) {
$config['client']['transport_config']['http_client'] = new Reference($transportClient);
}
$client->replaceArgument('$config', $config['client'] ?? []);
$client->replaceArgument('$resultSetBuilder', new Reference("elastically.{$name}.result_set_builder"));
Expand Down
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Client extends ElasticaClient
private ResultSetBuilder $resultSetBuilder;
private IndexNameMapper $indexNameMapper;

public function __construct($config = [], ?callable $callback = null, ?LoggerInterface $logger = null, ?ResultSetBuilder $resultSetBuilder = null, ?IndexNameMapper $indexNameMapper = null)
public function __construct($config = [], ?LoggerInterface $logger = null, ?ResultSetBuilder $resultSetBuilder = null, ?IndexNameMapper $indexNameMapper = null)
{
parent::__construct($config, $callback, $logger);
parent::__construct($config, $logger);

// BC Layer, to remove in 2.0
$this->factory = new Factory($config);
Expand Down
1 change: 0 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function buildClient(): Client
return $this->client ??= new Client(
$this->config,
null,
null,
$this->buildBuilder(),
$this->buildIndexNameMapper()
);
Expand Down
12 changes: 5 additions & 7 deletions src/IndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
use Elastica\Exception\ExceptionInterface;
use Elastica\Exception\RuntimeException;
use Elastica\Reindex;
use Elastica\Request;
use Elastica\Response;
use Elastica\Task;
use Elasticsearch\Endpoints\Cluster\State;
use JoliCode\Elastically\Mapping\MappingProviderInterface;

class IndexBuilder
Expand Down Expand Up @@ -64,7 +62,9 @@ public function markAsLive(Index $index, string $indexName): Response
$data['actions'][] = ['remove' => ['index' => $indexPrefixedName . '*', 'alias' => $indexPrefixedName]];
$data['actions'][] = ['add' => ['index' => $index->getName(), 'alias' => $indexPrefixedName]];

return $this->client->request('_aliases', Request::POST, $data);
return $this->client->toElasticaResponse(
$this->client->indices()->updateAliases(['index' => $indexName, 'body' => $data])
);
}

/**
Expand Down Expand Up @@ -119,13 +119,11 @@ public function purgeOldIndices(string $indexName, bool $dryRun = false): array
{
$indexName = $this->indexNameMapper->getPrefixedIndex($indexName);

$stateRequest = new State();
$stateRequest->setParams([
$state = $this->client->cluster()->state([
'filter_path' => 'metadata.indices.*.state,metadata.indices.*.aliases',
]);

$indexes = $this->client->requestEndpoint($stateRequest);
$indexes = $indexes->getData();
$indexes = $this->client->toElasticaResponse($state)->getData();
$indexes = $indexes['metadata']['indices'];

foreach ($indexes as $realIndexName => &$data) {
Expand Down
5 changes: 5 additions & 0 deletions src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public function setBulkRequestParams(array $bulkRequestParams): void
$this->refreshBulkRequestParams();
}

public function getClient(): Client
{
return $this->client;
}

protected function getCurrentBulk(): Bulk
{
if (!$this->currentBulk) {
Expand Down
4 changes: 0 additions & 4 deletions src/Messenger/IndexationRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use JoliCode\Elastically\Client;
use JoliCode\Elastically\Indexer;
use JoliCode\Elastically\IndexNameMapper;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -50,9 +49,6 @@ public function __construct(Client $client, MessageBusInterface $bus, DocumentEx
$this->exchanger = $exchanger;
$this->indexer = $indexer;
$this->indexNameMapper = $indexNameMapper;

// Disable the logs for memory concerns
$this->client->setLogger(new NullLogger());
}

/**
Expand Down
151 changes: 0 additions & 151 deletions src/Transport/HttpClientTransport.php

This file was deleted.

6 changes: 4 additions & 2 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace JoliCode\Elastically\Tests;

use Elastica\Request;
use Http\Discovery\Psr17Factory;
use JoliCode\Elastically\Client;
use JoliCode\Elastically\Factory;
use PHPUnit\Framework\TestCase;
Expand All @@ -21,15 +23,15 @@ abstract class BaseTestCase extends TestCase
{
protected function setUp(): void
{
$this->getFactory()->buildClient()->request('*', 'DELETE');
$this->getClient()->sendRequest((new Psr17Factory())->createRequest(Request::DELETE, '*'));
}

protected function getFactory(?string $path = null, array $config = []): Factory
{
return new Factory($config + [
Factory::CONFIG_MAPPINGS_DIRECTORY => $path ?? __DIR__ . '/configs',
'log' => false,
'port' => '9999',
'hosts' => ['http://127.0.0.1:9999'],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ public function testWithTransport(): void
'connections' => [
'default' => [
'client' => [
'transport' => HttpClientTransport::class,
'transport_config' => [
'http_client' => '@psr_es_client'
],
],
'mapping_directory' => __DIR__,
'index_class_mapping' => ['foobar' => self::class],
Expand All @@ -222,7 +224,7 @@ public function testWithTransport(): void
$container->compile();

$configArgument = $container->getDefinition('elastically.default.client')->getArgument('$config');
$this->assertInstanceOf(Reference::class, $configArgument['transport']);
$this->assertInstanceOf(Reference::class, $configArgument['transport_config']['http_client'] ?? null);
}

private function buildContainer(): ContainerBuilder
Expand Down
4 changes: 2 additions & 2 deletions tests/IndexBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace JoliCode\Elastically\Tests;

use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastica\Document as ElasticaDocument;
use Elastica\Exception\InvalidException;
use Elastica\Exception\ResponseException;
use Elastica\Index;
use Elastica\Index\Settings;
use JoliCode\Elastically\Factory;
Expand Down Expand Up @@ -175,7 +175,7 @@ public function testPurgeAndCloseOldIndices(IndexBuilder $indexBuilder): void
try {
$index2->search();
$this->assertFalse(true, 'Search should throw a "closed index" exception.');
} catch (ResponseException $e) {
} catch (ClientResponseException $e) {
$this->assertStringContainsStringIgnoringCase('closed', $e->getMessage());
}
}
Expand Down
Loading

0 comments on commit cbe1ea8

Please sign in to comment.