| 207 | } |
| 208 | |
| 209 | void DatabaseManager::dropGraph(const std::string& graphName, main::ClientContext* clientContext) { |
| 210 | if (StringUtils::caseInsensitiveEquals(graphName, "main")) { |
| 211 | throw BinderException{"Cannot drop the main graph."}; |
| 212 | } |
| 213 | |
| 214 | auto upperCaseName = StringUtils::getUpper(graphName); |
| 215 | |
| 216 | // Check if graph exists in system catalog first |
| 217 | auto mainCatalog = clientContext->getDatabase()->getCatalog(); |
| 218 | auto transaction = TransactionContext::Get(*clientContext)->getActiveTransaction(); |
| 219 | if (!mainCatalog->containsGraph(transaction, graphName)) { |
| 220 | throw RuntimeException{std::format("No graph named {}.", graphName)}; |
| 221 | } |
| 222 | |
| 223 | // Remove from system catalog |
| 224 | mainCatalog->dropGraph(transaction, graphName); |
| 225 | |
| 226 | for (auto it = graphs.begin(); it != graphs.end(); ++it) { |
| 227 | auto graphNameUpper = StringUtils::getUpper((*it)->getCatalogName()); |
| 228 | if (graphNameUpper == upperCaseName) { |
| 229 | if (defaultGraph != "" && StringUtils::getUpper(defaultGraph) == upperCaseName) { |
| 230 | defaultGraph = ""; |
| 231 | } |
| 232 | auto storageManager = (*it)->getStorageManager(); |
| 233 | std::string graphPath; |
| 234 | if (storageManager != nullptr) { |
| 235 | graphPath = storageManager->getDatabasePath(); |
| 236 | storageManager->closeFileHandle(); |
| 237 | } |
| 238 | if (hasAttachedDatabase(graphName)) { |
| 239 | detachDatabase(graphName); |
| 240 | } |
| 241 | graphs.erase(it); |
| 242 | |
| 243 | // Delete the physical graph files |
| 244 | if (!graphPath.empty() && |
| 245 | !DBConfig::isDBPathInMemory(clientContext->getDatabasePath())) { |
| 246 | auto vfs = common::VirtualFileSystem::GetUnsafe(*clientContext); |
| 247 | vfs->removeFileIfExists(graphPath, clientContext); |
| 248 | vfs->removeFileIfExists(storage::StorageUtils::getWALFilePath(graphPath), |
| 249 | clientContext); |
| 250 | vfs->removeFileIfExists(storage::StorageUtils::getShadowFilePath(graphPath), |
| 251 | clientContext); |
| 252 | vfs->removeFileIfExists(storage::StorageUtils::getTmpFilePath(graphPath), |
| 253 | clientContext); |
| 254 | } |
| 255 | |
| 256 | auto dbStorageManager = clientContext->getDatabase()->getStorageManager(); |
| 257 | auto databaseHeader = dbStorageManager->getOrInitDatabaseHeader(*clientContext); |
| 258 | auto newHeader = std::make_unique<storage::DatabaseHeader>(*databaseHeader); |
| 259 | newHeader->catalogPageRange.startPageIdx = common::INVALID_PAGE_IDX; |
| 260 | newHeader->catalogPageRange.numPages = 0; |
| 261 | newHeader->metadataPageRange.startPageIdx = common::INVALID_PAGE_IDX; |
| 262 | newHeader->metadataPageRange.numPages = 0; |
| 263 | dbStorageManager->setDatabaseHeader(std::move(newHeader)); |
| 264 | return; |
| 265 | } |
| 266 | } |
nothing calls this directly
no test coverage detected