|
10 | 10 | use Utopia\Database\Adapter\MySQL; |
11 | 11 | use Utopia\Database\Adapter\Pool; |
12 | 12 | use Utopia\Database\Database; |
| 13 | +use Utopia\Database\Document; |
13 | 14 | use Utopia\Database\Exception; |
14 | 15 | use Utopia\Database\Exception\Duplicate; |
15 | 16 | use Utopia\Database\Exception\Limit; |
| 17 | +use Utopia\Database\Helpers\Permission; |
| 18 | +use Utopia\Database\Helpers\Role; |
16 | 19 | use Utopia\Database\PDO; |
17 | 20 | use Utopia\Pools\Adapter\Stack; |
18 | 21 | use Utopia\Pools\Pool as UtopiaPool; |
@@ -109,4 +112,89 @@ protected function deleteIndex(string $collection, string $index): bool |
109 | 112 |
|
110 | 113 | return true; |
111 | 114 | } |
| 115 | + |
| 116 | + /** |
| 117 | + * Execute raw SQL via the pool using reflection to access the adapter's PDO. |
| 118 | + * |
| 119 | + * @param string $sql |
| 120 | + * @param array<string, mixed> $binds |
| 121 | + */ |
| 122 | + private function execRawSQL(string $sql, array $binds = []): void |
| 123 | + { |
| 124 | + self::$pool->use(function (Adapter $adapter) use ($sql, $binds) { |
| 125 | + $class = new ReflectionClass($adapter); |
| 126 | + $property = $class->getProperty('pdo'); |
| 127 | + $property->setAccessible(true); |
| 128 | + $pdo = $property->getValue($adapter); |
| 129 | + $stmt = $pdo->prepare($sql); |
| 130 | + foreach ($binds as $key => $value) { |
| 131 | + $stmt->bindValue($key, $value); |
| 132 | + } |
| 133 | + $stmt->execute(); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test that orphaned permission records from a previous failed delete |
| 139 | + * don't block document recreation. The createDocument method should |
| 140 | + * clean up orphaned perms and retry. |
| 141 | + */ |
| 142 | + public function testOrphanedPermissionsRecovery(): void |
| 143 | + { |
| 144 | + $database = $this->getDatabase(); |
| 145 | + $collection = 'orphanedPermsRecovery'; |
| 146 | + |
| 147 | + $database->createCollection($collection); |
| 148 | + $database->createAttribute($collection, 'title', Database::VAR_STRING, 128, true); |
| 149 | + |
| 150 | + // Step 1: Create a document with permissions |
| 151 | + $doc = $database->createDocument($collection, new Document([ |
| 152 | + '$id' => 'orphan_test', |
| 153 | + '$permissions' => [ |
| 154 | + Permission::read(Role::any()), |
| 155 | + Permission::update(Role::any()), |
| 156 | + Permission::delete(Role::any()), |
| 157 | + ], |
| 158 | + 'title' => 'Original', |
| 159 | + ])); |
| 160 | + $this->assertEquals('orphan_test', $doc->getId()); |
| 161 | + |
| 162 | + // Step 2: Delete the document normally (cleans up both doc and perms) |
| 163 | + $database->deleteDocument($collection, 'orphan_test'); |
| 164 | + $deleted = $database->getDocument($collection, 'orphan_test'); |
| 165 | + $this->assertTrue($deleted->isEmpty()); |
| 166 | + |
| 167 | + // Step 3: Manually re-insert orphaned permission rows (simulating a partial delete failure) |
| 168 | + $namespace = $this->getDatabase()->getNamespace(); |
| 169 | + $dbName = $this->getDatabase()->getDatabase(); |
| 170 | + $permsTable = "`{$dbName}`.`{$namespace}_{$collection}_perms`"; |
| 171 | + |
| 172 | + $this->execRawSQL( |
| 173 | + "INSERT INTO {$permsTable} (_type, _permission, _document) VALUES (:type, :perm, :doc)", |
| 174 | + [':type' => 'read', ':perm' => 'any', ':doc' => 'orphan_test'] |
| 175 | + ); |
| 176 | + $this->execRawSQL( |
| 177 | + "INSERT INTO {$permsTable} (_type, _permission, _document) VALUES (:type, :perm, :doc)", |
| 178 | + [':type' => 'update', ':perm' => 'any', ':doc' => 'orphan_test'] |
| 179 | + ); |
| 180 | + |
| 181 | + // Step 4: Recreate a document with the same ID - should succeed by cleaning up orphaned perms |
| 182 | + $newDoc = $database->createDocument($collection, new Document([ |
| 183 | + '$id' => 'orphan_test', |
| 184 | + '$permissions' => [ |
| 185 | + Permission::read(Role::any()), |
| 186 | + Permission::update(Role::any()), |
| 187 | + ], |
| 188 | + 'title' => 'Recreated', |
| 189 | + ])); |
| 190 | + $this->assertEquals('orphan_test', $newDoc->getId()); |
| 191 | + $this->assertEquals('Recreated', $newDoc->getAttribute('title')); |
| 192 | + |
| 193 | + // Verify the document can be fetched |
| 194 | + $found = $database->getDocument($collection, 'orphan_test'); |
| 195 | + $this->assertFalse($found->isEmpty()); |
| 196 | + $this->assertEquals('Recreated', $found->getAttribute('title')); |
| 197 | + |
| 198 | + $database->deleteCollection($collection); |
| 199 | + } |
112 | 200 | } |
0 commit comments