Skip to content

Commit

Permalink
Merge pull request #40 from utopia-php/preserveDates
Browse files Browse the repository at this point in the history
Preserve dates In Resources
  • Loading branch information
christyjacob4 authored Sep 24, 2024
2 parents fe7c8a4 + 55e9ae6 commit 6a06417
Show file tree
Hide file tree
Showing 23 changed files with 332 additions and 153 deletions.
2 changes: 1 addition & 1 deletion bin/MigrationCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MigrationCLI

protected mixed $destination;

protected const array STRUCTURE = [
protected const STRUCTURE = [
'$collection' => 'databases',
'$id' => 'collections',
'name' => 'Collections',
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"ext-curl": "*",
"ext-openssl": "*",
"appwrite/appwrite": "11.1.*",
"utopia-php/database": "0.52.*",
"utopia-php/database": "0.53.*",
"utopia-php/storage": "0.18.*",
"utopia-php/dsn": "0.2.*",
"utopia-php/framework": "0.33.*"
Expand Down
140 changes: 70 additions & 70 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/Migration/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function add(Resource $resource): void
$resource->setData(''); // Prevent Memory Leak
}

if($resource->getName() == Resource::TYPE_DOCUMENT) {
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
return;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public function update(Resource $resource): void
$this->add($resource);
}

if($resource->getName() == Resource::TYPE_DOCUMENT) {
if ($resource->getName() == Resource::TYPE_DOCUMENT) {
return;
}

Expand Down
16 changes: 14 additions & 2 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ protected function import(array $resources, callable $callback): void
$isLast = $index === $total - 1;

try {
$this->database->setPreserveDates(true);

$responseResource = match ($resource->getGroup()) {
Transfer::GROUP_DATABASES => $this->importDatabaseResource($resource, $isLast),
Transfer::GROUP_STORAGE => $this->importFileResource($resource),
Expand All @@ -247,6 +249,8 @@ protected function import(array $resources, callable $callback): void
}

$responseResource = $resource;
} finally {
$this->database->setPreserveDates(false);
}

$this->cache->update($responseResource);
Expand Down Expand Up @@ -313,6 +317,8 @@ protected function createDatabase(Database $resource): bool
'name' => $resource->getDatabaseName(),
'enabled' => true,
'search' => implode(' ', [$resource->getId(), $resource->getDatabaseName()]),
'$createdAt' => $resource->getCreatedAt(),
'$updatedAt' => $resource->getUpdatedAt(),
]));

$resource->setInternalId($database->getInternalId());
Expand Down Expand Up @@ -372,6 +378,8 @@ protected function createCollection(Collection $resource): bool
'enabled' => true,
'name' => $resource->getCollectionName(),
'search' => implode(' ', [$resource->getId(), $resource->getCollectionName()]),
'$createdAt' => $resource->getCreatedAt(),
'$updatedAt' => $resource->getUpdatedAt(),
]));

$resource->setInternalId($collection->getInternalId());
Expand Down Expand Up @@ -493,6 +501,8 @@ protected function createAttribute(Attribute $resource): bool
'formatOptions' => $resource->getFormatOptions(),
'filters' => $resource->getFilters(),
'options' => $resource->getOptions(),
'$createdAt' => $resource->getCreatedAt(),
'$updatedAt' => $resource->getUpdatedAt(),
]);

$this->database->checkAttribute($collection, $attribute);
Expand Down Expand Up @@ -549,6 +559,8 @@ protected function createAttribute(Attribute $resource): bool
'formatOptions' => $resource->getFormatOptions(),
'filters' => $resource->getFilters(),
'options' => $options,
'$createdAt' => $resource->getCreatedAt(),
'$updatedAt' => $resource->getUpdatedAt(),
]);

$this->database->createDocument('attributes', $twoWayAttribute);
Expand Down Expand Up @@ -792,6 +804,8 @@ protected function createIndex(Index $resource): bool
'attributes' => $resource->getAttributes(),
'lengths' => $lengths,
'orders' => $resource->getOrders(),
'$createdAt' => $resource->getCreatedAt(),
'$updatedAt' => $resource->getUpdatedAt(),
]);

$validator = new IndexValidator(
Expand Down Expand Up @@ -888,14 +902,12 @@ protected function createDocument(Document $resource, bool $isLast): bool
$collectionInternalId = $collection->getInternalId();

$this->database
->setPreserveDates(true)
->createDocuments(
'database_' . $databaseInternalId . '_collection_' . $collectionInternalId,
$this->documentBuffer
);
} finally {
$this->documentBuffer = [];
$this->database->setPreserveDates(false);
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ abstract class Resource implements \JsonSerializable

protected string $internalId = '';

protected string $createdAt = '';
protected string $updatedAt = '';

protected string $status = self::STATUS_PENDING;

protected string $message = '';
Expand Down Expand Up @@ -168,4 +171,35 @@ public function setPermissions(array $permissions): self

return $this;
}

/**
* @returns string
*/
public function getCreatedAt(): string
{
return $this->createdAt;
}

/**
* @returns string
*/
public function getUpdatedAt(): string
{
return $this->updatedAt;
}


public function setCreatedAt(string $date): self
{
$this->createdAt = $date;

return $this;
}

public function setUpdatedAt(string $date): self
{
$this->updatedAt = $date;

return $this;
}
}
16 changes: 8 additions & 8 deletions src/Migration/Resources/Auth/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
*/
class Hash extends Resource
{
public const string ALGORITHM_SCRYPT_MODIFIED = 'scryptModified';
public const ALGORITHM_SCRYPT_MODIFIED = 'scryptModified';

public const string ALGORITHM_BCRYPT = 'bcrypt';
public const ALGORITHM_BCRYPT = 'bcrypt';

public const string ALGORITHM_MD5 = 'md5';
public const ALGORITHM_MD5 = 'md5';

public const string ALGORITHM_ARGON2 = 'argon2';
public const ALGORITHM_ARGON2 = 'argon2';

public const string ALGORITHM_SHA256 = 'sha256';
public const ALGORITHM_SHA256 = 'sha256';

public const string ALGORITHM_PHPASS = 'phpass';
public const ALGORITHM_PHPASS = 'phpass';

public const string ALGORITHM_SCRYPT = 'scrypt';
public const ALGORITHM_SCRYPT = 'scrypt';

public const string ALGORITHM_PLAINTEXT = 'plainText';
public const ALGORITHM_PLAINTEXT = 'plainText';

public function __construct(
private readonly string $hash,
Expand Down
24 changes: 14 additions & 10 deletions src/Migration/Resources/Database/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

abstract class Attribute extends Resource
{
public const string TYPE_STRING = 'string';
public const string TYPE_INTEGER = 'int';
public const string TYPE_FLOAT = 'float';
public const string TYPE_BOOLEAN = 'bool';
public const string TYPE_DATETIME = 'dateTime';
public const string TYPE_EMAIL = 'email';
public const string TYPE_ENUM = 'enum';
public const string TYPE_IP = 'IP';
public const string TYPE_URL = 'URL';
public const string TYPE_RELATIONSHIP = 'relationship';
public const TYPE_STRING = 'string';
public const TYPE_INTEGER = 'int';
public const TYPE_FLOAT = 'float';
public const TYPE_BOOLEAN = 'bool';
public const TYPE_DATETIME = 'dateTime';
public const TYPE_EMAIL = 'email';
public const TYPE_ENUM = 'enum';
public const TYPE_IP = 'IP';
public const TYPE_URL = 'URL';
public const TYPE_RELATIONSHIP = 'relationship';

/**
* @param string $key
Expand All @@ -43,6 +43,8 @@ public function __construct(
protected readonly array $formatOptions = [],
protected readonly array $filters = [],
protected array $options = [],
protected string $createdAt = '',
protected string $updatedAt = '',
) {
}

Expand All @@ -64,6 +66,8 @@ public function jsonSerialize(): array
'formatOptions' => $this->formatOptions,
'filters' => $this->filters,
'options' => $this->options,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
}

Expand Down
10 changes: 9 additions & 1 deletion src/Migration/Resources/Database/Attributes/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ public function __construct(
bool $required = false,
?bool $default = null,
bool $array = false,
string $createdAt = '',
string $updatedAt = ''
) {
parent::__construct(
$key,
$collection,
required: $required,
default: $default,
array: $array
array: $array,
createdAt: $createdAt,
updatedAt: $updatedAt
);
}

Expand All @@ -39,6 +43,8 @@ public function __construct(
* required: bool,
* array: bool,
* default: ?bool,
* createdAt: string,
* updatedAt: string,
* } $array
* @return self
*/
Expand All @@ -50,6 +56,8 @@ public static function fromArray(array $array): self
required: $array['required'],
default: $array['default'],
array: $array['array'],
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Migration/Resources/Database/Attributes/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function __construct(
bool $required = false,
?string $default = null,
bool $array = false,
string $createdAt = '',
string $updatedAt = ''
) {
parent::__construct(
$key,
Expand All @@ -21,6 +23,8 @@ public function __construct(
default: $default,
array: $array,
filters: ['datetime'],
createdAt: $createdAt,
updatedAt: $updatedAt
);
}

Expand All @@ -45,6 +49,8 @@ public function getType(): string
* required: bool,
* array: bool,
* default: ?string,
* createdAt: string,
* updatedAt: string,
* } $array
* @return self
*/
Expand All @@ -56,6 +62,8 @@ public static function fromArray(array $array): self
required: $array['required'],
default: $array['default'],
array: $array['array'],
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}
}
12 changes: 10 additions & 2 deletions src/Migration/Resources/Database/Attributes/Decimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function __construct(
?float $min = null,
?float $max = null,
bool $signed = true,
string $createdAt = '',
string $updatedAt = ''
) {
$min ??= PHP_FLOAT_MIN;
$max ??= PHP_FLOAT_MAX;
Expand All @@ -30,7 +32,9 @@ public function __construct(
formatOptions: [
'min' => $min,
'max' => $max,
]
],
createdAt: $createdAt,
updatedAt: $updatedAt
);
}

Expand All @@ -53,7 +57,9 @@ public function __construct(
* formatOptions: array{
* min: ?float,
* max: ?float
* }
* },
* createdAt: string,
* updatedAt: string,
* } $array
* @return self
*/
Expand All @@ -67,6 +73,8 @@ public static function fromArray(array $array): self
array: $array['array'],
min: $array['formatOptions']['min'],
max: $array['formatOptions']['max'],
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Migration/Resources/Database/Attributes/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public function __construct(
bool $required = false,
?string $default = null,
bool $array = false,
int $size = 254
int $size = 254,
string $createdAt = '',
string $updatedAt = ''
) {
parent::__construct(
$key,
Expand All @@ -23,6 +25,8 @@ public function __construct(
array: $array,
size: $size,
format: 'email',
createdAt: $createdAt,
updatedAt: $updatedAt
);
}

Expand Down
Loading

0 comments on commit 6a06417

Please sign in to comment.