Skip to content

Commit

Permalink
feat(Firestore): Part 2 of Upgrade Firestore V2 (#7300)
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 authored Jun 13, 2024
1 parent 10a1b3a commit 5309261
Show file tree
Hide file tree
Showing 91 changed files with 2,273 additions and 4,897 deletions.
10 changes: 3 additions & 7 deletions Core/src/Testing/FirestoreTestHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ trait FirestoreTestHelperTrait

private static $_serializer;

/**
* @return Serializer
*/
private function getSerializer()
{
if (!self::$_serializer) {
Expand All @@ -44,13 +47,6 @@ private function getSerializer()
'google.protobuf.Struct' => function ($v) {
return $this->flattenStruct($v);
},
'google.protobuf.Timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
},
], [], [
'google.protobuf.Int32Value' => function ($v) {
return ['value' => $v];
}
]);
}

Expand Down
6 changes: 0 additions & 6 deletions Core/tests/Unit/ServiceBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Google\Cloud\Core\Testing\CheckForClassTrait;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Google\Cloud\Datastore\DatastoreClient;
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Language\LanguageClient;
use Google\Cloud\Logging\LoggingClient;
use Google\Cloud\Spanner\SpannerClient;
Expand Down Expand Up @@ -164,11 +163,6 @@ public function serviceProvider()
], [
'datastore',
DatastoreClient::class
], [
'firestore',
FirestoreClient::class,
[],
[$this, 'checkAndSkipGrpcTests']
], [
'logging',
LoggingClient::class
Expand Down
43 changes: 43 additions & 0 deletions Firestore/MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Migration Guide for V2 library


## Timestamp

Throughout the exposed functionalities of the library, all the `\Google\Cloud\Core\Timestamp` usage has been replaced with `\Google\Protobuf\Timestamp`.

Earlier usage:

```php
use Google\Cloud\Code\Timestamp;

$timestamp = new Timestamp(new \DateTime());
```

Current usage:

```php
use Google\Protobuf\Timestamp;

$timestamp = new Timestamp();
$timestamp->fromDateTime(new \DateTime());
```
## ListCollectionIds

ListCollectionIds rpc is exposed via `DocumentReference::collections()` and `FirestoreClient::collections()` methods.
These method used to return an `\InvalidArgumentException` when `readTime` was invalid. Now we've removed this client
level check and exception is thrown by the Serializer when it converts the array data into Protobuf Request object.

## ListDocuments

ListDocuments rpc is exposed via `CollectionReference::listDocuments()` method. These method used to return an `\InvalidArgumentException`
when `readTime` was invalid. Now we've removed this client level check and exception is thrown by the Serializer
when it converts the array data into Protobuf Request object.

## RunQuery

RunQuery RPC is exposed via `CollectionReference::documents()` and `Transaction::runQuery()` methods. These method used to return an `\InvalidArgumentException`
when `readTime` was invalid. Now we've removed this client level check and exception is thrown by the Serializer
when it converts the array data into Protobuf Request object.



42 changes: 20 additions & 22 deletions Firestore/src/AggregateQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
namespace Google\Cloud\Firestore;

use Google\ApiCore\Serializer;
use Google\Cloud\Core\ApiHelperTrait;
use Google\Cloud\Core\RequestHandler;
use Google\Cloud\Firestore\Connection\ConnectionInterface;
use Google\Cloud\Firestore\QueryTrait;
use Google\Cloud\Firestore\V1\Client\FirestoreClient;
use Google\Cloud\Firestore\V1\RunAggregationQueryRequest;

/**
* A Cloud Firestore Aggregate Query.
Expand All @@ -37,14 +39,9 @@
*/
class AggregateQuery
{
use ApiHelperTrait;
use QueryTrait;

/**
* @var ConnectionInterface
* @internal
*/
private $connection;

/**
* @var RequestHandler
*/
Expand Down Expand Up @@ -73,9 +70,6 @@ class AggregateQuery
/**
* Create an aggregation query.
*
* @param ConnectionInterface $connection A Connection to Cloud Firestore.
* This object is created by FirestoreClient,
* and should not be instantiated outside of this client.
* @param RequestHandler $requestHandler The request handler responsible for sending
* requests and serializing responses into relevant classes.
* @param Serializer $serializer The serializer instance to encode/decode messages.
Expand All @@ -84,14 +78,12 @@ class AggregateQuery
* @param Aggregate $aggregate Aggregation over the provided query.
*/
public function __construct(
ConnectionInterface $connection,
RequestHandler $requestHandler,
Serializer $serializer,
$parent,
array $query,
Aggregate $aggregate
) {
$this->connection = $connection;
$this->requestHandler = $requestHandler;
$this->serializer = $serializer;
$this->parentName = $parent;
Expand All @@ -114,25 +106,31 @@ public function addAggregation($aggregate)
/**
* Executes the AggregateQuery.
*
* @param array $options [optional] {
* Configuration options is an array.
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/firestore/docs/reference/rpc/google.firestore.v1#runaggregationqueryrequest RunAggregationqueryRequest
* @codingStandardsIgnoreEnd
*
* @param array $options [optional] Configuration options is an array.
*
* @type Timestamp $readTime Reads entities as they were at the given timestamp.
* }
* @return AggregateQuerySnapshot
*/
public function getSnapshot($options = [])
{
$parsedAggregates = [];
foreach ($this->aggregates as $aggregate) {
$parsedAggregates[] = $aggregate->getProps();
}
$snapshot = $this->connection->runAggregationQuery([
list($data, $optionalArgs) = $this->splitOptionalArgs($options);
$data += $this->arrayFilterRemoveNull([
'parent' => $this->parentName,
'structuredAggregationQuery' => $this->aggregateQueryPrepare([
'aggregates' => $this->aggregates
] + $this->query),
] + $options)->current();
]);
$request = $this->serializer->decodeMessage(new RunAggregationQueryRequest(), $data);

$snapshot = $this->requestHandler->sendRequest(
FirestoreClient::class,
'runAggregationQuery',
$request,
$optionalArgs
)->current();

return new AggregateQuerySnapshot($snapshot);
}
Expand Down
11 changes: 3 additions & 8 deletions Firestore/src/AggregateQuerySnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

namespace Google\Cloud\Firestore;

use Google\Cloud\Core\Timestamp;
use Google\Cloud\Core\TimeTrait;

/**
* Represents the result set of an AggregateQuery.
*
Expand All @@ -36,10 +33,9 @@
*/
class AggregateQuerySnapshot
{
use TimeTrait;

/**
* @var Timestamp
* @var array
*/
private $readTime;

Expand All @@ -64,8 +60,7 @@ public function __construct($snapshot = [])
$this->transaction = $snapshot['transaction'];
}
if (isset($snapshot['readTime'])) {
$time = $this->parseTimeString($snapshot['readTime']);
$this->readTime = new Timestamp($time[0], $time[1]);
$this->readTime = $snapshot['readTime'];
}
if (isset($snapshot['result']['aggregateFields'])) {
$this->aggregateFields = $snapshot['result']['aggregateFields'];
Expand All @@ -85,7 +80,7 @@ public function getTransaction()
/**
* Get the Aggregation read time.
*
* @return Timestamp
* @return array|null
*/
public function getReadTime()
{
Expand Down
Loading

0 comments on commit 5309261

Please sign in to comment.