Skip to content

Commit 79e7eba

Browse files
updated spatial update attribute
1 parent f06a57a commit 79e7eba

7 files changed

Lines changed: 63 additions & 9 deletions

File tree

src/Database/Adapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,11 @@ abstract public function createAttributes(string $collection, array $attributes)
578578
* @param bool $signed
579579
* @param bool $array
580580
* @param string|null $newKey
581+
* @param bool $required
581582
*
582583
* @return bool
583584
*/
584-
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool;
585+
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool;
585586

586587
/**
587588
* Delete Attribute

src/Database/Adapter/MariaDB.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,16 @@ public function getSchemaAttributes(string $collection): array
409409
* @param bool $signed
410410
* @param bool $array
411411
* @param string|null $newKey
412+
* @param bool $required
412413
* @return bool
413414
* @throws DatabaseException
414415
*/
415-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
416+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
416417
{
417418
$name = $this->filter($collection);
418419
$id = $this->filter($id);
419420
$newKey = empty($newKey) ? null : $this->filter($newKey);
420-
$type = $this->getSQLType($type, $size, $signed, $array, false);
421-
421+
$type = $this->getSQLType($type, $size, $signed, $array, $required);
422422
if (!empty($newKey)) {
423423
$sql = "ALTER TABLE {$this->getSQLTable($name)} CHANGE COLUMN `{$id}` `{$newKey}` {$type};";
424424
} else {

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function createAttributes(string $collection, array $attributes): bool
175175
return $this->delegate(__FUNCTION__, \func_get_args());
176176
}
177177

178-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
178+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
179179
{
180180
return $this->delegate(__FUNCTION__, \func_get_args());
181181
}

src/Database/Adapter/Postgres.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,17 @@ public function renameAttribute(string $collection, string $old, string $new): b
535535
* @param bool $signed
536536
* @param bool $array
537537
* @param string|null $newKey
538+
* @param bool $required
538539
* @return bool
539540
* @throws Exception
540541
* @throws PDOException
541542
*/
542-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
543+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
543544
{
544545
$name = $this->filter($collection);
545546
$id = $this->filter($id);
546547
$newKey = empty($newKey) ? null : $this->filter($newKey);
547-
$type = $this->getSQLType($type, $size, $signed, $array, false);
548+
$type = $this->getSQLType($type, $size, $signed, $array, $required);
548549

549550
if ($type == 'TIMESTAMP(3)') {
550551
$type = "TIMESTAMP(3) without time zone USING TO_TIMESTAMP(\"$id\", 'YYYY-MM-DD HH24:MI:SS.MS')";

src/Database/Adapter/SQLite.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,12 @@ public function analyzeCollection(string $collection): bool
327327
* @param bool $signed
328328
* @param bool $array
329329
* @param string|null $newKey
330+
* @param bool $required
330331
* @return bool
331332
* @throws Exception
332333
* @throws PDOException
333334
*/
334-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null): bool
335+
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
335336
{
336337
if (!empty($newKey) && $newKey !== $id) {
337338
return $this->renameAttribute($collection, $id, $newKey);

src/Database/Database.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,10 @@ public function updateAttribute(string $collection, string $id, ?string $type =
21612161
$default = null;
21622162
}
21632163

2164+
if ($required === true && in_array($type, Database::SPATIAL_TYPES)) {
2165+
$altering = true;
2166+
}
2167+
21642168
switch ($type) {
21652169
case self::VAR_STRING:
21662170
if (empty($size)) {
@@ -2322,7 +2326,7 @@ public function updateAttribute(string $collection, string $id, ?string $type =
23222326
}
23232327
}
23242328

2325-
$updated = $this->adapter->updateAttribute($collection, $id, $type, $size, $signed, $array, $newKey);
2329+
$updated = $this->adapter->updateAttribute($collection, $id, $type, $size, $signed, $array, $newKey, $required);
23262330

23272331
if (!$updated) {
23282332
throw new DatabaseException('Failed to update attribute');

tests/e2e/Adapter/Scopes/SpatialTests.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,53 @@ public function testSpatialIndex(): void
840840
} finally {
841841
$database->deleteCollection($collNullIndex);
842842
}
843+
844+
$collUpdateNull = 'spatial_idx_update_null_' . uniqid();
845+
try {
846+
$database->createCollection($collUpdateNull);
847+
848+
$database->createAttribute($collUpdateNull, 'loc', Database::VAR_POINT, 0, false);
849+
if (!$nullSupported) {
850+
try {
851+
$database->createIndex($collUpdateNull, 'idx_loc', Database::INDEX_SPATIAL, ['loc']);
852+
$this->fail('Expected exception when creating spatial index on NULL-able attribute');
853+
} catch (\Throwable $e) {
854+
$this->assertInstanceOf(Exception::class, $e);
855+
}
856+
} else {
857+
$this->assertTrue($database->createIndex($collUpdateNull, 'idx_loc', Database::INDEX_SPATIAL, ['loc']));
858+
}
859+
860+
$database->updateAttribute($collUpdateNull, 'loc', required: true);
861+
862+
$this->assertTrue($database->createIndex($collUpdateNull, 'idx_loc_req', Database::INDEX_SPATIAL, ['loc']));
863+
} finally {
864+
$database->deleteCollection($collUpdateNull);
865+
}
866+
867+
868+
$collUpdateNull = 'spatial_idx_index_null_' . uniqid();
869+
try {
870+
$database->createCollection($collUpdateNull);
871+
872+
$database->createAttribute($collUpdateNull, 'loc', Database::VAR_POINT, 0, false);
873+
if (!$nullSupported) {
874+
try {
875+
$database->createIndex($collUpdateNull, 'idx_loc', Database::INDEX_SPATIAL, ['loc']);
876+
$this->fail('Expected exception when creating spatial index on NULL-able attribute');
877+
} catch (\Throwable $e) {
878+
$this->assertInstanceOf(Exception::class, $e);
879+
}
880+
} else {
881+
$this->assertTrue($database->createIndex($collUpdateNull, 'idx_loc', Database::INDEX_SPATIAL, ['loc']));
882+
}
883+
884+
$database->updateAttribute($collUpdateNull, 'loc', required: true);
885+
886+
$this->assertTrue($database->createIndex($collUpdateNull, 'idx_loc_req', Database::INDEX_SPATIAL, ['loc']));
887+
} finally {
888+
$database->deleteCollection($collUpdateNull);
889+
}
843890
}
844891

845892
public function testComplexGeometricShapes(): void

0 commit comments

Comments
 (0)