diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 7decf2e3d..449522345 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -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 ¶meters) { L_D(); @@ -2369,114 +2374,112 @@ bool MainDb::import (Backend, const string ¶meters) { try { soci::transaction tr(*d->dbSession.getBackendSession()); + 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 messages = (inSession->prepare << "SELECT * FROM history"); - try { - for (const auto &message : messages) { - const int direction = message.get(LEGACY_MESSAGE_COL_DIRECTION); - if (direction != 0 && direction != 1) { - lWarning() << "Unable to import legacy message with invalid direction."; - continue; - } - - const int &state = message.get( - LEGACY_MESSAGE_COL_STATE, static_cast(ChatMessage::State::Displayed) - ); - if (state < 0 || state > static_cast(ChatMessage::State::Displayed)) { - lWarning() << "Unable to import legacy message with invalid state."; - continue; - } - - const tm &creationTime = Utils::getTimeTAsTm(message.get(LEGACY_MESSAGE_COL_DATE, 0)); - - bool isNull; - getValueFromLegacyMessage(message, LEGACY_MESSAGE_COL_URL, isNull); - - const int &contentId = message.get(LEGACY_MESSAGE_COL_CONTENT_ID, -1); - ContentType contentType(message.get(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(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(message, LEGACY_MESSAGE_COL_APP_DATA, isNull); - if (isNull) { - lWarning() << "Unable to import legacy file message without app data."; - continue; - } - - content.setAppData("legacy", appData); - } - - soci::session *session = d->dbSession.getBackendSession(); - const int &eventType = static_cast(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(LEGACY_MESSAGE_COL_LOCAL_ADDRESS)); - const long long &remoteSipAddressId = d->insertSipAddress(message.get(LEGACY_MESSAGE_COL_REMOTE_ADDRESS)); - const long long &chatRoomId = d->insertOrUpdateImportedBasicChatRoom( - remoteSipAddressId, - localSipAddressId, - creationTime - ); - const int &isSecured = message.get(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(ChatMessage::State::Displayed)) - d->insertChatMessageParticipant(eventId, remoteSipAddressId, state); + for (const auto &message : messages) { + const int direction = message.get(LEGACY_MESSAGE_COL_DIRECTION); + if (direction != 0 && direction != 1) { + lWarning() << "Unable to import legacy message with invalid direction."; + continue; } - tr.commit(); - } catch (const exception &e) { - lInfo() << "Failed to import legacy messages from: `" << uri << "`. (" << e.what() << ")"; - return false; + const int &state = message.get( + LEGACY_MESSAGE_COL_STATE, static_cast(ChatMessage::State::Displayed) + ); + if (state < 0 || state > static_cast(ChatMessage::State::Displayed)) { + lWarning() << "Unable to import legacy message with invalid state."; + continue; + } + + const tm &creationTime = Utils::getTimeTAsTm(message.get(LEGACY_MESSAGE_COL_DATE, 0)); + + bool isNull; + getValueFromLegacyMessage(message, LEGACY_MESSAGE_COL_URL, isNull); + + const int &contentId = message.get(LEGACY_MESSAGE_COL_CONTENT_ID, -1); + ContentType contentType(message.get(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(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(message, LEGACY_MESSAGE_COL_APP_DATA, isNull); + if (isNull) { + lWarning() << "Unable to import legacy file message without app data."; + continue; + } + + content.setAppData("legacy", appData); + } + + soci::session *session = d->dbSession.getBackendSession(); + const int &eventType = static_cast(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(LEGACY_MESSAGE_COL_LOCAL_ADDRESS)); + const long long &remoteSipAddressId = d->insertSipAddress(message.get(LEGACY_MESSAGE_COL_REMOTE_ADDRESS)); + const long long &chatRoomId = d->insertOrUpdateImportedBasicChatRoom( + remoteSipAddressId, + localSipAddressId, + creationTime + ); + const int &isSecured = message.get(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(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; }