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:
@@ -30,10 +30,12 @@
|
||||
#include "log.h"
|
||||
#include "common.h"
|
||||
#include "config/files.h"
|
||||
#include "config/accounts.h"
|
||||
#include "database.h"
|
||||
#include "database_flatfile.h"
|
||||
#include "config/preferences.h"
|
||||
#include "ui/ui.h"
|
||||
#include "xmpp/session.h"
|
||||
#include "xmpp/xmpp.h"
|
||||
#include "xmpp/message.h"
|
||||
|
||||
@@ -46,6 +48,43 @@
|
||||
// Print progress to console every N messages during export/import
|
||||
#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
|
||||
// =========================================================================
|
||||
@@ -429,6 +468,15 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
||||
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
|
||||
GSList* contacts = NULL;
|
||||
if (contact_jid) {
|
||||
@@ -437,6 +485,7 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
||||
contacts = db_sqlite_list_contacts();
|
||||
if (!contacts) {
|
||||
cons_show("No contacts found in SQLite database.");
|
||||
_close_temp_sqlite(sqlite_opened);
|
||||
return 0;
|
||||
}
|
||||
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);
|
||||
_close_temp_sqlite(sqlite_opened);
|
||||
return total_exported;
|
||||
}
|
||||
|
||||
@@ -478,6 +528,12 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
||||
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
|
||||
GSList* contacts = NULL;
|
||||
if (contact_jid) {
|
||||
@@ -486,6 +542,7 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
||||
contacts = _ff_list_contacts();
|
||||
if (!contacts) {
|
||||
cons_show("No flat-file history found to import.");
|
||||
_close_temp_sqlite(sqlite_opened);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -613,6 +670,7 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
||||
}
|
||||
|
||||
g_slist_free_full(contacts, g_free);
|
||||
_close_temp_sqlite(sqlite_opened);
|
||||
return total_imported;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user