diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 114749d3a..d82ac82ad 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -6709,11 +6709,13 @@ int linphone_core_get_video_dscp(const LinphoneCore *lc){ void linphone_core_set_chat_database_path (LinphoneCore *lc, const char *path) { if (!linphone_core_conference_server_enabled(lc)) { - auto &mainDb = L_GET_PRIVATE(lc->cppPtr)->mainDb; - if (mainDb) + auto &mainDb = L_GET_PRIVATE_FROM_C_OBJECT(lc)->mainDb; + if (mainDb) { mainDb->import(LinphonePrivate::MainDb::Sqlite3, path); - else + L_GET_PRIVATE_FROM_C_OBJECT(lc)->loadChatRooms(); + } else { ms_warning("linphone_core_set_chat_database_path() needs to be called once linphone_core_start() has been called"); + } } } diff --git a/src/core/core-chat-room.cpp b/src/core/core-chat-room.cpp index 511963e3b..ee9cd8a6c 100644 --- a/src/core/core-chat-room.cpp +++ b/src/core/core-chat-room.cpp @@ -118,6 +118,11 @@ void CorePrivate::insertChatRoomWithDb (const shared_ptr &chat mainDb->insertChatRoom(chatRoom); } +void CorePrivate::loadChatRooms () { + for (auto &chatRoom : mainDb->getChatRooms()) + insertChatRoom(chatRoom); +} + void CorePrivate::replaceChatRoom (const shared_ptr &replacedChatRoom, const shared_ptr &newChatRoom) { const ChatRoomId &replacedChatRoomId = replacedChatRoom->getChatRoomId(); const ChatRoomId &newChatRoomId = newChatRoom->getChatRoomId(); diff --git a/src/core/core-p.h b/src/core/core-p.h index 71feb8492..1f47779c6 100644 --- a/src/core/core-p.h +++ b/src/core/core-p.h @@ -57,6 +57,7 @@ public: void postConfigureAudioStream (AudioStream *stream, bool muted); void setPlaybackGainDb (AudioStream *stream, float gain); + void loadChatRooms (); void insertChatRoom (const std::shared_ptr &chatRoom); void insertChatRoomWithDb (const std::shared_ptr &chatRoom); std::shared_ptr createBasicChatRoom (const ChatRoomId &chatRoomId, AbstractChatRoom::CapabilitiesMask capabilities); diff --git a/src/core/core.cpp b/src/core/core.cpp index c09784f5c..eebb785de 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -57,8 +57,7 @@ void CorePrivate::init () { if (!mainDb->connect(backend, uri)) lFatal() << "Unable to open linphone database."; - for (auto &chatRoom : mainDb->getChatRooms()) - insertChatRoom(chatRoom); + loadChatRooms(); } void CorePrivate::registerListener (CoreListener *listener) { diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 41ece4212..5868ba477 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -1423,8 +1423,12 @@ void MainDbPrivate::importLegacyHistory (DbSession &inDbSession) { soci::use(eventType), soci::use(creationTime); const long long &eventId = dbSession.getLastInsertId(); - const long long &localSipAddressId = insertSipAddress(message.get(LegacyMessageColLocalAddress)); - const long long &remoteSipAddressId = insertSipAddress(message.get(LegacyMessageColRemoteAddress)); + const long long &localSipAddressId = insertSipAddress( + IdentityAddress(message.get(LegacyMessageColLocalAddress)).asString() + ); + const long long &remoteSipAddressId = insertSipAddress( + IdentityAddress(message.get(LegacyMessageColRemoteAddress)).asString() + ); const long long &chatRoomId = insertOrUpdateImportedBasicChatRoom( remoteSipAddressId, localSipAddressId, diff --git a/tester/db/messages.db b/tester/db/messages.db index 01ad041d3..2eb613810 100644 Binary files a/tester/db/messages.db and b/tester/db/messages.db differ diff --git a/tester/message_tester.c b/tester/message_tester.c index bdcafa835..6aaea5cb7 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -1467,7 +1467,7 @@ static void lime_unit(void) { * Destination file is truncated if existing. * Return 0 on success, positive value on error. */ -int message_tester_copy_file(const char *from, const char *to) +static int message_tester_copy_file(const char *from, const char *to) { FILE *in, *out; char buf[256]; @@ -2309,7 +2309,7 @@ static void message_received_callback(LinphoneCore *lc, LinphoneChatRoom *room, } BC_ASSERT_EQUAL(0, linphone_chat_room_get_unread_messages_count(room), int, "%d"); } - + void unread_message_count_callback(void) { LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc"); LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc"); @@ -2328,6 +2328,26 @@ void unread_message_count_callback(void) { linphone_core_manager_destroy(pauline); } +static void migration_from_messages_db (void) { + LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc"); + char *src_db = bc_tester_res("db/messages.db"); + char *tmp_db = bc_tester_file("tmp.db"); + + BC_ASSERT_EQUAL(message_tester_copy_file(src_db, tmp_db), 0, int, "%d"); + + // The messages.db has 10000 dummy messages with the very first DB scheme. + // This will test the migration procedure + linphone_core_set_chat_database_path(marie->lc, tmp_db); + + const bctbx_list_t *chatrooms = linphone_core_get_chat_rooms(marie->lc); + BC_ASSERT(bctbx_list_size(chatrooms) > 0); + + linphone_core_manager_destroy(marie); + remove(tmp_db); + bctbx_free(src_db); + bctbx_free(tmp_db); +} + test_t message_tests[] = { TEST_NO_TAG("Text message", text_message), TEST_NO_TAG("Text message with credentials from auth callback", text_message_with_credential_from_auth_callback), @@ -2404,6 +2424,7 @@ test_t message_tests[] = { TEST_NO_TAG("Crash during file transfer", crash_during_file_transfer), TEST_NO_TAG("Text status after destroying chat room", text_status_after_destroying_chat_room), TEST_NO_TAG("Transfer io error after destroying chatroom", file_transfer_io_error_after_destroying_chatroom), + TEST_NO_TAG("Migration from messages db", migration_from_messages_db) }; static int message_tester_before_suite(void) {