diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 84889d15d..50672a113 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -832,9 +832,17 @@ public function deleteIndex(string $collection, string $id): bool $sql = $this->trigger(Database::EVENT_INDEX_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/SQLite.php b/src/Database/Adapter/SQLite.php index 90008bb09..005b58564 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -499,9 +499,17 @@ public function deleteIndex(string $collection, string $id): bool $sql = "DROP INDEX `{$this->getNamespace()}_{$this->tenant}_{$name}_{$id}`"; $sql = $this->trigger(Database::EVENT_INDEX_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 index')) { + return true; + } + + throw $e; + } } /** diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index a66d96991..72622dd31 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -49,6 +49,14 @@ abstract protected static function getDatabase(): Database; */ abstract protected static function deleteColumn(string $collection, string $column): bool; + /** + * @param string $collection + * @param string $index + * + * @return bool + */ + abstract protected static function deleteIndex(string $collection, string $index): bool; + /** * @return string */ @@ -1616,6 +1624,16 @@ public function testCreateDeleteIndex(): void $this->assertInstanceOf(DuplicateException::class, $e); } + // Test delete index when index does not exist + $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC])); + $this->assertEquals(true, static::deleteIndex('indexes', 'index1')); + $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index1')); + + // Test delete index when attribute does not exist + $this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC])); + $this->assertEquals(true, static::getDatabase()->deleteAttribute('indexes', 'string')); + $this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index1')); + static::getDatabase()->deleteCollection('indexes'); } diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index 10a217d4f..32d8c0c0c 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -70,4 +70,14 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/MirrorTest.php b/tests/e2e/Adapter/MirrorTest.php index ae8bd7185..a3d457624 100644 --- a/tests/e2e/Adapter/MirrorTest.php +++ b/tests/e2e/Adapter/MirrorTest.php @@ -331,4 +331,19 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`"; + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; + + self::$sourcePdo->exec($sql); + + $sqlTable = "`" . self::$destination->getDatabase() . "`.`" . self::$destination->getNamespace() . "_" . $collection . "`"; + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; + + self::$destinationPdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/MongoDBTest.php b/tests/e2e/Adapter/MongoDBTest.php index 9bd560a8f..e582aef59 100644 --- a/tests/e2e/Adapter/MongoDBTest.php +++ b/tests/e2e/Adapter/MongoDBTest.php @@ -100,4 +100,9 @@ protected static function deleteColumn(string $collection, string $column): bool { return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + return true; + } } diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 1cd3eb2e8..369958f07 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -72,4 +72,14 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index ebf2cbc11..62977c913 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -69,4 +69,15 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $key = "\"".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}\""; + + $sql = "DROP INDEX \"".self::getDatabase()->getDatabase()."\".{$key}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/SQLiteTest.php b/tests/e2e/Adapter/SQLiteTest.php index 0718d5522..87676b7f5 100644 --- a/tests/e2e/Adapter/SQLiteTest.php +++ b/tests/e2e/Adapter/SQLiteTest.php @@ -75,4 +75,14 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $index = "`".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}`"; + $sql = "DROP INDEX {$index}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index f6d58a30f..b747c3244 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -73,4 +73,14 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/SharedTables/MongoDBTest.php b/tests/e2e/Adapter/SharedTables/MongoDBTest.php index 51027b88f..7d41bb711 100644 --- a/tests/e2e/Adapter/SharedTables/MongoDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MongoDBTest.php @@ -103,4 +103,9 @@ protected static function deleteColumn(string $collection, string $column): bool { return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + return true; + } } diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index 4cd682319..e56a2ba51 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -75,4 +75,14 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index 23c659958..8319311d5 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -72,4 +72,15 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $key = "\"".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}\""; + + $sql = "DROP INDEX \"".self::getDatabase()->getDatabase()."\".{$key}"; + + self::$pdo->exec($sql); + + return true; + } } diff --git a/tests/e2e/Adapter/SharedTables/SQLiteTest.php b/tests/e2e/Adapter/SharedTables/SQLiteTest.php index 7cea4fb61..daa39d6be 100644 --- a/tests/e2e/Adapter/SharedTables/SQLiteTest.php +++ b/tests/e2e/Adapter/SharedTables/SQLiteTest.php @@ -78,4 +78,14 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } + + protected static function deleteIndex(string $collection, string $index): bool + { + $index = "`".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}`"; + $sql = "DROP INDEX {$index}"; + + self::$pdo->exec($sql); + + return true; + } }