Skip to content

Commit

Permalink
Specify time series options when creating collection
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Oct 10, 2024
1 parent e4c8142 commit d2a5c5a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Doctrine/ODM/MongoDB/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,20 @@ public function createDocumentCollection(string $documentName, ?int $maxTimeMs =
$options['validationLevel'] = $class->getValidationLevel();
}

if ($class->isTimeSeries) {
$options['timeseries'] = array_filter(
[
'timeField' => $class->timeSeriesOptions->timeField,
'metaField' => $class->timeSeriesOptions->metaField,
'granularity' => $class->timeSeriesOptions->granularity?->value,
],
);

if ($class->timeSeriesOptions->expireAfterSeconds) {
$options['expireAfterSeconds'] = $class->timeSeriesOptions->expireAfterSeconds;
}
}

$this->dm->getDocumentDatabase($documentName)->createCollection(
$class->getCollection(),
$this->getWriteOptions($maxTimeMs, $writeConcern, $options),
Expand Down
28 changes: 28 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Documents\Sharded\ShardedOne;
use Documents\Sharded\ShardedOneWithDifferentKey;
use Documents\SimpleReferenceUser;
use Documents\TimeSeries\TimeSeriesDocument;
use Documents\Tournament\Tournament;
use Documents\UserName;
use InvalidArgumentException;
Expand Down Expand Up @@ -760,6 +761,33 @@ public function testCreateView(array $expectedWriteOptions, ?int $maxTimeMs, ?Wr
$this->schemaManager->createDocumentCollection(UserName::class, $maxTimeMs, $writeConcern);
}

/** @psalm-param IndexOptions $expectedWriteOptions */
#[DataProvider('getWriteOptions')]
public function testCreateTimeSeriesCollection(array $expectedWriteOptions, ?int $maxTimeMs, ?WriteConcern $writeConcern): void
{
$metadata = $this->dm->getClassMetadata(TimeSeriesDocument::class);

$options = [
'timeseries' => [
'timeField' => 'time',
'metaField' => 'metadata',
'granularity' => 'seconds',
],
'expireAfterSeconds' => 86400,
];

$database = $this->documentDatabases[$this->getDatabaseName($metadata)];
$database
->expects($this->once())
->method('createCollection')
->with(
'TimeSeriesDocument',
$this->writeOptions($options + $expectedWriteOptions),

Check failure on line 785 in tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

InvalidArgument

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php:785:37: InvalidArgument: Argument 1 of Doctrine\ODM\MongoDB\Tests\SchemaManagerTest::writeOptions expects array{background?: bool, bits?: int, default_language?: string, expireAfterSeconds?: int, language_override?: string, max?: float, min?: float, name?: string, partialFilterExpression?: array<array-key, mixed>, sparse?: bool, storageEngine?: array<array-key, mixed>, textIndexVersion?: int, unique?: bool, weights?: list{string, int}}, but array{background?: bool, bits?: int, default_language?: string, expireAfterSeconds: 86400, language_override?: string, max?: float, min?: float, name?: string, partialFilterExpression?: array<array-key, mixed>, sparse?: bool, storageEngine?: array<array-key, mixed>, textIndexVersion?: int, timeseries: array{granularity: 'seconds', metaField: 'metadata', timeField: 'time'}, unique?: bool, weights?: list{string, int}} with additional array shape fields (timeseries) was provided (see https://psalm.dev/004)
);

$this->schemaManager->createDocumentCollection(TimeSeriesDocument::class, $maxTimeMs, $writeConcern);
}

/** @psalm-param IndexOptions $expectedWriteOptions */
#[DataProvider('getWriteOptions')]
public function testCreateCollections(array $expectedWriteOptions, ?int $maxTimeMs, ?WriteConcern $writeConcern): void
Expand Down
26 changes: 26 additions & 0 deletions tests/Documents/TimeSeries/TimeSeriesDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Documents\TimeSeries;

use DateTime;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\TimeSeries\Granularity;

#[ODM\Document]
#[ODM\TimeSeries('time', 'metadata', Granularity::Seconds, 86400)]
class TimeSeriesDocument
{
#[ODM\Id]
public ?string $id;

#[ODM\Field]
public DateTime $time;

#[ODM\Field]
public string $metadata;

#[ODM\Field]
public int $value;
}

0 comments on commit d2a5c5a

Please sign in to comment.