[Fix #4] Archive current db when creating new

This commit is contained in:
Martin Brodbeck 2018-10-08 10:48:28 +02:00
parent 271aac0914
commit cfc632a986
3 changed files with 26 additions and 3 deletions

View file

@ -5,6 +5,8 @@
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include "boost/date_time/posix_time/posix_time.hpp"
Database::Database(const std::string& dbname) Database::Database(const std::string& dbname)
{ {
dbname_ = dbname; dbname_ = dbname;
@ -30,11 +32,26 @@ Database::Database()
throw err; throw err;
} }
} }
dbpath /= "kima2-cpp.db"; dbpath /= "kima2.db";
dbname_ = dbpath.string(); dbname_ = dbpath.string();
init(); 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_); } Database::~Database() { sqlite3_close(db_); }
void Database::exec(const std::string& sql) void Database::exec(const std::string& sql)

View file

@ -24,6 +24,7 @@ class Database
unsigned int loadSales(std::vector<std::unique_ptr<Sale>>& sales, unsigned int loadSales(std::vector<std::unique_ptr<Sale>>& sales,
std::vector<std::unique_ptr<Seller>>& sellers); std::vector<std::unique_ptr<Seller>>& sellers);
void updateCashPointNo(int oldCashPointNo, int newCashPointNo); void updateCashPointNo(int oldCashPointNo, int newCashPointNo);
void newDb();
private: private:
sqlite3* db_{nullptr}; sqlite3* db_{nullptr};

View file

@ -7,6 +7,7 @@
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <sstream> #include <sstream>
#include <filesystem>
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -272,7 +273,7 @@ std::string escapeCsvValue(const std::string& value, const char delimiter)
void Marketplace::clear() 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") { if (seller->getUuidAsString() == "11111111-1111-1111-1111-111111111111") {
for (auto& article : seller->getArticles()) { for (auto& article : seller->getArticles()) {
article->setState(Article::State::DELETE); article->setState(Article::State::DELETE);
@ -283,5 +284,9 @@ void Marketplace::clear()
}); });
std::for_each(sales_.begin(), sales_.end(), std::for_each(sales_.begin(), sales_.end(),
[](auto& sale) { sale->setState(Sale::State::DELETE); }); [](auto& sale) { sale->setState(Sale::State::DELETE); });
storeToDb(); storeToDb(); */
Database db;
db.newDb();
loadFromDb();
} }