Skip to content
Open
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
4 changes: 1 addition & 3 deletions src/include/main/database_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ class DatabaseManager {
void loadGraphsFromCatalog(storage::MemoryManager* memoryManager,
main::ClientContext* clientContext);
void setDefaultGraph(const std::string& graphName);
void clearDefaultGraph();
bool hasGraph(const std::string& graphName);
catalog::Catalog* getGraphCatalog(const std::string& graphName);
catalog::Catalog* getDefaultGraphCatalog() const;
bool hasDefaultGraph() const { return defaultGraph != "" && defaultGraph != "main"; }
bool hasDefaultGraph() const { return defaultGraph != ""; }
std::string getDefaultGraphName() const { return defaultGraph; }
std::vector<catalog::Catalog*> getGraphs() const;
storage::StorageManager* getDefaultGraphStorageManager() const;
Expand Down
34 changes: 9 additions & 25 deletions src/main/database_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ void DatabaseManager::createGraph(const std::string& graphName,
storage::MemoryManager* memoryManager, main::ClientContext* clientContext, bool isAnyGraph) {
auto upperCaseName = StringUtils::getUpper(graphName);

// main is the default graph
if (upperCaseName == "MAIN") {
throw RuntimeException{"MAIN is a reserved graph name."};
}

// Check if graph already exists in system catalog
auto mainCatalog = clientContext->getDatabase()->getCatalog();
auto transaction = TransactionContext::Get(*clientContext)->getActiveTransaction();
Expand Down Expand Up @@ -196,9 +201,6 @@ void DatabaseManager::createGraph(const std::string& graphName,
}

graphs.push_back(std::move(catalog));
if (defaultGraph == "") {
defaultGraph = graphName;
}
}

void DatabaseManager::dropGraph(const std::string& graphName, main::ClientContext* clientContext) {
Expand Down Expand Up @@ -259,10 +261,12 @@ void DatabaseManager::dropGraph(const std::string& graphName, main::ClientContex

void DatabaseManager::setDefaultGraph(const std::string& graphName) {
auto upperCaseName = StringUtils::getUpper(graphName);

if (upperCaseName == "MAIN") {
defaultGraph = "main";
defaultGraph = "";
return;
}

for (auto& graph : graphs) {
auto graphNameUpper = StringUtils::getUpper(graph->getCatalogName());
if (graphNameUpper == upperCaseName) {
Expand All @@ -273,10 +277,6 @@ void DatabaseManager::setDefaultGraph(const std::string& graphName) {
throw BinderException{std::format("No graph named {}.", graphName)};
}

void DatabaseManager::clearDefaultGraph() {
defaultGraph = "main";
}

void DatabaseManager::loadGraphsFromCatalog(storage::MemoryManager* memoryManager,
main::ClientContext* clientContext) {
auto mainCatalog = clientContext->getDatabase()->getCatalog();
Expand Down Expand Up @@ -322,11 +322,6 @@ void DatabaseManager::loadGraphsFromCatalog(storage::MemoryManager* memoryManage
catalog->setStorageManager(std::move(storageManager));

graphs.push_back(std::move(catalog));

// Set first loaded graph as default if no default set
if (defaultGraph == "") {
defaultGraph = graphName;
}
}
}

Expand All @@ -341,19 +336,8 @@ bool DatabaseManager::hasGraph(const std::string& graphName) {
return false;
}

catalog::Catalog* DatabaseManager::getGraphCatalog(const std::string& graphName) {
auto upperCaseName = StringUtils::getUpper(graphName);
for (auto& graph : graphs) {
auto graphNameUpper = StringUtils::getUpper(graph->getCatalogName());
if (graphNameUpper == upperCaseName) {
return graph.get();
}
}
throw BinderException{std::format("No graph named {}.", graphName)};
}

catalog::Catalog* DatabaseManager::getDefaultGraphCatalog() const {
if (defaultGraph == "" || defaultGraph == "main") {
if (defaultGraph == "") {
return nullptr;
}
auto upperCaseName = StringUtils::getUpper(defaultGraph);
Expand Down
10 changes: 5 additions & 5 deletions src/processor/operator/ddl/drop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ void Drop::dropGraph(const main::ClientContext* context) {
auto dbManager = main::DatabaseManager::Get(*context);
auto memoryManager = storage::MemoryManager::Get(*context);

// Protect the main graph unconditionally — IF EXISTS cannot bypass this.
if (StringUtils::getUpper(dropInfo.name) == "MAIN") {
throw BinderException("Cannot drop the main graph.");
}

if (!dbManager->hasGraph(dropInfo.name)) {
auto message = std::format("Graph {} does not exist.", dropInfo.name);
switch (dropInfo.conflictAction) {
Expand All @@ -139,11 +144,6 @@ void Drop::dropGraph(const main::ClientContext* context) {
}
}

if (dbManager->hasDefaultGraph() && StringUtils::getUpper(dbManager->getDefaultGraphName()) ==
StringUtils::getUpper(dropInfo.name)) {
dbManager->clearDefaultGraph();
}

dbManager->dropGraph(dropInfo.name, const_cast<main::ClientContext*>(context));
appendMessage(std::format("Graph {} has been dropped.", dropInfo.name), memoryManager);
}
Expand Down
17 changes: 17 additions & 0 deletions test/test_files/graph/any_graph.test
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ _nodes

-STATEMENT DROP GRAPH mygraph
---- ok

-CASE AnyGraphNamedMain

-LOG CreateAnyGraphNamedMain
-STATEMENT CREATE GRAPH main ANY
---- error
Runtime exception: MAIN is a reserved graph name.

-LOG CreateAnyGraphNamedMainUpperCase
-STATEMENT CREATE GRAPH MAIN ANY
---- error
Runtime exception: MAIN is a reserved graph name.

-LOG CreateAnyGraphNamedMainMixedCase
-STATEMENT CREATE GRAPH Main ANY
---- error
Runtime exception: MAIN is a reserved graph name.
45 changes: 45 additions & 0 deletions test/test_files/graph/graph.test
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,51 @@ person|foo1(graph)
-STATEMENT DROP GRAPH foo1
---- ok

-CASE MainGraphProtection

-LOG CreateGraphNamedMain
-STATEMENT CREATE GRAPH main
---- error
Runtime exception: MAIN is a reserved graph name.

-LOG CreateGraphNamedMainUpperCase
-STATEMENT CREATE GRAPH MAIN
---- error
Runtime exception: MAIN is a reserved graph name.

-LOG CreateGraphNamedMainMixedCase
-STATEMENT CREATE GRAPH Main
---- error
Runtime exception: MAIN is a reserved graph name.

-LOG DropGraphMain
-STATEMENT DROP GRAPH main
---- error
Binder exception: Cannot drop the main graph.

-LOG DropGraphMainUpperCase
-STATEMENT DROP GRAPH MAIN
---- error
Binder exception: Cannot drop the main graph.

-LOG DropGraphMainMixedCase
-STATEMENT DROP GRAPH Main
---- error
Binder exception: Cannot drop the main graph.

-LOG DropGraphIfExistsMain
-STATEMENT DROP GRAPH IF EXISTS main
---- error
Binder exception: Cannot drop the main graph.

-LOG UseGraphMain
-STATEMENT USE GRAPH main
---- ok

-LOG UseGraphMainUpperCase
-STATEMENT USE GRAPH MAIN
---- ok

-CASE ErrorCases
-LOG DuplicateGraphError
-STATEMENT CREATE GRAPH graph1_ec
Expand Down
Loading