summaryrefslogtreecommitdiff
path: root/src/database/database.hpp
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2018-04-10 23:33:59 +0200
committerlouiz’ <louiz@louiz.org>2018-04-11 00:25:18 +0200
commit857c7d3972a03cbeebf730d99b924d3710dee6a0 (patch)
tree5bf1ed66ba7ddda3f16a470115cf364a8c7a10cb /src/database/database.hpp
parent0cd848e532c8c60ed4f3a5d1e6a3850929f2765b (diff)
downloadbiboumi-857c7d3972a03cbeebf730d99b924d3710dee6a0.tar.gz
biboumi-857c7d3972a03cbeebf730d99b924d3710dee6a0.tar.bz2
biboumi-857c7d3972a03cbeebf730d99b924d3710dee6a0.tar.xz
biboumi-857c7d3972a03cbeebf730d99b924d3710dee6a0.zip
Use a different Date data type
PLEASE backup your database before testing this commit, and report any migration issue. In postgresql, we use timestamp with timezone. In sqlite3 we use REAL (the date is expressed as julianday) This requires a migration of the muclogline_ table: In postgresql it’s pretty simple, we convert all the integer into timestamps With sqlite3, we actually rename the table, create the new one with the correct type, then copy everything to the new table, with a conversion function for the Date_ column, and then we delete the old table. fix #3343
Diffstat (limited to 'src/database/database.hpp')
-rw-r--r--src/database/database.hpp30
1 files changed, 26 insertions, 4 deletions
diff --git a/src/database/database.hpp b/src/database/database.hpp
index d986ecc..75ff8f3 100644
--- a/src/database/database.hpp
+++ b/src/database/database.hpp
@@ -10,6 +10,7 @@
#include <database/engine.hpp>
#include <utils/optional_bool.hpp>
+#include <utils/datetime.hpp>
#include <chrono>
#include <string>
@@ -17,11 +18,9 @@
#include <memory>
#include <map>
-
class Database
{
public:
- using time_point = std::chrono::system_clock::time_point;
struct RecordNotFound: public std::exception {};
enum class Paging { first, last };
@@ -37,7 +36,8 @@ class Database
struct Server: Column<std::string> { static constexpr auto name = "server_"; };
- struct Date: Column<time_point::rep> { static constexpr auto name = "date_"; };
+ struct OldDate: Column<std::chrono::system_clock::time_point::rep> { static constexpr auto name = "date_"; };
+ struct Date: Column<DateTime> { static constexpr auto name = "date_"; };
struct Body: Column<std::string> { static constexpr auto name = "body_"; };
@@ -88,6 +88,8 @@ class Database
using MucLogLineTable = Table<Id, Uuid, Owner, IrcChanName, IrcServerName, Date, Body, Nick>;
using MucLogLine = MucLogLineTable::RowType;
+ using OldMucLogLineTable = Table<Id, Uuid, Owner, IrcChanName, IrcServerName, OldDate, Body, Nick>;
+ using OldMucLogLine = OldMucLogLineTable::RowType;
using GlobalOptionsTable = Table<Id, Owner, MaxHistoryLength, RecordHistory, GlobalPersistent>;
using GlobalOptions = GlobalOptionsTable::RowType;
@@ -141,7 +143,7 @@ class Database
*/
static MucLogLine get_muc_log(const std::string& owner, const std::string& chan_name, const std::string& server, const std::string& uuid, const std::string& start="", const std::string& end="");
static std::string store_muc_message(const std::string& owner, const std::string& chan_name, const std::string& server_name,
- time_point date, const std::string& body, const std::string& nick);
+ DateTime::time_point date, const std::string& body, const std::string& nick);
static void add_roster_item(const std::string& local, const std::string& remote);
static bool has_roster_item(const std::string& local, const std::string& remote);
@@ -168,6 +170,13 @@ class Database
static std::unique_ptr<DatabaseEngine> db;
+ static DatabaseEngine::EngineType engine_type()
+ {
+ if (Database::db)
+ return Database::db->engine_type();
+ return DatabaseEngine::EngineType::None;
+ }
+
/**
* Some caches, to avoid doing very frequent query requests for a few options.
*/
@@ -216,7 +225,20 @@ class Transaction
public:
Transaction();
~Transaction();
+ void rollback();
bool success{false};
};
+template <typename... T>
+void convert_date_format(DatabaseEngine& db, Table<T...> table)
+{
+ const auto existing_columns = db.get_all_columns_from_table(table.get_name());
+ const auto date_pair = existing_columns.find(Database::Date::name);
+ if (date_pair != existing_columns.end() && date_pair->second == "integer")
+ {
+ log_info("Converting Date_ format to the new one.");
+ db.convert_date_format(db);
+ }
+}
+
#endif /* USE_DATABASE */