fix(export): open sqlite on demand when flatfile is active backend
Some checks failed
CI Code / Code Coverage (pull_request) Failing after 12m27s
CI Code / Check spelling (pull_request) Failing after 12m56s
CI Code / Check coding style (pull_request) Failing after 13m33s
CI Code / Linux (ubuntu) (pull_request) Failing after 14m3s
CI Code / Linux (debian) (pull_request) Failing after 14m38s
CI Code / Linux (arch) (pull_request) Failing after 15m12s
Some checks failed
CI Code / Code Coverage (pull_request) Failing after 12m27s
CI Code / Check spelling (pull_request) Failing after 12m56s
CI Code / Check coding style (pull_request) Failing after 13m33s
CI Code / Linux (ubuntu) (pull_request) Failing after 14m3s
CI Code / Linux (debian) (pull_request) Failing after 14m38s
CI Code / Linux (arch) (pull_request) Failing after 15m12s
This commit is contained in:
@@ -93,6 +93,7 @@ void db_sqlite_begin_transaction(void);
|
|||||||
void db_sqlite_end_transaction(void);
|
void db_sqlite_end_transaction(void);
|
||||||
void db_sqlite_rollback_transaction(void);
|
void db_sqlite_rollback_transaction(void);
|
||||||
int db_sqlite_last_changes(void);
|
int db_sqlite_last_changes(void);
|
||||||
|
gboolean db_sqlite_is_open(void);
|
||||||
#endif
|
#endif
|
||||||
db_backend_t* db_backend_flatfile(void);
|
db_backend_t* db_backend_flatfile(void);
|
||||||
|
|
||||||
|
|||||||
@@ -30,10 +30,12 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "config/files.h"
|
#include "config/files.h"
|
||||||
|
#include "config/accounts.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "database_flatfile.h"
|
#include "database_flatfile.h"
|
||||||
#include "config/preferences.h"
|
#include "config/preferences.h"
|
||||||
#include "ui/ui.h"
|
#include "ui/ui.h"
|
||||||
|
#include "xmpp/session.h"
|
||||||
#include "xmpp/xmpp.h"
|
#include "xmpp/xmpp.h"
|
||||||
#include "xmpp/message.h"
|
#include "xmpp/message.h"
|
||||||
|
|
||||||
@@ -46,6 +48,43 @@
|
|||||||
// Print progress to console every N messages during export/import
|
// Print progress to console every N messages during export/import
|
||||||
#define EXPORT_PROGRESS_INTERVAL 500
|
#define EXPORT_PROGRESS_INTERVAL 500
|
||||||
|
|
||||||
|
// Open SQLite if it isn't already (e.g. when flatfile is the active backend
|
||||||
|
// and sqlite was never initialised this session, or was closed by /history
|
||||||
|
// switch). Returns 1 if we opened it (caller must close), 0 if it was already
|
||||||
|
// open, -1 on failure.
|
||||||
|
static int
|
||||||
|
_ensure_sqlite_open(void)
|
||||||
|
{
|
||||||
|
if (db_sqlite_is_open())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
db_backend_t* sqlite_be = db_backend_sqlite();
|
||||||
|
if (!sqlite_be || !sqlite_be->init)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
const char* account_name = session_get_account_name();
|
||||||
|
if (!account_name)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
ProfAccount* account = accounts_get_account(account_name);
|
||||||
|
if (!account)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
gboolean ok = sqlite_be->init(account);
|
||||||
|
account_free(account);
|
||||||
|
return ok ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_close_temp_sqlite(int opened)
|
||||||
|
{
|
||||||
|
if (opened != 1)
|
||||||
|
return;
|
||||||
|
db_backend_t* sqlite_be = db_backend_sqlite();
|
||||||
|
if (sqlite_be && sqlite_be->close)
|
||||||
|
sqlite_be->close();
|
||||||
|
}
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Dedup key: stanza_id if present, else hash of timestamp+from+body
|
// Dedup key: stanza_id if present, else hash of timestamp+from+body
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
@@ -429,6 +468,15 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SQLite must be open for db_sqlite_list_contacts/db_sqlite_get_all_chat
|
||||||
|
// to return rows. When the flatfile backend is active, sqlite is closed —
|
||||||
|
// queries silently return NULL and the export reports zero changes.
|
||||||
|
int sqlite_opened = _ensure_sqlite_open();
|
||||||
|
if (sqlite_opened < 0) {
|
||||||
|
cons_show_error("Export failed: could not open SQLite database.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Determine contact list
|
// Determine contact list
|
||||||
GSList* contacts = NULL;
|
GSList* contacts = NULL;
|
||||||
if (contact_jid) {
|
if (contact_jid) {
|
||||||
@@ -437,6 +485,7 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
|||||||
contacts = db_sqlite_list_contacts();
|
contacts = db_sqlite_list_contacts();
|
||||||
if (!contacts) {
|
if (!contacts) {
|
||||||
cons_show("No contacts found in SQLite database.");
|
cons_show("No contacts found in SQLite database.");
|
||||||
|
_close_temp_sqlite(sqlite_opened);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cons_show("Exporting %d contact(s) to flat-file format...", g_slist_length(contacts));
|
cons_show("Exporting %d contact(s) to flat-file format...", g_slist_length(contacts));
|
||||||
@@ -452,6 +501,7 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
g_slist_free_full(contacts, g_free);
|
g_slist_free_full(contacts, g_free);
|
||||||
|
_close_temp_sqlite(sqlite_opened);
|
||||||
return total_exported;
|
return total_exported;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -478,6 +528,12 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sqlite_opened = _ensure_sqlite_open();
|
||||||
|
if (sqlite_opened < 0) {
|
||||||
|
cons_show_error("Import failed: could not open SQLite database.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Determine contacts
|
// Determine contacts
|
||||||
GSList* contacts = NULL;
|
GSList* contacts = NULL;
|
||||||
if (contact_jid) {
|
if (contact_jid) {
|
||||||
@@ -486,6 +542,7 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
|||||||
contacts = _ff_list_contacts();
|
contacts = _ff_list_contacts();
|
||||||
if (!contacts) {
|
if (!contacts) {
|
||||||
cons_show("No flat-file history found to import.");
|
cons_show("No flat-file history found to import.");
|
||||||
|
_close_temp_sqlite(sqlite_opened);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -613,6 +670,7 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
g_slist_free_full(contacts, g_free);
|
g_slist_free_full(contacts, g_free);
|
||||||
|
_close_temp_sqlite(sqlite_opened);
|
||||||
return total_imported;
|
return total_imported;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ static gboolean _check_available_space_for_db_migration(char* path_to_db);
|
|||||||
|
|
||||||
static const int latest_version = 2;
|
static const int latest_version = 2;
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
db_sqlite_is_open(void)
|
||||||
|
{
|
||||||
|
return g_chatlog_database != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Helper: close DB handle (if any), warn on busy, and shutdown SQLite
|
// Helper: close DB handle (if any), warn on busy, and shutdown SQLite
|
||||||
static void
|
static void
|
||||||
_db_teardown(const char* ctx)
|
_db_teardown(const char* ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user