Skip to content

Commit 5514bb7

Browse files
authored
Merge pull request #707 from utopia-php/fix-spatial-required-cols
Handling not null spatial column in the existing data
2 parents 066e2bd + 3820bbe commit 5514bb7

7 files changed

Lines changed: 96 additions & 0 deletions

File tree

src/Database/Adapter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,13 @@ abstract public function getSupportForSpatialAttributes(): bool;
10641064
*/
10651065
abstract public function getSupportForSpatialIndexNull(): bool;
10661066

1067+
/**
1068+
* Adapter supports optional spatial attributes with existing rows.
1069+
*
1070+
* @return bool
1071+
*/
1072+
abstract public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool;
1073+
10671074
/**
10681075
* Does the adapter support order attribute in spatial indexes?
10691076
*

src/Database/Adapter/MariaDB.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,4 +1917,14 @@ public function getSupportForSpatialAxisOrder(): bool
19171917
{
19181918
return false;
19191919
}
1920+
1921+
/**
1922+
* Adapter supports optional spatial attributes with existing rows.
1923+
*
1924+
* @return bool
1925+
*/
1926+
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
1927+
{
1928+
return true;
1929+
}
19201930
}

src/Database/Adapter/MySQL.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Utopia\Database\Database;
77
use Utopia\Database\Exception as DatabaseException;
88
use Utopia\Database\Exception\Dependency as DependencyException;
9+
use Utopia\Database\Exception\Structure as StructureException;
910
use Utopia\Database\Exception\Timeout as TimeoutException;
1011
use Utopia\Database\Query;
1112

@@ -155,6 +156,10 @@ protected function processException(PDOException $e): \Exception
155156
return new DependencyException('Attribute cannot be deleted because it is used in an index', $e->getCode(), $e);
156157
}
157158

159+
if ($e->getCode() === '22004' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1138) {
160+
return new StructureException('Attribute does not allow null values', $e->getCode(), $e);
161+
}
162+
158163
return parent::processException($e);
159164
}
160165
/**
@@ -249,4 +254,14 @@ protected function getSpatialAxisOrderSpec(): string
249254
{
250255
return "'axis-order=long-lat'";
251256
}
257+
258+
/**
259+
* Adapter supports optional spatial attributes with existing rows.
260+
*
261+
* @return bool
262+
*/
263+
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
264+
{
265+
return false;
266+
}
252267
}

src/Database/Adapter/Pool.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,14 @@ public function getSupportForSpatialAxisOrder(): bool
549549
{
550550
return $this->delegate(__FUNCTION__, \func_get_args());
551551
}
552+
553+
/**
554+
* Adapter supports optional spatial attributes with existing rows.
555+
*
556+
* @return bool
557+
*/
558+
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
559+
{
560+
return $this->delegate(__FUNCTION__, \func_get_args());
561+
}
552562
}

src/Database/Adapter/Postgres.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,4 +2002,14 @@ public function getSupportForSpatialAxisOrder(): bool
20022002
{
20032003
return false;
20042004
}
2005+
2006+
/**
2007+
* Adapter supports optional spatial attributes with existing rows.
2008+
*
2009+
* @return bool
2010+
*/
2011+
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
2012+
{
2013+
return false;
2014+
}
20052015
}

src/Database/Adapter/SQLite.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,4 +1284,14 @@ public function getSupportForSpatialAxisOrder(): bool
12841284
{
12851285
return false;
12861286
}
1287+
1288+
/**
1289+
* Adapter supports optionalspatial attributes with existing rows.
1290+
*
1291+
* @return bool
1292+
*/
1293+
public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
1294+
{
1295+
return true;
1296+
}
12871297
}

tests/e2e/Adapter/Scopes/SpatialTests.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,4 +2747,38 @@ public function testInvalidCoordinateDocuments(): void
27472747
$database->deleteCollection($collectionName);
27482748
}
27492749
}
2750+
2751+
public function testCreateSpatialColumnWithExistingData(): void
2752+
{
2753+
/** @var Database $database */
2754+
$database = static::getDatabase();
2755+
if (!$database->getAdapter()->getSupportForSpatialAttributes()) {
2756+
$this->expectNotToPerformAssertions();
2757+
return;
2758+
}
2759+
if ($database->getAdapter()->getSupportForSpatialIndexNull()) {
2760+
$this->expectNotToPerformAssertions();
2761+
return;
2762+
}
2763+
2764+
if ($database->getAdapter()->getSupportForOptionalSpatialAttributeWithExistingRows()) {
2765+
$this->expectNotToPerformAssertions();
2766+
return;
2767+
}
2768+
2769+
$col = 'spatial_col_existing_data';
2770+
try {
2771+
$database->createCollection($col);
2772+
2773+
$database->createAttribute($col, 'name', Database::VAR_STRING, 40, false);
2774+
$database->createDocument($col, new Document(['name' => 'test-doc','$permissions' => [Permission::update(Role::any()), Permission::read(Role::any())]]));
2775+
try {
2776+
$database->createAttribute($col, 'loc', Database::VAR_POINT, 0, true);
2777+
} catch (\Throwable $e) {
2778+
$this->assertInstanceOf(StructureException::class, $e);
2779+
}
2780+
} finally {
2781+
$database->deleteCollection($col);
2782+
}
2783+
}
27502784
}

0 commit comments

Comments
 (0)