Merge pull request #19 from utopia-php/feat-bulk-writes-2

Upsert
This commit is contained in:
Jake Barnby authored and GitHub committed 2025-07-24 02:55:58 +12:00
commit 6b62e8daa5
10 files changed
+359 -450

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.0', '8.1', '8.2', '8.3'] # add PHP versions as required
php-versions: ['8.3'] # add PHP versions as required
steps:
- name: Checkout repository
-57
View File
@@ -1,57 +0,0 @@
FROM composer:2.0 as composer
ARG TESTING=false
ENV TESTING=$TESTING
WORKDIR /usr/local/src/
COPY composer.lock /usr/local/src/
COPY composer.json /usr/local/src/
RUN composer update --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist
FROM appwrite/utopia-base:php-8.0-0.1.0 as compile
ENV PHP_MONGO_VERSION=1.11.1
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN \
apk update \
&& apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git \
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
&& rm -rf /var/cache/apk/*
## MongoDB Extension
FROM compile AS mongodb
RUN \
git clone --depth 1 --branch $PHP_MONGO_VERSION https://github.com/mongodb/mongo-php-driver.git \
&& cd mongo-php-driver \
&& git submodule update --init \
&& phpize \
&& ./configure \
&& make && make install
FROM compile as final
LABEL maintainer="team@appwrite.io"
WORKDIR /usr/src/code
RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini
RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini
COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20200930/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
# Add Source Code
COPY . /usr/src/code
CMD [ "tail", "-f", "/dev/null" ]
-57
View File
@@ -1,57 +0,0 @@
FROM composer:2.0 as composer
ARG TESTING=false
ENV TESTING=$TESTING
WORKDIR /usr/local/src/
COPY composer.lock /usr/local/src/
COPY composer.json /usr/local/src/
RUN composer update --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist
FROM appwrite/utopia-base:php-8.1-0.1.0 as compile
ENV PHP_MONGO_VERSION=1.11.1
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN \
apk update \
&& apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git \
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
&& rm -rf /var/cache/apk/*
## MongoDB Extension
FROM compile AS mongodb
RUN \
git clone --depth 1 --branch $PHP_MONGO_VERSION https://github.com/mongodb/mongo-php-driver.git \
&& cd mongo-php-driver \
&& git submodule update --init \
&& phpize \
&& ./configure \
&& make && make install
FROM compile as final
LABEL maintainer="team@appwrite.io"
WORKDIR /usr/src/code
RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini
RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini
COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20210902/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20210902/
# Add Source Code
COPY . /usr/src/code
CMD [ "tail", "-f", "/dev/null" ]
-57
View File
@@ -1,57 +0,0 @@
FROM composer:2.0 as composer
ARG TESTING=false
ENV TESTING=$TESTING
WORKDIR /usr/local/src/
COPY composer.lock /usr/local/src/
COPY composer.json /usr/local/src/
RUN composer update --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist
FROM appwrite/utopia-base:php-8.2-0.1.0 as compile
ENV PHP_MONGO_VERSION=1.11.1
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN \
apk update \
&& apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ git \
&& docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \
&& rm -rf /var/cache/apk/*
## MongoDB Extension
FROM compile AS mongodb
RUN \
git clone --depth 1 --branch $PHP_MONGO_VERSION https://github.com/mongodb/mongo-php-driver.git \
&& cd mongo-php-driver \
&& git submodule update --init \
&& phpize \
&& ./configure \
&& make && make install
FROM compile as final
LABEL maintainer="team@appwrite.io"
WORKDIR /usr/src/code
RUN echo extension=mongodb.so >> /usr/local/etc/php/conf.d/mongodb.ini
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini
RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini
COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20220829/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20220829/
# Add Source Code
COPY . /usr/src/code
CMD [ "tail", "-f", "/dev/null" ]
+1 -1
View File
@@ -13,7 +13,7 @@ RUN composer update --ignore-platform-reqs --optimize-autoloader \
FROM appwrite/utopia-base:php-8.3-0.1.0 as compile
ENV PHP_MONGO_VERSION=1.11.1
ENV PHP_MONGO_VERSION=2.1.1
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
+3 -3
View File
@@ -29,14 +29,14 @@
},
"require": {
"php": ">=8.0",
"ext-mongodb": "*",
"mongodb/mongodb": "1.10.0"
"ext-mongodb": "2.1.1",
"mongodb/mongodb": "2.1.0"
},
"require-dev": {
"fakerphp/faker": "^1.14",
"phpunit/phpunit": "^9.4",
"swoole/ide-helper": "4.8.0",
"laravel/pint": "1.2.*",
"phpstan/phpstan": "1.8.*"
"phpstan/phpstan": "2.1.*"
}
}
Generated
+248 -245
View File
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -10,7 +10,7 @@ class Auth
private string $secret;
private string $authzid;
private string $gs2Header;
private string $cnonce;
private ?string $cnonce = null;
private string $firstMessageBare;
private string $saltedPassword;
private string $authMessage;
@@ -218,7 +218,7 @@ class Auth
/**
* @return string
*/
public function getCnonce(): string
public function getCnonce(): ?string
{
return $this->cnonce;
}
+67 -27
View File
@@ -2,7 +2,8 @@
namespace Utopia\Mongo;
use MongoDB\BSON;
use MongoDB\BSON\Document;
use MongoDB\BSON\ObjectId;
use Swoole\Client as SwooleClient;
use Swoole\Coroutine\Client as CoroutineClient;
use stdClass;
@@ -136,7 +137,7 @@ class Client
'$db' => $db ?? $this->database,
]);
$sections = BSON\fromPHP($params);
$sections = Document::fromPHP($params);
$message = pack('V*', 21 + strlen($sections), $this->id, 0, 2013, 0) . "\0" . $sections;
return $this->send($message);
}
@@ -196,11 +197,24 @@ class Client
(!isset($responseLength)) || ($receivedLength < $responseLength)
);
/**
* @var stdClass $result
/*
* The first 21 bytes of the MongoDB wire protocol response consist of:
* - 16 bytes: Standard message header, which includes:
* - messageLength (4 bytes): Total size of the message, including the header.
* - requestID (4 bytes): Identifier for this message.
* - responseTo (4 bytes): The requestID that this message is responding to.
* - opCode (4 bytes): The operation code for the message type (e.g., OP_MSG).
* - 4 bytes: flagBits, which provide additional information about the message.
* - 1 byte: payloadType, indicating the type of the following payload (usually 0 for a BSON document).
*
* These 21 bytes are protocol metadata and precede the actual BSON-encoded document in the response.
*/
$result = BSON\toPHP(substr($res, 21, $responseLength - 21));
$bsonString = substr($res, 21, $responseLength - 21);
$result = Document::fromBSON($bsonString)->toPHP();
if (is_array($result)) {
$result = (object) $result;
}
if (property_exists($result, "writeErrors")) {
// Throws Utopia\Mongo\Exception
throw new Exception(
@@ -434,7 +448,7 @@ class Client
$docObj->{$key} = $value;
}
$docObj->_id ??= new BSON\ObjectId();
$docObj->_id ??= new ObjectId();
$this->query(array_merge([
self::COMMAND_INSERT => $collection,
@@ -459,7 +473,7 @@ class Client
$docObj->{$key} = $value;
}
$docObj->_id ??= new BSON\ObjectId();
$docObj->_id ??= new ObjectId();
$docObjs[] = $docObj;
}
@@ -533,48 +547,52 @@ class Client
}
/**
* Insert, or update, a document/s.
* Insert, or update, document(s) with support for bulk operations.
* https://docs.mongodb.com/manual/reference/command/update/#syntax
*
* @param string $collection
* @param array $where
* @param array $updates
* @param array $operations Array of operations, each with 'filter' and 'update' keys
* @param array $options
*
* @return Client
* @return self
* @throws Exception
*/
public function upsert(string $collection, array $where = [], array $updates = [], array $options = []): self
public function upsert(string $collection, array $operations, array $options = []): self
{
$cleanUpdates = [];
$updates = [];
foreach ($updates as $k => $v) {
if (\is_null($v)) {
continue;
foreach ($operations as $op) {
$cleanUpdate = [];
foreach ($op['update'] as $k => $v) {
if (!is_null($v)) {
$cleanUpdate[$k] = $v;
}
}
$cleanUpdates[$k] = $v;
}
$updateOperation = [
'q' => $op['filter'],
'u' => $this->toObject($cleanUpdate),
'upsert' => true,
'multi' => isset($op['multi']) ? $op['multi'] : false,
];
$updates[] = $updateOperation;
}
$this->query(
array_merge(
[
'update' => $collection,
'updates' => [
[
'q' => ['_uid' => $where['_uid']],
'u' => ['$set' => $cleanUpdates],
]
],
self::COMMAND_UPDATE => $collection,
'updates' => $updates,
],
$options
)
);
return $this;
}
/**
* Find a document/s.
* https://docs.mongodb.com/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find
@@ -782,4 +800,26 @@ class Client
return $cleanedFilters;
}
private ?bool $replicaSet = null;
/**
* Check if MongoDB is running as a replica set.
*
* @return bool True if this is a replica set, false if standalone
* @throws Exception
*/
public function isReplicaSet(): bool
{
if ($this->replicaSet !== null) {
return $this->replicaSet;
}
$result = $this->query([
'isMaster' => 1,
], 'admin');
$this->replicaSet = property_exists($result, 'setName');
return $this->replicaSet;
}
}
+37
View File
@@ -224,4 +224,41 @@ class MongoTest extends TestCase
['maxTimeMS'=> 1]
)->cursor->firstBatch ?? [];
}
public function testUpsert()
{
$this->getDatabase()->insert(
'movies_upsert',
[
'name' => 'Gone with the wind',
'language' => 'English',
'country' => 'UK',
'counter' => 1
]
);
$this->getDatabase()->upsert('movies_upsert', [
[
'filter' => ['name' => 'Gone with the wind'],
'update' => [
'$set' => ['country' => 'USA'],
'$inc' => ['counter' => 3]
]
],
[
'filter' => ['name' => 'The godfather'],
'update' => [
'$set' => ['name' => 'The godfather 2', 'country' => 'USA', 'language' => 'English']
]
],
]);
$documents = $this->getDatabase()->find('movies_upsert')->cursor->firstBatch ?? [];
self::assertCount(2, $documents);
self::assertEquals(4, $documents[0]->counter);
self::assertEquals('The godfather 2', $documents[1]->name);
self::assertEquals('USA', $documents[1]->country);
self::assertEquals('English', $documents[1]->language);
}
}