1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "media_library_db_upgrade.h"
16
17 #include <string>
18 #include <vector>
19
20 #include "dfx_transaction.h"
21 #include "rdb_store.h"
22 #include "media_log.h"
23 #include "album_plugin_table_event_handler.h"
24 #include "db_upgrade_utils.h"
25 #include "medialibrary_rdb_transaction.h"
26 #include "photo_album_update_date_modified_operation.h"
27 #include "photo_day_month_year_operation.h"
28
29 namespace OHOS::Media {
30 namespace DataTransfer {
31 /**
32 * @brief Upgrade the database, before data restore or clone.
33 */
OnUpgrade(NativeRdb::RdbStore & store)34 int32_t MediaLibraryDbUpgrade::OnUpgrade(NativeRdb::RdbStore &store)
35 {
36 int32_t ret = NativeRdb::E_OK;
37 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::OnUpgrade start");
38 ret = this->UpgradeAlbumPlugin(store);
39 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
40
41 ret = this->UpgradePhotoAlbum(store);
42 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
43
44 ret = this->UpgradePhotos(store);
45 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
46
47 ret = this->UpgradePhotoMap(store);
48 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
49
50 ret = this->MergeAlbumFromOldBundleNameToNewBundleName(store);
51 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
52
53 ret = this->UpgradePhotosBelongsToAlbum(store);
54 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
55
56 ret = this->ExecSqlWithRetry([&]() {
57 return PhotoAlbumUpdateDateModifiedOperation::UpdateAlbumDateNeedFix(store);
58 });
59 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
60
61 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::OnUpgrade end");
62 return NativeRdb::E_OK;
63 }
64
ExecSqlWithRetry(std::function<int32_t ()> execSql)65 int32_t MediaLibraryDbUpgrade::ExecSqlWithRetry(std::function<int32_t()> execSql)
66 {
67 int currentTime = 0;
68 int32_t busyRetryTime = 0;
69 int32_t err = NativeRdb::E_OK;
70 while (busyRetryTime < MAX_BUSY_TRY_TIMES && currentTime <= MAX_TRY_TIMES) {
71 err = execSql();
72 if (err == NativeRdb::E_OK) {
73 break;
74 } else if (err == NativeRdb::E_SQLITE_LOCKED) {
75 std::this_thread::sleep_for(std::chrono::milliseconds(TRANSACTION_WAIT_INTERVAL));
76 currentTime++;
77 MEDIA_ERR_LOG("ExecuteSqlInternal busy, ret:%{public}d, time:%{public}d", err, currentTime);
78 } else if (err == NativeRdb::E_SQLITE_BUSY || err == NativeRdb::E_DATABASE_BUSY) {
79 busyRetryTime++;
80 MEDIA_ERR_LOG("ExecuteSqlInternal busy, ret:%{public}d, busyRetryTime:%{public}d", err, busyRetryTime);
81 } else {
82 MEDIA_ERR_LOG("ExecuteSqlInternal faile, ret = %{public}d", err);
83 break;
84 }
85 }
86 return err;
87 }
88
MergeAlbumFromOldBundleNameToNewBundleName(NativeRdb::RdbStore & store)89 int32_t MediaLibraryDbUpgrade::MergeAlbumFromOldBundleNameToNewBundleName(NativeRdb::RdbStore &store)
90 {
91 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::MergeAlbumFromOldBundleNameToNewBundleName start");
92 int32_t ret = NativeRdb::E_OK;
93 std::vector<NativeRdb::ValueObject> args;
94 for (auto &executeSql : SQL_MERGE_ALBUM_FROM_OLD_BUNDLE_NAME_TO_NEW_BUNDLE_NAME) {
95 ret = ExecSqlWithRetry([&]() { return store.ExecuteSql(executeSql); });
96 CHECK_AND_RETURN_RET_LOG(ret == NativeRdb::E_OK, ret,
97 "Media_Restore: executeSql failed, sql: %{public}s", executeSql.c_str());
98 }
99 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::MergeAlbumFromOldBundleNameToNewBundleName end");
100 return NativeRdb::E_OK;
101 }
102
UpgradePhotosBelongsToAlbum(NativeRdb::RdbStore & store)103 int32_t MediaLibraryDbUpgrade::UpgradePhotosBelongsToAlbum(NativeRdb::RdbStore &store)
104 {
105 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::UpgradePhotosBelongsToAlbum start");
106 int32_t ret = NativeRdb::E_OK;
107 std::vector<NativeRdb::ValueObject> args;
108 for (const auto &executeSql : this->SQL_PHOTOS_NEED_TO_BELONGS_TO_ALBUM) {
109 ret = ExecSqlWithRetry([&]() { return store.ExecuteSql(executeSql); });
110 CHECK_AND_RETURN_RET_LOG(ret == NativeRdb::E_OK, ret,
111 "Media_Restore: executeSql failed, sql: %{public}s", executeSql.c_str());
112 }
113 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::UpgradePhotosBelongsToAlbum end");
114 return NativeRdb::E_OK;
115 }
116
117 /**
118 * @brief Upgrade album_plugin table.
119 */
UpgradeAlbumPlugin(NativeRdb::RdbStore & store)120 int32_t MediaLibraryDbUpgrade::UpgradeAlbumPlugin(NativeRdb::RdbStore &store)
121 {
122 CHECK_AND_RETURN_RET(!(this->dbUpgradeUtils_.IsTableExists(store, "album_plugin")), NativeRdb::E_OK);
123 AlbumPluginTableEventHandler handler;
124 return handler.OnUpgrade(store, 0, 0);
125 }
126
127 /**
128 * @brief Upgrade PhotoAlbum table.
129 */
UpgradePhotoAlbum(NativeRdb::RdbStore & store)130 int32_t MediaLibraryDbUpgrade::UpgradePhotoAlbum(NativeRdb::RdbStore &store)
131 {
132 int32_t ret = NativeRdb::E_OK;
133 ret = this->dbUpgradeUtils_.DropAllTriggers(store, "PhotoAlbum");
134 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
135
136 ret = this->dbUpgradeUtils_.DropAllUniqueIndex(store, "PhotoAlbum");
137 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
138
139 ret = this->AddlPathColumn(store);
140 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
141 return this->UpdatelPathColumn(store);
142 }
143
144 /**
145 * @brief Upgrade the lpath column in PhotoAlbum table.
146 */
UpdatelPathColumn(NativeRdb::RdbStore & store)147 int32_t MediaLibraryDbUpgrade::UpdatelPathColumn(NativeRdb::RdbStore &store)
148 {
149 std::string executeSql = this->SQL_PHOTO_ALBUM_TABLE_UPDATE_LPATH_COLUMN;
150 std::vector<NativeRdb::ValueObject> args;
151 int32_t ret = ExecSqlWithRetry([&]() { return store.ExecuteSql(executeSql); });
152 if (ret != NativeRdb::E_OK) {
153 MEDIA_ERR_LOG("Media_Restore: MediaLibraryDbUpgrade::UpdatelPathColumn failed, ret=%{public}d, sql=%{public}s",
154 ret,
155 executeSql.c_str());
156 return ret;
157 }
158 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::UpdatelPathColumn success");
159 return ret;
160 }
161
162 /**
163 * @brief Add owner_album_id column to Photos table.
164 */
AddOwnerAlbumIdColumn(NativeRdb::RdbStore & store)165 int32_t MediaLibraryDbUpgrade::AddOwnerAlbumIdColumn(NativeRdb::RdbStore &store)
166 {
167 if (this->dbUpgradeUtils_.IsColumnExists(store, "Photos", "owner_album_id")) {
168 return NativeRdb::E_OK;
169 }
170
171 std::vector<NativeRdb::ValueObject> args;
172 std::string sql = this->SQL_PHOTOS_TABLE_ADD_OWNER_ALBUM_ID;
173 int32_t ret = ExecSqlWithRetry([&]() { return store.ExecuteSql(sql, args); });
174 CHECK_AND_PRINT_LOG(ret == NativeRdb::E_OK,
175 "Media_Restore: MediaLibraryDbUpgrade::AddOwnerAlbumIdColumn failed, ret=%{public}d, sql=%{public}s",
176 ret, sql.c_str());
177 MEDIA_INFO_LOG("Media_Restore: MediaLibraryDbUpgrade::AddOwnerAlbumIdColumn success");
178 return ret;
179 }
180
181 /**
182 * @brief Add lpath column to PhotoAlbum table.
183 */
AddlPathColumn(NativeRdb::RdbStore & store)184 int32_t MediaLibraryDbUpgrade::AddlPathColumn(NativeRdb::RdbStore &store)
185 {
186 CHECK_AND_RETURN_RET(!(this->dbUpgradeUtils_.IsColumnExists(store, "PhotoAlbum", "lpath")), NativeRdb::E_OK);
187 std::vector<NativeRdb::ValueObject> args;
188 std::string sql = this->SQL_PHOTO_ALBUM_TABLE_ADD_LPATH_COLUMN;
189 return ExecSqlWithRetry([&]() { return store.ExecuteSql(sql, args); });
190 }
191
192 /**
193 * @brief Move the single relationship from PhotoMap table to Photos table, stored in owner_album_id.
194 */
MoveSingleRelationshipToPhotos(NativeRdb::RdbStore & store)195 int32_t MediaLibraryDbUpgrade::MoveSingleRelationshipToPhotos(NativeRdb::RdbStore &store)
196 {
197 std::vector<std::string> executeSqls = {this->SQL_TEMP_PHOTO_MAP_TABLE_DROP, this->SQL_TEMP_PHOTO_MAP_TABLE_CREATE};
198 executeSqls.insert(executeSqls.end(),
199 this->SQL_TEMP_PHOTO_MAP_TABLE_CREATE_INDEX_ARRAY.begin(),
200 this->SQL_TEMP_PHOTO_MAP_TABLE_CREATE_INDEX_ARRAY.end());
201 executeSqls.push_back(this->SQL_PHOTOS_TABLE_UPDATE_ALBUM_ID);
202 executeSqls.push_back(this->SQL_PHOTO_MAP_TABLE_DELETE_SINGLE_RELATIONSHIP);
203 executeSqls.push_back(this->SQL_TEMP_PHOTO_MAP_TABLE_DROP);
204 int ret = NativeRdb::E_OK;
205 MEDIA_INFO_LOG("MoveSingleRelationshipToPhotos begin");
206 auto [errCode, transaction] = store.CreateTransaction(OHOS::NativeRdb::Transaction::DEFERRED);
207 DfxTransaction reporter{__func__};
208 if (errCode != NativeRdb::E_OK || transaction == nullptr) {
209 reporter.ReportError(DfxTransaction::AbnormalType::CREATE_ERROR, errCode);
210 MEDIA_ERR_LOG("transaction failed, err:%{public}d", errCode);
211 return errCode;
212 }
213 for (const std::string &executeSql : executeSqls) {
214 auto res = transaction->Execute(executeSql);
215 ret = res.first;
216 if (ret != NativeRdb::E_OK) {
217 reporter.ReportError(DfxTransaction::AbnormalType::EXECUTE_ERROR, ret);
218 MEDIA_ERR_LOG("Media_Restore: MoveSingleRelationshipToPhotos failed, ret=%{public}d, sql=%{public}s",
219 ret,
220 executeSql.c_str());
221 transaction->Rollback();
222 return ret;
223 }
224 }
225 ret = transaction->Commit();
226 if (ret != NativeRdb::E_OK) {
227 reporter.ReportError(DfxTransaction::AbnormalType::COMMIT_ERROR, ret);
228 MEDIA_ERR_LOG("MoveSingleRelationshipToPhotos: tans finish fail!, ret:%{public}d", ret);
229 transaction->Rollback();
230 } else {
231 reporter.ReportIfTimeout();
232 }
233 return NativeRdb::E_OK;
234 }
235
236 /**
237 * @brief Upgrade Photos table.
238 */
UpgradePhotos(NativeRdb::RdbStore & store)239 int32_t MediaLibraryDbUpgrade::UpgradePhotos(NativeRdb::RdbStore &store)
240 {
241 int32_t ret = NativeRdb::E_OK;
242 ret = this->dbUpgradeUtils_.DropAllTriggers(store, "Photos");
243 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
244
245 ret = this->AddOwnerAlbumIdColumn(store);
246 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
247
248 ret = ExecSqlWithRetry([&]() { return PhotoDayMonthYearOperation::UpdatePhotosDate(store); });
249 CHECK_AND_RETURN_RET_LOG(ret == NativeRdb::E_OK, ret, "update photos date failed, ret=%{public}d", ret);
250
251 return NativeRdb::E_OK;
252 }
253
254 /**
255 * @brief Upgrade PhotoMap table.
256 */
UpgradePhotoMap(NativeRdb::RdbStore & store)257 int32_t MediaLibraryDbUpgrade::UpgradePhotoMap(NativeRdb::RdbStore &store)
258 {
259 int32_t ret = NativeRdb::E_OK;
260 ret = this->dbUpgradeUtils_.DropAllTriggers(store, "PhotoMap");
261 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
262
263 ret = this->MoveSingleRelationshipToPhotos(store);
264 CHECK_AND_RETURN_RET(ret == NativeRdb::E_OK, ret);
265 return NativeRdb::E_OK;
266 }
267 } // namespace DataTransfer
268 } // namespace OHOS::Media