diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 465f57e3e..8bd731594 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -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; + } } /** diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 67067c0a6..116807a67 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -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; + } } /** diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index e03b5e794..90008bb09 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -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; + } } /** diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index b794aec37..a94c63dcc 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -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 */ @@ -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'); } /** diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index d7966e657..10a217d4f 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -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 @@ -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; + } } diff --git a/tests/e2e/Adapter/MirrorTest.php b/tests/e2e/Adapter/MirrorTest.php index ee34386f9..ae8bd7185 100644 --- a/tests/e2e/Adapter/MirrorTest.php +++ b/tests/e2e/Adapter/MirrorTest.php @@ -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; @@ -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'; @@ -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); @@ -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; + } } diff --git a/tests/e2e/Adapter/MongoDBTest.php b/tests/e2e/Adapter/MongoDBTest.php index 3f3b5d5de..9bd560a8f 100644 --- a/tests/e2e/Adapter/MongoDBTest.php +++ b/tests/e2e/Adapter/MongoDBTest.php @@ -95,4 +95,9 @@ public function testKeywords(): void { $this->assertTrue(true); } + + protected static function deleteColumn(string $collection, string $column): bool + { + return true; + } } diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 8d855d6ca..1cd3eb2e8 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -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 @@ -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; + } } diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index e024e4442..ebf2cbc11 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -12,6 +12,7 @@ class PostgresTest extends Base { public static ?Database $database = null; + protected static ?PDO $pdo = null; protected static string $namespace; /** @@ -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; + } } diff --git a/tests/e2e/Adapter/SQLiteTest.php b/tests/e2e/Adapter/SQLiteTest.php index 2d4240994..0718d5522 100644 --- a/tests/e2e/Adapter/SQLiteTest.php +++ b/tests/e2e/Adapter/SQLiteTest.php @@ -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 @@ -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; + } } diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index 249e13319..f6d58a30f 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -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 @@ -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; + } } diff --git a/tests/e2e/Adapter/SharedTables/MongoDBTest.php b/tests/e2e/Adapter/SharedTables/MongoDBTest.php index 47f480073..51027b88f 100644 --- a/tests/e2e/Adapter/SharedTables/MongoDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MongoDBTest.php @@ -98,4 +98,9 @@ public function testKeywords(): void { $this->assertTrue(true); } + + protected static function deleteColumn(string $collection, string $column): bool + { + return true; + } } diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index 689ea49c7..4cd682319 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -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 @@ -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; + } } diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index df85dfeb0..23c659958 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -13,6 +13,7 @@ class PostgresTest extends Base { public static ?Database $database = null; + public static ?PDO $pdo = null; protected static string $namespace; /** @@ -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; + } } diff --git a/tests/e2e/Adapter/SharedTables/SQLiteTest.php b/tests/e2e/Adapter/SharedTables/SQLiteTest.php index a69984216..7cea4fb61 100644 --- a/tests/e2e/Adapter/SharedTables/SQLiteTest.php +++ b/tests/e2e/Adapter/SharedTables/SQLiteTest.php @@ -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 @@ -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; + } }