Skip to content

Commit

Permalink
Merge pull request #470 from utopia-php/0.53.x-success-on-missing-att…
Browse files Browse the repository at this point in the history
…ribute-delete

Don't throw on deleteAttribute if column doesn't exist
  • Loading branch information
abnegate authored Nov 8, 2024
2 parents 895176b + 5c93bd3 commit 4895188
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 9 deletions.
14 changes: 11 additions & 3 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,17 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa

$sql = $this->trigger(Database::EVENT_ATTRIBUTE_DELETE, $sql);

return $this->getPDO()
->prepare($sql)
->execute();
try {
return $this->getPDO()
->prepare($sql)
->execute();
} catch (PDOException $e) {
if ($e->getCode() === "42000" && $e->errorInfo[1] === 1091) {
return true;
}

throw $e;
}
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,17 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa

$sql = $this->trigger(Database::EVENT_ATTRIBUTE_DELETE, $sql);

return $this->getPDO()
->prepare($sql)
->execute();
try {
return $this->getPDO()
->prepare($sql)
->execute();
} catch (PDOException $e) {
if ($e->getCode() === "42703" && $e->errorInfo[1] === 7) {
return true;
}

throw $e;
}
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,17 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa

$sql = $this->trigger(Database::EVENT_COLLECTION_DELETE, $sql);

return $this->getPDO()
->prepare($sql)
->execute();
try {
return $this->getPDO()
->prepare($sql)
->execute();
} catch (PDOException $e) {
if (str_contains($e->getMessage(), 'no such column')) {
return true;
}

throw $e;
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ abstract class Base extends TestCase
*/
abstract protected static function getDatabase(): Database;

/**
* @param string $collection
* @param string $column
*
* @return bool
*/
abstract protected static function deleteColumn(string $collection, string $column): bool;

/**
* @return string
*/
Expand Down Expand Up @@ -1341,6 +1349,26 @@ public function testCreateDeleteAttribute(): void
} catch (Exception $e) {
$this->assertInstanceOf(DuplicateException::class, $e);
}

// Test delete attribute when column does not exist
$this->assertEquals(true, static::getDatabase()->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true));
sleep(1);

$this->assertEquals(true, static::deleteColumn('attributes', 'string1'));

$collection = static::getDatabase()->getCollection('attributes');
$attributes = $collection->getAttribute('attributes');
$attribute = end($attributes);
$this->assertEquals('string1', $attribute->getId());

$this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string1'));

$collection = static::getDatabase()->getCollection('attributes');
$attributes = $collection->getAttribute('attributes');
$attribute = end($attributes);
$this->assertNotEquals('string1', $attribute->getId());

$collection = static::getDatabase()->getCollection('attributes');
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class MariaDBTest extends Base
{
protected static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;

// Remove once all methods are implemented
Expand Down Expand Up @@ -56,6 +57,17 @@ public static function getDatabase(bool $fresh = false): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->exec($sql);

return true;
}
}
19 changes: 19 additions & 0 deletions tests/e2e/Adapter/MirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
class MirrorTest extends Base
{
protected static ?Mirror $database = null;
protected static ?PDO $destinationPdo = null;
protected static ?PDO $sourcePdo = null;
protected static Database $source;
protected static Database $destination;

Expand All @@ -48,6 +50,7 @@ protected static function getDatabase(bool $fresh = false): Mirror
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));

self::$sourcePdo = $pdo;
self::$source = new Database(new MariaDB($pdo), $cache);

$mirrorHost = 'mariadb-mirror';
Expand All @@ -61,6 +64,7 @@ protected static function getDatabase(bool $fresh = false): Mirror
$mirrorRedis->flushAll();
$mirrorCache = new Cache(new RedisAdapter($mirrorRedis));

self::$destinationPdo = $mirrorPdo;
self::$destination = new Database(new MariaDB($mirrorPdo), $mirrorCache);

$database = new Mirror(self::$source, self::$destination);
Expand Down Expand Up @@ -312,4 +316,19 @@ public function testDeleteMirroredDocument(): void
$this->assertTrue($database->getSource()->getDocument('testDeleteMirroredDocument', $document->getId())->isEmpty());
$this->assertTrue($database->getDestination()->getDocument('testDeleteMirroredDocument', $document->getId())->isEmpty());
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$sourcePdo->exec($sql);

$sqlTable = "`" . self::$destination->getDatabase() . "`.`" . self::$destination->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$destinationPdo->exec($sql);

return true;
}
}
5 changes: 5 additions & 0 deletions tests/e2e/Adapter/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public function testKeywords(): void
{
$this->assertTrue(true);
}

protected static function deleteColumn(string $collection, string $column): bool
{
return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class MySQLTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;

// Remove once all methods are implemented
Expand Down Expand Up @@ -58,6 +59,17 @@ public static function getDatabase(): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->exec($sql);

return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class PostgresTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;

/**
Expand Down Expand Up @@ -55,6 +56,17 @@ public static function getDatabase(): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = '"' . self::getDatabase()->getDatabase() . '"."' . self::getDatabase()->getNamespace() . '_' . $collection . '"';
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\"";

self::$pdo->exec($sql);

return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/SQLiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class SQLiteTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;

// Remove once all methods are implemented
Expand Down Expand Up @@ -61,6 +62,17 @@ public static function getDatabase(): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->exec($sql);

return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/SharedTables/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class MariaDBTest extends Base
{
protected static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;

// Remove once all methods are implemented
Expand Down Expand Up @@ -59,6 +60,17 @@ public static function getDatabase(bool $fresh = false): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->exec($sql);

return true;
}
}
5 changes: 5 additions & 0 deletions tests/e2e/Adapter/SharedTables/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public function testKeywords(): void
{
$this->assertTrue(true);
}

protected static function deleteColumn(string $collection, string $column): bool
{
return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/SharedTables/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class MySQLTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;

// Remove once all methods are implemented
Expand Down Expand Up @@ -61,6 +62,17 @@ public static function getDatabase(): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->exec($sql);

return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/SharedTables/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class PostgresTest extends Base
{
public static ?Database $database = null;
public static ?PDO $pdo = null;
protected static string $namespace;

/**
Expand Down Expand Up @@ -58,6 +59,17 @@ public static function getDatabase(): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = '"' . self::getDatabase()->getDatabase() . '"."' . self::getDatabase()->getNamespace() . '_' . $collection . '"';
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\"";

self::$pdo->exec($sql);

return true;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/SharedTables/SQLiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class SQLiteTest extends Base
{
public static ?Database $database = null;
public static ?PDO $pdo = null;
protected static string $namespace;

// Remove once all methods are implemented
Expand Down Expand Up @@ -64,6 +65,17 @@ public static function getDatabase(): Database

$database->create();

self::$pdo = $pdo;
return self::$database = $database;
}

protected static function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->exec($sql);

return true;
}
}

0 comments on commit 4895188

Please sign in to comment.