From bbfbf23a20b57080b27950accf0b53606ed92314 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 5 Nov 2024 12:21:15 +0900 Subject: [PATCH 1/6] Don't throw on deleteAttribute if column doesn't exist --- src/Database/Adapter/MariaDB.php | 10 +++++++++- src/Database/Adapter/Postgres.php | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 0e72eee4e..40984e012 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -419,9 +419,17 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa $sql = $this->trigger(Database::EVENT_ATTRIBUTE_DELETE, $sql); - return $this->getPDO() + 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 983e112dd..14986dfba 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -346,9 +346,17 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa $sql = $this->trigger(Database::EVENT_ATTRIBUTE_DELETE, $sql); - return $this->getPDO() + try { + return $this->getPDO() ->prepare($sql) ->execute(); + } catch (PDOException $e) { + if ($e->getCode() === "4242703000" && $e->errorInfo[1] === 7) { + return true; + } + + throw $e; + } } /** From e014f9f6ed0dc56498a477abf42433ec47cf611f Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 7 Nov 2024 15:46:22 +0900 Subject: [PATCH 2/6] Add tests, fix formatting and postgres error code --- src/Database/Adapter/MariaDB.php | 6 +++--- src/Database/Adapter/Postgres.php | 6 +++--- src/Database/Adapter/SQLite.php | 14 +++++++++++--- tests/e2e/Adapter/Base.php | 13 +++++++++++++ tests/e2e/Adapter/MariaDBTest.php | 12 ++++++++++++ tests/e2e/Adapter/MongoDBTest.php | 5 +++++ tests/e2e/Adapter/MySQLTest.php | 12 ++++++++++++ tests/e2e/Adapter/PostgresTest.php | 12 ++++++++++++ tests/e2e/Adapter/SQLiteTest.php | 12 ++++++++++++ tests/e2e/Adapter/SharedTables/MariaDBTest.php | 12 ++++++++++++ tests/e2e/Adapter/SharedTables/MongoDBTest.php | 5 +++++ tests/e2e/Adapter/SharedTables/MySQLTest.php | 12 ++++++++++++ tests/e2e/Adapter/SharedTables/PostgresTest.php | 12 ++++++++++++ tests/e2e/Adapter/SharedTables/SQLiteTest.php | 12 ++++++++++++ 14 files changed, 136 insertions(+), 9 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 40984e012..d110b54dc 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -421,8 +421,8 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa try { return $this->getPDO() - ->prepare($sql) - ->execute(); + ->prepare($sql) + ->execute(); } catch (PDOException $e) { if ($e->getCode() === "42000" && $e->errorInfo[1] === 1091) { return true; @@ -435,7 +435,7 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa /** * Rename Attribute * - * @param string $collection + * @param string $collectionp * @param string $old * @param string $new * @return bool diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 14986dfba..c60710d24 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -348,10 +348,10 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa try { return $this->getPDO() - ->prepare($sql) - ->execute(); + ->prepare($sql) + ->execute(); } catch (PDOException $e) { - if ($e->getCode() === "4242703000" && $e->errorInfo[1] === 7) { + if ($e->getCode() === "42703" && $e->errorInfo[1] === 7) { return true; } diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 5d1945598..70b28077f 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -341,9 +341,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 6fde24f1f..105827870 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 */ @@ -1295,6 +1303,11 @@ 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)); + $this->assertEquals(true, static::deleteColumn('attributes', 'string1')); + $this->assertEquals(true, static::getDatabase()->deleteAttribute('attributes', 'string1')); } /** diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index d7966e657..ca5b1988a 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/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..467ec820d 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..6834f9297 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..fefc8e504 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..3cd63e0a7 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..9d3f77c5c 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..9d108d08a 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..5ab52a281 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; + } } From f9321e73637bc1ab270cc4476e82257731d813fa Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Thu, 7 Nov 2024 16:07:16 +0900 Subject: [PATCH 3/6] Format and fix PHPStan --- src/Database/Adapter/MariaDB.php | 2 +- tests/e2e/Adapter/Base.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index d110b54dc..5271a02c5 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -435,7 +435,7 @@ public function deleteAttribute(string $collection, string $id, bool $array = fa /** * Rename Attribute * - * @param string $collectionp + * @param string $collection * @param string $old * @param string $new * @return bool diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 105827870..b9dbb796a 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -44,7 +44,7 @@ abstract protected static function getDatabase(): Database; /** * @param string $collection * @param string $column - * + * * @return bool */ abstract protected static function deleteColumn(string $collection, string $column): bool; From 529ac35d537e6cc2da2abfa27fd6fe3668934e04 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 8 Nov 2024 13:43:59 +0900 Subject: [PATCH 4/6] Address Comments --- tests/e2e/Adapter/Base.php | 15 +++++++++++++++ tests/e2e/Adapter/MariaDBTest.php | 4 ++-- tests/e2e/Adapter/MySQLTest.php | 4 ++-- tests/e2e/Adapter/PostgresTest.php | 4 ++-- tests/e2e/Adapter/SQLiteTest.php | 4 ++-- tests/e2e/Adapter/SharedTables/MariaDBTest.php | 4 ++-- tests/e2e/Adapter/SharedTables/MySQLTest.php | 4 ++-- tests/e2e/Adapter/SharedTables/PostgresTest.php | 4 ++-- tests/e2e/Adapter/SharedTables/SQLiteTest.php | 4 ++-- 9 files changed, 31 insertions(+), 16 deletions(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index b9dbb796a..a7e68e3d3 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1306,8 +1306,23 @@ public function testCreateDeleteAttribute(): void // 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 ca5b1988a..10a217d4f 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -63,8 +63,8 @@ public static function getDatabase(bool $fresh = false): 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}`"; + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 467ec820d..1cd3eb2e8 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -65,8 +65,8 @@ public static function getDatabase(): 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}`"; + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index 6834f9297..ebf2cbc11 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -62,8 +62,8 @@ public static function getDatabase(): 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}\""; + $sqlTable = '"' . self::getDatabase()->getDatabase() . '"."' . self::getDatabase()->getNamespace() . '_' . $collection . '"'; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\""; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SQLiteTest.php b/tests/e2e/Adapter/SQLiteTest.php index fefc8e504..0718d5522 100644 --- a/tests/e2e/Adapter/SQLiteTest.php +++ b/tests/e2e/Adapter/SQLiteTest.php @@ -68,8 +68,8 @@ public static function getDatabase(): Database protected static function deleteColumn(string $collection, string $column): bool { - $SQLTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; - $sql = "ALTER TABLE {$SQLTable} DROP COLUMN `{$column}`"; + $sqlTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index 3cd63e0a7..f6d58a30f 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -66,8 +66,8 @@ public static function getDatabase(bool $fresh = false): 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}`"; + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index 9d3f77c5c..4cd682319 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -68,8 +68,8 @@ public static function getDatabase(): 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}`"; + $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index 9d108d08a..23c659958 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -65,8 +65,8 @@ public static function getDatabase(): 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}\""; + $sqlTable = '"' . self::getDatabase()->getDatabase() . '"."' . self::getDatabase()->getNamespace() . '_' . $collection . '"'; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\""; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/SQLiteTest.php b/tests/e2e/Adapter/SharedTables/SQLiteTest.php index 5ab52a281..7cea4fb61 100644 --- a/tests/e2e/Adapter/SharedTables/SQLiteTest.php +++ b/tests/e2e/Adapter/SharedTables/SQLiteTest.php @@ -71,8 +71,8 @@ public static function getDatabase(): Database protected static function deleteColumn(string $collection, string $column): bool { - $SQLTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; - $sql = "ALTER TABLE {$SQLTable} DROP COLUMN `{$column}`"; + $sqlTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); From 10ce467e452bb2426e5bb78744f98a509c98ca05 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 8 Nov 2024 14:28:16 +0900 Subject: [PATCH 5/6] Add missing method in mirror --- tests/e2e/Adapter/MirrorTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/e2e/Adapter/MirrorTest.php b/tests/e2e/Adapter/MirrorTest.php index ee34386f9..40417ab14 100644 --- a/tests/e2e/Adapter/MirrorTest.php +++ b/tests/e2e/Adapter/MirrorTest.php @@ -22,6 +22,7 @@ class MirrorTest extends Base { protected static ?Mirror $database = null; + protected static ?PDO $pdo = null; protected static Database $source; protected static Database $destination; @@ -95,6 +96,7 @@ protected static function getDatabase(bool $fresh = false): Mirror $database->create(); + self::$pdo = $pdo; return self::$database = $database; } @@ -312,4 +314,14 @@ 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::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; + + self::$pdo->exec($sql); + + return true; + } } From 5c93bd31a021f4154a5275fc90ebbc8dd103782d Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 8 Nov 2024 15:26:52 +0900 Subject: [PATCH 6/6] Address Comments --- tests/e2e/Adapter/MirrorTest.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Adapter/MirrorTest.php b/tests/e2e/Adapter/MirrorTest.php index 40417ab14..ae8bd7185 100644 --- a/tests/e2e/Adapter/MirrorTest.php +++ b/tests/e2e/Adapter/MirrorTest.php @@ -22,7 +22,8 @@ class MirrorTest extends Base { protected static ?Mirror $database = null; - protected static ?PDO $pdo = null; + protected static ?PDO $destinationPdo = null; + protected static ?PDO $sourcePdo = null; protected static Database $source; protected static Database $destination; @@ -49,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'; @@ -62,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); @@ -96,7 +99,6 @@ protected static function getDatabase(bool $fresh = false): Mirror $database->create(); - self::$pdo = $pdo; return self::$database = $database; } @@ -317,10 +319,15 @@ public function testDeleteMirroredDocument(): void protected static function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; - self::$pdo->exec($sql); + 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; }