1 package expo.modules.updates.db
2 
3 import expo.modules.updates.db.entity.AssetEntity
4 import expo.modules.updates.db.entity.UpdateEntity
5 import expo.modules.updates.db.enums.UpdateStatus
6 import java.io.File
7 
8 open class DatabaseIntegrityCheck {
runnull9   fun run(database: UpdatesDatabase, updatesDirectory: File?, embeddedUpdate: UpdateEntity?) {
10     val assets = database.assetDao().loadAllAssets()
11 
12     val missingAssets = mutableListOf<AssetEntity>()
13     for (asset in assets) {
14       if (asset.relativePath == null || !assetExists(asset, updatesDirectory)) {
15         missingAssets.add(asset)
16       }
17     }
18 
19     if (missingAssets.size > 0) {
20       database.updateDao().markUpdatesWithMissingAssets(missingAssets)
21     }
22 
23     val updatesToDelete = mutableListOf<UpdateEntity>()
24     // we can't run any updates with the status EMBEDDED unless they match the current embedded update
25     val updatesWithEmbeddedStatus = database.updateDao().loadAllUpdatesWithStatus(UpdateStatus.EMBEDDED)
26     for (update in updatesWithEmbeddedStatus) {
27       if (embeddedUpdate == null || update.id != embeddedUpdate.id) {
28         updatesToDelete.add(update)
29       }
30     }
31 
32     if (updatesToDelete.size > 0) {
33       database.updateDao().deleteUpdates(updatesToDelete)
34     }
35   }
36 
assetExistsnull37   internal fun assetExists(asset: AssetEntity, updatesDirectory: File?): Boolean {
38     val path = File(updatesDirectory, asset.relativePath)
39     return path.exists()
40   }
41 }
42