This commit is contained in:
@@ -193,6 +193,48 @@ bool Database::ensureDataDirs(std::string& err)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Database::migrateLegacyMapsDir(std::string& err)
|
||||
{
|
||||
if (data_dir_.filename() != "data")
|
||||
return true;
|
||||
|
||||
const std::filesystem::path legacy = "maps";
|
||||
const auto target = mapsDir();
|
||||
if (!std::filesystem::is_directory(legacy) || legacy == target)
|
||||
return true;
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(target, ec);
|
||||
if (ec)
|
||||
{
|
||||
err = "failed to create maps directory: " + target.string();
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(legacy, ec))
|
||||
{
|
||||
if (ec || !entry.is_directory())
|
||||
continue;
|
||||
const auto dest = target / entry.path().filename();
|
||||
if (std::filesystem::exists(dest))
|
||||
continue;
|
||||
std::filesystem::rename(entry.path(), dest, ec);
|
||||
if (ec)
|
||||
{
|
||||
ec.clear();
|
||||
std::filesystem::copy(entry.path(), dest, std::filesystem::copy_options::recursive, ec);
|
||||
if (ec)
|
||||
{
|
||||
err = "failed to migrate map directory: " + entry.path().string();
|
||||
return false;
|
||||
}
|
||||
std::filesystem::remove_all(entry.path(), ec);
|
||||
ec.clear();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Database::applySchema(std::string& err)
|
||||
{
|
||||
return execSql(db_, kSchemaSql, err);
|
||||
@@ -494,6 +536,8 @@ bool Database::init(std::string& err)
|
||||
return false;
|
||||
if (!ensureDataDirs(err))
|
||||
return false;
|
||||
if (!migrateLegacyMapsDir(err))
|
||||
return false;
|
||||
if (!migrateFromJsonIfNeeded(err))
|
||||
return false;
|
||||
if (!getMeta("schema_version"))
|
||||
|
||||
@@ -45,6 +45,7 @@ private:
|
||||
bool applySchemaMigrations(std::string& err);
|
||||
bool migrateFromJsonIfNeeded(std::string& err);
|
||||
bool ensureDataDirs(std::string& err);
|
||||
bool migrateLegacyMapsDir(std::string& err);
|
||||
std::optional<std::string> getMeta(const std::string& key) const;
|
||||
bool setMeta(const std::string& key, const std::string& value);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user