Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw on deleteAttribute if column doesn't exist #470

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
PineappleIOnic marked this conversation as resolved.
Show resolved Hide resolved

$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;
}
}
12 changes: 12 additions & 0 deletions tests/e2e/Adapter/MirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class MirrorTest extends Base
{
protected static ?Mirror $database = null;
protected static ?PDO $pdo = null;
PineappleIOnic marked this conversation as resolved.
Show resolved Hide resolved
protected static Database $source;
protected static Database $destination;

Expand Down Expand Up @@ -95,6 +96,7 @@ protected static function getDatabase(bool $fresh = false): Mirror

$database->create();

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

Expand Down Expand Up @@ -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 . "`";
PineappleIOnic marked this conversation as resolved.
Show resolved Hide resolved
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";

self::$pdo->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;
}
}
Loading