Skip to content

Commit

Permalink
special treatment for :memory: dbdir argument
Browse files Browse the repository at this point in the history
  • Loading branch information
tdoehmen committed Feb 26, 2020
1 parent c26b7c0 commit 58f8022
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/database.cpp
Expand Up @@ -24,6 +24,11 @@ DuckDB::DuckDB(const char *path, DBConfig *config) {
if (temporary_directory.empty() && path) {
// no directory specified: use default temp path
temporary_directory = string(path) + ".tmp";

// special treatment for in-memory mode
if (strcmp(path, ":memory:") == 0) {
temporary_directory = ".tmp";
}
}
if (config && !config->use_temporary_directory) {
// temporary directories explicitly disabled
Expand Down
6 changes: 4 additions & 2 deletions test/api/CMakeLists.txt
Expand Up @@ -7,7 +7,8 @@ if(NOT WIN32)
test_prepared_api.cpp
test_table_info.cpp
test_appender_api.cpp
test_query_profiler.cpp)
test_query_profiler.cpp
test_dbdir.cpp)
else()
add_library_unity(test_api
OBJECT
Expand All @@ -16,7 +17,8 @@ else()
test_prepared_api.cpp
test_table_info.cpp
test_appender_api.cpp
test_query_profiler.cpp)
test_query_profiler.cpp
test_dbdir.cpp)
endif()

set(ALL_OBJECT_FILES
Expand Down
44 changes: 44 additions & 0 deletions test/api/test_dbdir.cpp
@@ -0,0 +1,44 @@
#include "catch.hpp"
#include "duckdb/common/file_system.hpp"
#include "duckdb/storage/storage_manager.hpp"
#include "test_helpers.hpp"

using namespace duckdb;
using namespace std;

static void test_in_memory_initialization(string dbdir) {
FileSystem fs;
unique_ptr<DuckDB> db;
string in_memory_tmp = ".tmp";

// make sure the temporary folder does not exist
DeleteDatabase(dbdir);
fs.RemoveDirectory(in_memory_tmp);

// cannot create an in-memory database using ":memory:" argument
REQUIRE_NOTHROW(db = make_unique<DuckDB>(dbdir));

// WAL is not null, which indicates that data is being persisted somewhere
REQUIRE(db->storage->GetWriteAheadLog() == 0);

// the temporary folder .tmp should be created in in-memory mode, but was not
REQUIRE(fs.DirectoryExists(in_memory_tmp));

// the database dir should not be created in in-memory mode, but was
REQUIRE(!fs.DirectoryExists(dbdir));

// clean up
db.reset();

// make sure to clean up the database & temporary folder
DeleteDatabase(dbdir);
fs.RemoveDirectory(in_memory_tmp);
}

TEST_CASE("Test in-memory database initialization argument \":memory:\"", "[api]") {
test_in_memory_initialization(":memory:");
}

TEST_CASE("Test in-memory database initialization argument \"\"", "[api]") {
test_in_memory_initialization("");
}

0 comments on commit 58f8022

Please sign in to comment.