1 package expo.modules.updates.db.entity
2 
3 import androidx.room.*
4 import expo.modules.updates.db.enums.UpdateStatus
5 import org.json.JSONObject
6 import java.util.*
7 
8 /**
9  * Data class that represents a (potential) row in the `updates` table in SQLite. The table schema
10  * is autogenerated from this file.
11  *
12  * expo-updates treats most fields (other than `status`, `keep`, `lastAccessed`, and the launch
13  * counts) as effectively immutable once in the database. This means an update server should never
14  * host two manifests with the same `id` that differ in any other field, as expo-updates will not
15  * take the difference into account.
16  *
17  * The `scopeKey` field is only relevant in environments such as Expo Go in which updates from
18  * multiple scopes can be launched.
19  */
20 @Entity(
21   tableName = "updates",
22   foreignKeys = [
23     ForeignKey(
24       entity = AssetEntity::class,
25       parentColumns = ["id"],
26       childColumns = ["launch_asset_id"],
27       onDelete = ForeignKey.CASCADE
28     )
29   ],
30   indices = [
31     Index(value = ["launch_asset_id"]),
32     Index(value = ["scope_key", "commit_time"], unique = true)
33   ]
34 )
35 class UpdateEntity(
36   @field:ColumnInfo(typeAffinity = ColumnInfo.BLOB) @field:PrimaryKey var id: UUID,
37   @field:ColumnInfo(name = "commit_time") var commitTime: Date,
38   @field:ColumnInfo(name = "runtime_version") var runtimeVersion: String,
39   @field:ColumnInfo(name = "scope_key") var scopeKey: String,
40   @field:ColumnInfo(name = "manifest") var manifest: JSONObject,
41 ) {
42   @ColumnInfo(name = "launch_asset_id")
43   var launchAssetId: Long? = null
44 
45   var status = UpdateStatus.PENDING
46 
47   var keep = false
48 
49   val loggingId: String
50     get() {
51       return id.toString().lowercase()
52     }
53 
54   @ColumnInfo(name = "last_accessed")
55   var lastAccessed: Date = Date()
56 
57   @ColumnInfo(name = "successful_launch_count", defaultValue = "0")
58   var successfulLaunchCount = 0
59 
60   @ColumnInfo(name = "failed_launch_count", defaultValue = "0")
61   var failedLaunchCount = 0
62 }
63