5252#include < boost/program_options/options_description.hpp>
5353#include < base/argsToConfig.h>
5454#include < filesystem>
55+ #include < Common/filesystemHelpers.h>
5556
5657#include " config.h"
5758
@@ -280,7 +281,19 @@ static DatabasePtr createMemoryDatabaseIfNotExists(ContextPtr context, const Str
280281static DatabasePtr createClickHouseLocalDatabaseOverlay (const String & name_, ContextPtr context)
281282{
282283 auto overlay = std::make_shared<DatabasesOverlay>(name_, context);
283- overlay->registerNextDatabase (std::make_shared<DatabaseAtomic>(name_, fs::weakly_canonical (context->getPath ()), UUIDHelpers::generateV4 (), context));
284+
285+ UUID default_database_uuid;
286+
287+ fs::path existing_path_symlink = fs::weakly_canonical (context->getPath ()) / " metadata" / " default" ;
288+ if (FS::isSymlinkNoThrow (existing_path_symlink))
289+ default_database_uuid = parse<UUID>(FS::readSymlink (existing_path_symlink).filename ());
290+ else
291+ default_database_uuid = UUIDHelpers::generateV4 ();
292+
293+ fs::path default_database_metadata_path = fs::weakly_canonical (context->getPath ()) / " store"
294+ / DatabaseCatalog::getPathForUUID (default_database_uuid);
295+
296+ overlay->registerNextDatabase (std::make_shared<DatabaseAtomic>(name_, default_database_metadata_path, default_database_uuid, context));
284297 overlay->registerNextDatabase (std::make_shared<DatabaseFilesystem>(name_, " " , context));
285298 return overlay;
286299}
@@ -292,7 +305,7 @@ void LocalServer::tryInitPath()
292305
293306 if (getClientConfiguration ().has (" path" ))
294307 {
295- // User-supplied path.
308+ // / User-supplied path.
296309 path = getClientConfiguration ().getString (" path" );
297310 Poco::trimInPlace (path);
298311
@@ -306,15 +319,15 @@ void LocalServer::tryInitPath()
306319 }
307320 else
308321 {
309- // The path is not provided explicitly - use a unique path in the system temporary directory
310- // (or in the current dir if a temporary doesn't exist)
322+ // / The user requested to use a temporary path - use a unique path in the system temporary directory
323+ // / (or in the current dir if a temporary doesn't exist)
311324 LoggerRawPtr log = &logger ();
312325 std::filesystem::path parent_folder;
313326 std::filesystem::path default_path;
314327
315328 try
316329 {
317- // try to guess a tmp folder name, and check if it's a directory (throw exception otherwise)
330+ // / Try to guess a tmp folder name, and check if it's a directory (throw an exception otherwise).
318331 parent_folder = std::filesystem::temp_directory_path ();
319332
320333 }
@@ -342,7 +355,7 @@ void LocalServer::tryInitPath()
342355 temporary_directory_to_delete = default_path;
343356
344357 path = default_path.string ();
345- LOG_DEBUG (log, " Working directory created: {}" , path);
358+ LOG_DEBUG (log, " Working directory will be created as needed : {}" , path);
346359 }
347360
348361 global_context->setPath (fs::path (path) / " " );
@@ -883,30 +896,38 @@ void LocalServer::processConfig()
883896
884897 if (getClientConfiguration ().has (" path" ))
885898 {
899+ attachSystemTablesServer (global_context, *createMemoryDatabaseIfNotExists (global_context, DatabaseCatalog::SYSTEM_DATABASE), false );
900+ attachInformationSchema (global_context, *createMemoryDatabaseIfNotExists (global_context, DatabaseCatalog::INFORMATION_SCHEMA));
901+ attachInformationSchema (global_context, *createMemoryDatabaseIfNotExists (global_context, DatabaseCatalog::INFORMATION_SCHEMA_UPPERCASE));
902+
886903 String path = global_context->getPath ();
887- fs::create_directories (fs::path (path));
888904
889905 // / Lock path directory before read
906+ fs::create_directories (fs::path (path));
890907 status.emplace (fs::path (path) / " status" , StatusFile::write_full_info);
891908
892- LOG_DEBUG (log, " Loading metadata from {}" , path);
893- auto load_system_metadata_tasks = loadMetadataSystem (global_context);
894- attachSystemTablesServer (global_context, *createMemoryDatabaseIfNotExists (global_context, DatabaseCatalog::SYSTEM_DATABASE), false );
895- attachInformationSchema (global_context, *createMemoryDatabaseIfNotExists (global_context, DatabaseCatalog::INFORMATION_SCHEMA));
896- attachInformationSchema (global_context, *createMemoryDatabaseIfNotExists (global_context, DatabaseCatalog::INFORMATION_SCHEMA_UPPERCASE));
897- waitLoad (TablesLoaderForegroundPoolId, load_system_metadata_tasks);
898-
899- if (!getClientConfiguration ().has (" only-system-tables" ))
909+ if (fs::exists (fs::path (path) / " metadata" ))
900910 {
901- DatabaseCatalog::instance ().createBackgroundTasks ();
902- waitLoad (loadMetadata (global_context));
903- DatabaseCatalog::instance ().startupBackgroundTasks ();
904- }
911+ LOG_DEBUG (log, " Loading metadata from {}" , path);
905912
906- // / For ClickHouse local if path is not set the loader will be disabled.
907- global_context->getUserDefinedSQLObjectsStorage ().loadObjects ();
913+ if (fs::exists (std::filesystem::path (path) / " metadata" / " system.sql" ))
914+ {
915+ LoadTaskPtrs load_system_metadata_tasks = loadMetadataSystem (global_context);
916+ waitLoad (TablesLoaderForegroundPoolId, load_system_metadata_tasks);
917+ }
908918
909- LOG_DEBUG (log, " Loaded metadata." );
919+ if (!getClientConfiguration ().has (" only-system-tables" ))
920+ {
921+ DatabaseCatalog::instance ().createBackgroundTasks ();
922+ waitLoad (loadMetadata (global_context));
923+ DatabaseCatalog::instance ().startupBackgroundTasks ();
924+ }
925+
926+ // / For ClickHouse local if path is not set the loader will be disabled.
927+ global_context->getUserDefinedSQLObjectsStorage ().loadObjects ();
928+
929+ LOG_DEBUG (log, " Loaded metadata." );
930+ }
910931 }
911932 else if (!getClientConfiguration ().has (" no-system-tables" ))
912933 {
@@ -981,7 +1002,7 @@ void LocalServer::addExtraOptions(OptionsDescription & options_description)
9811002 (" logger.level" , po::value<std::string>(), " Log level" )
9821003
9831004 (" no-system-tables" , " do not attach system tables (better startup time)" )
984- (" path" , po::value<std::string>(), " Storage path" )
1005+ (" path" , po::value<std::string>(), " Storage path. If it was not specified, we will use a temporary directory, that is cleaned up on exit. " )
9851006 (" only-system-tables" , " attach only system tables from specified path" )
9861007 (" top_level_domains_path" , po::value<std::string>(), " Path to lists with custom TLDs" )
9871008 ;
@@ -1023,8 +1044,6 @@ void LocalServer::processOptions(const OptionsDescription &, const CommandLineOp
10231044 getClientConfiguration ().setBool (" no-system-tables" , true );
10241045 if (options.count (" only-system-tables" ))
10251046 getClientConfiguration ().setBool (" only-system-tables" , true );
1026- if (options.count (" database" ))
1027- getClientConfiguration ().setString (" default_database" , options[" database" ].as <std::string>());
10281047
10291048 if (options.count (" input-format" ))
10301049 getClientConfiguration ().setString (" table-data-format" , options[" input-format" ].as <std::string>());
0 commit comments