Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 12 additions & 5 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,16 @@ public function deleteRelationship(

switch ($type) {
case Database::RELATION_ONE_TO_ONE:
$sql = "ALTER TABLE {$table} DROP COLUMN `{$key}`;";
if ($twoWay) {
$sql .= "ALTER TABLE {$relatedTable} DROP COLUMN `{$twoWayKey}`;";
if ($side === Database::RELATION_SIDE_PARENT) {
$sql = "ALTER TABLE {$table} DROP COLUMN `{$key}`;";
if ($twoWay) {
$sql .= "ALTER TABLE {$relatedTable} DROP COLUMN `{$twoWayKey}`;";
}
} elseif ($side === Database::RELATION_SIDE_CHILD) {
$sql = "ALTER TABLE {$relatedTable} DROP COLUMN `{$twoWayKey}`;";
if ($twoWay) {
$sql .= "ALTER TABLE {$table} DROP COLUMN `{$key}`;";
}
}
break;
case Database::RELATION_ONE_TO_MANY:
Expand All @@ -545,9 +552,9 @@ public function deleteRelationship(
}
break;
case Database::RELATION_MANY_TO_ONE:
if ($twoWay && $side === Database::RELATION_SIDE_CHILD) {
if ($side === Database::RELATION_SIDE_CHILD) {
$sql = "ALTER TABLE {$relatedTable} DROP COLUMN `{$twoWayKey}`;";
} else {
} elseif ($twoWay) {
$sql = "ALTER TABLE {$table} DROP COLUMN `{$key}`;";
}
break;
Expand Down
27 changes: 20 additions & 7 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,25 +525,34 @@ public function deleteRelationship(
$relatedTable = $this->getSQLTable($relatedName);
$key = $this->filter($key);

$sql = '';

switch ($type) {
case Database::RELATION_ONE_TO_ONE:
$sql = "ALTER TABLE {$table} DROP COLUMN \"{$key}\";";
if ($twoWay) {
$sql .= "ALTER TABLE {$relatedTable} DROP COLUMN \"{$twoWayKey}\";";
if ($side === Database::RELATION_SIDE_PARENT) {
$sql = "ALTER TABLE {$table} DROP COLUMN \"{$key}\";";
if ($twoWay) {
$sql .= "ALTER TABLE {$relatedTable} DROP COLUMN \"{$twoWayKey}\";";
}
} elseif ($side === Database::RELATION_SIDE_CHILD) {
$sql = "ALTER TABLE {$relatedTable} DROP COLUMN \"{$twoWayKey}\";";
if ($twoWay) {
$sql .= "ALTER TABLE {$table} DROP COLUMN \"{$key}\";";
}
}
break;
case Database::RELATION_ONE_TO_MANY:
if ($side === Database::RELATION_SIDE_PARENT) {
$sql = "ALTER TABLE {$relatedTable} DROP COLUMN \"{$twoWayKey}\";";
} else {
} elseif ($twoWay) {
$sql = "ALTER TABLE {$table} DROP COLUMN \"{$key}\";";
}
break;
case Database::RELATION_MANY_TO_ONE:
if ($side === Database::RELATION_SIDE_PARENT) {
$sql = "ALTER TABLE {$table} DROP COLUMN \"{$key}\";";
} else {
if ($side === Database::RELATION_SIDE_CHILD) {
$sql = "ALTER TABLE {$relatedTable} DROP COLUMN \"{$twoWayKey}\";";
} elseif ($twoWay) {
$sql = "ALTER TABLE {$table} DROP COLUMN \"{$key}\";";
}
break;
case Database::RELATION_MANY_TO_MANY:
Expand All @@ -564,6 +573,10 @@ public function deleteRelationship(
throw new DatabaseException('Invalid relationship type');
}

if (empty($sql)) {
return true;
}

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

return $this->getPDO()
Expand Down
16 changes: 12 additions & 4 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2186,9 +2186,17 @@ public function deleteRelationship(string $collection, string $id): bool

switch ($type) {
case self::RELATION_ONE_TO_ONE:
$this->deleteIndex($collection->getId(), $indexKey);
if ($twoWay) {
if ($side === Database::RELATION_SIDE_PARENT) {
$this->deleteIndex($collection->getId(), $indexKey);
if ($twoWay) {
$this->deleteIndex($relatedCollection->getId(), $twoWayIndexKey);
}
}
if ($side === Database::RELATION_SIDE_CHILD) {
$this->deleteIndex($relatedCollection->getId(), $twoWayIndexKey);
if ($twoWay) {
$this->deleteIndex($collection->getId(), $indexKey);
}
}
break;
case self::RELATION_ONE_TO_MANY:
Expand All @@ -2199,9 +2207,9 @@ public function deleteRelationship(string $collection, string $id): bool
}
break;
case self::RELATION_MANY_TO_ONE:
if ($twoWay && $side === Database::RELATION_SIDE_CHILD) {
if ($side === Database::RELATION_SIDE_CHILD) {
$this->deleteIndex($relatedCollection->getId(), $twoWayIndexKey);
} else {
} elseif ($twoWay) {
$this->deleteIndex($collection->getId(), $indexKey);
}
break;
Expand Down
163 changes: 163 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,169 @@ public function testCreateExistsDelete(): void
$this->assertEquals(true, static::getDatabase()->create());
}

public function testDeleteRelatedCollection(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
$this->expectNotToPerformAssertions();
return;
}

static::getDatabase()->createCollection('c1');
static::getDatabase()->createCollection('c2');

// ONE_TO_ONE
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_ONE,
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c1'));
$collection = static::getDatabase()->getCollection('c2');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c1');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_ONE,
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c2'));
$collection = static::getDatabase()->getCollection('c1');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c2');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_ONE,
twoWay: true
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c1'));
$collection = static::getDatabase()->getCollection('c2');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c1');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_ONE,
twoWay: true
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c2'));
$collection = static::getDatabase()->getCollection('c1');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

// ONE_TO_MANY
static::getDatabase()->createCollection('c2');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_MANY,
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c1'));
$collection = static::getDatabase()->getCollection('c2');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c1');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_MANY,
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c2'));
$collection = static::getDatabase()->getCollection('c1');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c2');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_MANY,
twoWay: true
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c1'));
$collection = static::getDatabase()->getCollection('c2');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c1');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_ONE_TO_MANY,
twoWay: true
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c2'));
$collection = static::getDatabase()->getCollection('c1');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

// RELATION_MANY_TO_ONE
static::getDatabase()->createCollection('c2');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_MANY_TO_ONE,
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c1'));
$collection = static::getDatabase()->getCollection('c2');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c1');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_MANY_TO_ONE,
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c2'));
$collection = static::getDatabase()->getCollection('c1');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c2');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_MANY_TO_ONE,
twoWay: true
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c1'));
$collection = static::getDatabase()->getCollection('c2');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));

static::getDatabase()->createCollection('c1');
static::getDatabase()->createRelationship(
collection: 'c1',
relatedCollection: 'c2',
type: Database::RELATION_MANY_TO_ONE,
twoWay: true
);

$this->assertEquals(true, static::getDatabase()->deleteCollection('c2'));
$collection = static::getDatabase()->getCollection('c1');
$this->assertCount(0, $collection->getAttribute('attributes'));
$this->assertCount(0, $collection->getAttribute('indexes'));
}

/**
* @throws Exception|Throwable
*/
Expand Down