Commit dc830c18 authored by Ronan's avatar Ronan

fix(MainDb): check history table presence on import (workaround for android/soci)

parent f1d4d76b
......@@ -2346,6 +2346,11 @@ static T getValueFromLegacyMessage (const soci::row &message, int index, bool &i
return T();
}
static bool checkLegacyHistoryTableExists (soci::session &session) {
session << "SELECT name FROM sqlite_master WHERE type='table' AND name='history'";
return session.got_data() > 0;
}
bool MainDb::import (Backend, const string &parameters) {
L_D();
......@@ -2369,114 +2374,112 @@ bool MainDb::import (Backend, const string &parameters) {
try {
soci::transaction tr(*d->dbSession.getBackendSession<soci::session>());
if (!checkLegacyHistoryTableExists(*inSession))
return false;
unsigned int version = d->getModuleVersion("legacy-history-import");
if (version >= L_VERSION(1, 0, 0))
return false;
d->updateModuleVersion("legacy-history-import", DB_MODULE_VERSION_LEGACY_HISTORY_IMPORT);
soci::rowset<soci::row> messages = (inSession->prepare << "SELECT * FROM history");
try {
for (const auto &message : messages) {
const int direction = message.get<int>(LEGACY_MESSAGE_COL_DIRECTION);
if (direction != 0 && direction != 1) {
lWarning() << "Unable to import legacy message with invalid direction.";
continue;
}
for (const auto &message : messages) {
const int direction = message.get<int>(LEGACY_MESSAGE_COL_DIRECTION);
if (direction != 0 && direction != 1) {
lWarning() << "Unable to import legacy message with invalid direction.";
continue;
}
const int &state = message.get<int>(
LEGACY_MESSAGE_COL_STATE, static_cast<int>(ChatMessage::State::Displayed)
);
if (state < 0 || state > static_cast<int>(ChatMessage::State::Displayed)) {
lWarning() << "Unable to import legacy message with invalid state.";
continue;
}
const int &state = message.get<int>(
LEGACY_MESSAGE_COL_STATE, static_cast<int>(ChatMessage::State::Displayed)
);
if (state < 0 || state > static_cast<int>(ChatMessage::State::Displayed)) {
lWarning() << "Unable to import legacy message with invalid state.";
continue;
}
const tm &creationTime = Utils::getTimeTAsTm(message.get<int>(LEGACY_MESSAGE_COL_DATE, 0));
const tm &creationTime = Utils::getTimeTAsTm(message.get<int>(LEGACY_MESSAGE_COL_DATE, 0));
bool isNull;
getValueFromLegacyMessage<string>(message, LEGACY_MESSAGE_COL_URL, isNull);
bool isNull;
getValueFromLegacyMessage<string>(message, LEGACY_MESSAGE_COL_URL, isNull);
const int &contentId = message.get<int>(LEGACY_MESSAGE_COL_CONTENT_ID, -1);
ContentType contentType(message.get<string>(LEGACY_MESSAGE_COL_CONTENT_TYPE, ""));
if (!contentType.isValid())
contentType = contentId != -1
? ContentType::FileTransfer
: (isNull ? ContentType::PlainText : ContentType::ExternalBody);
if (contentType == ContentType::ExternalBody) {
lInfo() << "Import of external body content is skipped.";
const int &contentId = message.get<int>(LEGACY_MESSAGE_COL_CONTENT_ID, -1);
ContentType contentType(message.get<string>(LEGACY_MESSAGE_COL_CONTENT_TYPE, ""));
if (!contentType.isValid())
contentType = contentId != -1
? ContentType::FileTransfer
: (isNull ? ContentType::PlainText : ContentType::ExternalBody);
if (contentType == ContentType::ExternalBody) {
lInfo() << "Import of external body content is skipped.";
continue;
}
const string &text = getValueFromLegacyMessage<string>(message, LEGACY_MESSAGE_COL_TEXT, isNull);
Content content;
content.setContentType(contentType);
if (contentType == ContentType::PlainText) {
if (isNull) {
lWarning() << "Unable to import legacy message with no text.";
continue;
}
content.setBody(text);
} else {
if (contentType != ContentType::FileTransfer) {
lWarning() << "Unable to import unsupported legacy content.";
continue;
}
const string &text = getValueFromLegacyMessage<string>(message, LEGACY_MESSAGE_COL_TEXT, isNull);
Content content;
content.setContentType(contentType);
if (contentType == ContentType::PlainText) {
if (isNull) {
lWarning() << "Unable to import legacy message with no text.";
continue;
}
content.setBody(text);
} else {
if (contentType != ContentType::FileTransfer) {
lWarning() << "Unable to import unsupported legacy content.";
continue;
}
const string appData = getValueFromLegacyMessage<string>(message, LEGACY_MESSAGE_COL_APP_DATA, isNull);
if (isNull) {
lWarning() << "Unable to import legacy file message without app data.";
continue;
}
content.setAppData("legacy", appData);
const string appData = getValueFromLegacyMessage<string>(message, LEGACY_MESSAGE_COL_APP_DATA, isNull);
if (isNull) {
lWarning() << "Unable to import legacy file message without app data.";
continue;
}
soci::session *session = d->dbSession.getBackendSession<soci::session>();
const int &eventType = static_cast<int>(EventLog::Type::ConferenceChatMessage);
*session << "INSERT INTO event (type, creation_time) VALUES (:type, :creationTime)",
soci::use(eventType), soci::use(creationTime);
const long long &eventId = getLastInsertId();
const long long &localSipAddressId = d->insertSipAddress(message.get<string>(LEGACY_MESSAGE_COL_LOCAL_ADDRESS));
const long long &remoteSipAddressId = d->insertSipAddress(message.get<string>(LEGACY_MESSAGE_COL_REMOTE_ADDRESS));
const long long &chatRoomId = d->insertOrUpdateImportedBasicChatRoom(
remoteSipAddressId,
localSipAddressId,
creationTime
);
const int &isSecured = message.get<int>(LEGACY_MESSAGE_COL_IS_SECURED, 0);
*session << "INSERT INTO conference_event (event_id, chat_room_id)"
" VALUES (:eventId, :chatRoomId)", soci::use(eventId), soci::use(chatRoomId);
*session << "INSERT INTO conference_chat_message_event ("
" event_id, from_sip_address_id, to_sip_address_id,"
" time, state, direction, imdn_message_id, is_secured"
") VALUES ("
" :eventId, :localSipAddressId, :remoteSipAddressId,"
" :creationTime, :state, :direction, '', :isSecured"
")", soci::use(eventId), soci::use(localSipAddressId), soci::use(remoteSipAddressId),
soci::use(creationTime), soci::use(state), soci::use(direction),
soci::use(isSecured);
d->insertContent(eventId, content);
d->insertChatRoomParticipant(chatRoomId, remoteSipAddressId, false);
if (state != static_cast<int>(ChatMessage::State::Displayed))
d->insertChatMessageParticipant(eventId, remoteSipAddressId, state);
content.setAppData("legacy", appData);
}
tr.commit();
} catch (const exception &e) {
lInfo() << "Failed to import legacy messages from: `" << uri << "`. (" << e.what() << ")";
return false;
soci::session *session = d->dbSession.getBackendSession<soci::session>();
const int &eventType = static_cast<int>(EventLog::Type::ConferenceChatMessage);
*session << "INSERT INTO event (type, creation_time) VALUES (:type, :creationTime)",
soci::use(eventType), soci::use(creationTime);
const long long &eventId = getLastInsertId();
const long long &localSipAddressId = d->insertSipAddress(message.get<string>(LEGACY_MESSAGE_COL_LOCAL_ADDRESS));
const long long &remoteSipAddressId = d->insertSipAddress(message.get<string>(LEGACY_MESSAGE_COL_REMOTE_ADDRESS));
const long long &chatRoomId = d->insertOrUpdateImportedBasicChatRoom(
remoteSipAddressId,
localSipAddressId,
creationTime
);
const int &isSecured = message.get<int>(LEGACY_MESSAGE_COL_IS_SECURED, 0);
*session << "INSERT INTO conference_event (event_id, chat_room_id)"
" VALUES (:eventId, :chatRoomId)", soci::use(eventId), soci::use(chatRoomId);
*session << "INSERT INTO conference_chat_message_event ("
" event_id, from_sip_address_id, to_sip_address_id,"
" time, state, direction, imdn_message_id, is_secured"
") VALUES ("
" :eventId, :localSipAddressId, :remoteSipAddressId,"
" :creationTime, :state, :direction, '', :isSecured"
")", soci::use(eventId), soci::use(localSipAddressId), soci::use(remoteSipAddressId),
soci::use(creationTime), soci::use(state), soci::use(direction),
soci::use(isSecured);
d->insertContent(eventId, content);
d->insertChatRoomParticipant(chatRoomId, remoteSipAddressId, false);
if (state != static_cast<int>(ChatMessage::State::Displayed))
d->insertChatMessageParticipant(eventId, remoteSipAddressId, state);
}
lInfo() << "Successful import of legacy messages from: `" << uri << "`.";
} catch (const exception &) {
// Table doesn't exist.
tr.commit();
} catch (const exception &e) {
lInfo() << "Failed to import legacy messages from: `" << uri << "`. (" << e.what() << ")";
return false;
}
lInfo() << "Successful import of legacy messages from: `" << uri << "`.";
return true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment