From cfc632a9863231d72841d3f6997de5cd908c4837 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 8 Oct 2018 10:48:28 +0200 Subject: [PATCH] [Fix #4] Archive current db when creating new --- src/core/database.cpp | 19 ++++++++++++++++++- src/core/database.h | 1 + src/core/marketplace.cpp | 9 +++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/core/database.cpp b/src/core/database.cpp index a3240cf..411135f 100644 --- a/src/core/database.cpp +++ b/src/core/database.cpp @@ -5,6 +5,8 @@ #include #include +#include "boost/date_time/posix_time/posix_time.hpp" + Database::Database(const std::string& dbname) { dbname_ = dbname; @@ -30,11 +32,26 @@ Database::Database() throw err; } } - dbpath /= "kima2-cpp.db"; + dbpath /= "kima2.db"; dbname_ = dbpath.string(); init(); } +void Database::newDb() +{ + namespace fs = std::filesystem; + + fs::path sourcePath = dbname_; + fs::path destPath = sourcePath.parent_path() / sourcePath.stem(); + destPath += std::string("_") += boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time()) += ".db"; + + fs::copy_file(sourcePath, destPath, fs::copy_options::overwrite_existing); + + fs::remove(sourcePath); + + init(); +} + Database::~Database() { sqlite3_close(db_); } void Database::exec(const std::string& sql) diff --git a/src/core/database.h b/src/core/database.h index 2bd3e0b..661d433 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -24,6 +24,7 @@ class Database unsigned int loadSales(std::vector>& sales, std::vector>& sellers); void updateCashPointNo(int oldCashPointNo, int newCashPointNo); + void newDb(); private: sqlite3* db_{nullptr}; diff --git a/src/core/marketplace.cpp b/src/core/marketplace.cpp index 7ec5122..302981a 100644 --- a/src/core/marketplace.cpp +++ b/src/core/marketplace.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace fs = std::filesystem; @@ -272,7 +273,7 @@ std::string escapeCsvValue(const std::string& value, const char delimiter) void Marketplace::clear() { - std::for_each(sellers_.begin(), sellers_.end(), [](auto& seller) { + /* std::for_each(sellers_.begin(), sellers_.end(), [](auto& seller) { if (seller->getUuidAsString() == "11111111-1111-1111-1111-111111111111") { for (auto& article : seller->getArticles()) { article->setState(Article::State::DELETE); @@ -283,5 +284,9 @@ void Marketplace::clear() }); std::for_each(sales_.begin(), sales_.end(), [](auto& sale) { sale->setState(Sale::State::DELETE); }); - storeToDb(); + storeToDb(); */ + + Database db; + db.newDb(); + loadFromDb(); } \ No newline at end of file