That fuck shit the fascists are using
1package org.tm.archive.database
2
3import android.app.Application
4import androidx.test.ext.junit.runners.AndroidJUnit4
5import junit.framework.TestCase.assertTrue
6import net.zetetic.database.sqlcipher.SQLiteDatabase
7import net.zetetic.database.sqlcipher.SQLiteOpenHelper
8import org.junit.Rule
9import org.junit.Test
10import org.junit.runner.RunWith
11import org.signal.core.util.ForeignKeyConstraint
12import org.signal.core.util.Index
13import org.signal.core.util.getForeignKeys
14import org.signal.core.util.getIndexes
15import org.signal.core.util.readToList
16import org.signal.core.util.requireNonNullString
17import org.tm.archive.database.helpers.SignalDatabaseMigrations
18import org.tm.archive.dependencies.ApplicationDependencies
19import org.tm.archive.testing.SignalActivityRule
20
21/**
22 * A test that guarantees that a freshly-created database looks the same as one that went through the upgrade path.
23 */
24@RunWith(AndroidJUnit4::class)
25class DatabaseConsistencyTest {
26
27 @get:Rule
28 val harness = SignalActivityRule()
29
30 @Test
31 fun testUpgradeConsistency() {
32 val currentVersionStatements = SignalDatabase.rawDatabase.getAllCreateStatements()
33 val testHelper = InMemoryTestHelper(ApplicationDependencies.getApplication()).also {
34 it.onUpgrade(it.writableDatabase, 181, SignalDatabaseMigrations.DATABASE_VERSION)
35 }
36
37 val upgradedStatements = testHelper.readableDatabase.getAllCreateStatements()
38
39 if (currentVersionStatements != upgradedStatements) {
40 var message = "\n"
41
42 val currentByName = currentVersionStatements.associateBy { it.name }
43 val upgradedByName = upgradedStatements.associateBy { it.name }
44
45 if (currentByName.keys != upgradedByName.keys) {
46 val exclusiveToCurrent = currentByName.keys - upgradedByName.keys
47 val exclusiveToUpgrade = upgradedByName.keys - currentByName.keys
48
49 message += "SQL entities exclusive to the newly-created database: $exclusiveToCurrent\n"
50 message += "SQL entities exclusive to the upgraded database: $exclusiveToUpgrade\n\n"
51 } else {
52 for (currentEntry in currentByName) {
53 val upgradedValue: Statement = upgradedByName[currentEntry.key]!!
54 if (upgradedValue.sql != currentEntry.value.sql) {
55 message += "Statement differed:\n"
56 message += "newly-created:\n"
57 message += "${currentEntry.value.sql}\n\n"
58 message += "upgraded:\n"
59 message += "${upgradedValue.sql}\n\n"
60 }
61 }
62 }
63
64 assertTrue(message, false)
65 }
66 }
67
68 @Test
69 fun testForeignKeyIndexCoverage() {
70 /** We may deem certain indexes non-critical if deletion frequency is low or table size is small. */
71 val ignoredColumns: List<Pair<String, String>> = listOf(
72 StorySendTable.TABLE_NAME to StorySendTable.DISTRIBUTION_ID
73 )
74
75 val foreignKeys: List<ForeignKeyConstraint> = SignalDatabase.rawDatabase.getForeignKeys()
76 val indexesByFirstColumn: List<Index> = SignalDatabase.rawDatabase.getIndexes()
77
78 val notFound: List<Pair<String, String>> = foreignKeys
79 .filterNot { ignoredColumns.contains(it.table to it.column) }
80 .filterNot { foreignKey ->
81 indexesByFirstColumn.hasPrimaryIndexFor(foreignKey.table, foreignKey.column)
82 }
83 .map { it.table to it.column }
84
85 assertTrue("Missing indexes to cover: $notFound", notFound.isEmpty())
86 }
87
88 private fun List<Index>.hasPrimaryIndexFor(table: String, column: String): Boolean {
89 return this.any { index -> index.table == table && index.columns[0] == column }
90 }
91
92 private data class Statement(
93 val name: String,
94 val sql: String
95 )
96
97 private fun SQLiteDatabase.getAllCreateStatements(): List<Statement> {
98 return this.rawQuery("SELECT name, sql FROM sqlite_schema WHERE sql NOT NULL AND name != 'sqlite_sequence'")
99 .readToList { cursor ->
100 Statement(
101 name = cursor.requireNonNullString("name"),
102 sql = cursor.requireNonNullString("sql").normalizeSql()
103 )
104 }
105 .filterNot { it.name.startsWith("sqlite_stat") }
106 .sortedBy { it.name }
107 }
108
109 private fun String.normalizeSql(): String {
110 return this
111 .split("\n")
112 .map { it.trim() }
113 .joinToString(separator = " ")
114 .replace(Regex.fromLiteral(" ,"), ",")
115 .replace(Regex("\\s+"), " ")
116 .replace(Regex.fromLiteral("( "), "(")
117 .replace(Regex.fromLiteral(" )"), ")")
118 .replace(Regex("CREATE TABLE \"([a-zA-Z_]+)\""), "CREATE TABLE $1") // for some reason SQLite will wrap table names in quotes for upgraded tables. This unwraps them.
119 }
120
121 private class InMemoryTestHelper(private val application: Application) : SQLiteOpenHelper(application, null, null, 1) {
122
123 override fun onCreate(db: SQLiteDatabase) {
124 for (statement in SNAPSHOT_V181) {
125 db.execSQL(statement.sql)
126 }
127 }
128
129 override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
130 SignalDatabaseMigrations.migrate(application, db, 181, SignalDatabaseMigrations.DATABASE_VERSION)
131 }
132
133 /**
134 * This is the list of statements that existed at version 181. Never change this.
135 */
136 private val SNAPSHOT_V181 = listOf(
137 Statement(
138 name = "message",
139 sql = "CREATE TABLE message (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n date_sent INTEGER NOT NULL,\n date_received INTEGER NOT NULL,\n date_server INTEGER DEFAULT -1,\n thread_id INTEGER NOT NULL REFERENCES thread (_id) ON DELETE CASCADE,\n recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,\n recipient_device_id INTEGER,\n type INTEGER NOT NULL,\n body TEXT,\n read INTEGER DEFAULT 0,\n ct_l TEXT,\n exp INTEGER,\n m_type INTEGER,\n m_size INTEGER,\n st INTEGER,\n tr_id TEXT,\n subscription_id INTEGER DEFAULT -1, \n receipt_timestamp INTEGER DEFAULT -1, \n delivery_receipt_count INTEGER DEFAULT 0, \n read_receipt_count INTEGER DEFAULT 0, \n viewed_receipt_count INTEGER DEFAULT 0,\n mismatched_identities TEXT DEFAULT NULL,\n network_failures TEXT DEFAULT NULL,\n expires_in INTEGER DEFAULT 0,\n expire_started INTEGER DEFAULT 0,\n notified INTEGER DEFAULT 0,\n quote_id INTEGER DEFAULT 0,\n quote_author INTEGER DEFAULT 0,\n quote_body TEXT DEFAULT NULL,\n quote_missing INTEGER DEFAULT 0,\n quote_mentions BLOB DEFAULT NULL,\n quote_type INTEGER DEFAULT 0,\n shared_contacts TEXT DEFAULT NULL,\n unidentified INTEGER DEFAULT 0,\n link_previews TEXT DEFAULT NULL,\n view_once INTEGER DEFAULT 0,\n reactions_unread INTEGER DEFAULT 0,\n reactions_last_seen INTEGER DEFAULT -1,\n remote_deleted INTEGER DEFAULT 0,\n mentions_self INTEGER DEFAULT 0,\n notified_timestamp INTEGER DEFAULT 0,\n server_guid TEXT DEFAULT NULL,\n message_ranges BLOB DEFAULT NULL,\n story_type INTEGER DEFAULT 0,\n parent_story_id INTEGER DEFAULT 0,\n export_state BLOB DEFAULT NULL,\n exported INTEGER DEFAULT 0,\n scheduled_date INTEGER DEFAULT -1\n )"
140 ),
141 Statement(
142 name = "part",
143 sql = "CREATE TABLE part (_id INTEGER PRIMARY KEY, mid INTEGER, seq INTEGER DEFAULT 0, ct TEXT, name TEXT, chset INTEGER, cd TEXT, fn TEXT, cid TEXT, cl TEXT, ctt_s INTEGER, ctt_t TEXT, encrypted INTEGER, pending_push INTEGER, _data TEXT, data_size INTEGER, file_name TEXT, unique_id INTEGER NOT NULL, digest BLOB, fast_preflight_id TEXT, voice_note INTEGER DEFAULT 0, borderless INTEGER DEFAULT 0, video_gif INTEGER DEFAULT 0, data_random BLOB, quote INTEGER DEFAULT 0, width INTEGER DEFAULT 0, height INTEGER DEFAULT 0, caption TEXT DEFAULT NULL, sticker_pack_id TEXT DEFAULT NULL, sticker_pack_key DEFAULT NULL, sticker_id INTEGER DEFAULT -1, sticker_emoji STRING DEFAULT NULL, data_hash TEXT DEFAULT NULL, blur_hash TEXT DEFAULT NULL, transform_properties TEXT DEFAULT NULL, transfer_file TEXT DEFAULT NULL, display_order INTEGER DEFAULT 0, upload_timestamp INTEGER DEFAULT 0, cdn_number INTEGER DEFAULT 0)"
144 ),
145 Statement(
146 name = "thread",
147 sql = "CREATE TABLE thread (\n _id INTEGER PRIMARY KEY AUTOINCREMENT, \n date INTEGER DEFAULT 0, \n meaningful_messages INTEGER DEFAULT 0,\n recipient_id INTEGER NOT NULL UNIQUE REFERENCES recipient (_id) ON DELETE CASCADE,\n read INTEGER DEFAULT 1, \n type INTEGER DEFAULT 0, \n error INTEGER DEFAULT 0, \n snippet TEXT, \n snippet_type INTEGER DEFAULT 0, \n snippet_uri TEXT DEFAULT NULL, \n snippet_content_type TEXT DEFAULT NULL, \n snippet_extras TEXT DEFAULT NULL, \n unread_count INTEGER DEFAULT 0, \n archived INTEGER DEFAULT 0, \n status INTEGER DEFAULT 0, \n delivery_receipt_count INTEGER DEFAULT 0, \n read_receipt_count INTEGER DEFAULT 0, \n expires_in INTEGER DEFAULT 0, \n last_seen INTEGER DEFAULT 0, \n has_sent INTEGER DEFAULT 0, \n last_scrolled INTEGER DEFAULT 0, \n pinned INTEGER DEFAULT 0, \n unread_self_mention_count INTEGER DEFAULT 0\n)"
148 ),
149 Statement(
150 name = "identities",
151 sql = "CREATE TABLE identities (\n _id INTEGER PRIMARY KEY AUTOINCREMENT, \n address INTEGER UNIQUE, \n identity_key TEXT, \n first_use INTEGER DEFAULT 0, \n timestamp INTEGER DEFAULT 0, \n verified INTEGER DEFAULT 0, \n nonblocking_approval INTEGER DEFAULT 0\n )"
152 ),
153 Statement(
154 name = "drafts",
155 sql = "CREATE TABLE drafts (\n _id INTEGER PRIMARY KEY, \n thread_id INTEGER, \n type TEXT, \n value TEXT\n )"
156 ),
157 Statement(
158 name = "push",
159 sql = "CREATE TABLE push (_id INTEGER PRIMARY KEY, type INTEGER, source TEXT, source_uuid TEXT, device_id INTEGER, body TEXT, content TEXT, timestamp INTEGER, server_timestamp INTEGER DEFAULT 0, server_delivered_timestamp INTEGER DEFAULT 0, server_guid TEXT DEFAULT NULL)"
160 ),
161 Statement(
162 name = "groups",
163 sql = "CREATE TABLE groups (\n _id INTEGER PRIMARY KEY, \n group_id TEXT, \n recipient_id INTEGER,\n title TEXT,\n avatar_id INTEGER, \n avatar_key BLOB,\n avatar_content_type TEXT, \n avatar_relay TEXT,\n timestamp INTEGER,\n active INTEGER DEFAULT 1,\n avatar_digest BLOB, \n mms INTEGER DEFAULT 0, \n master_key BLOB, \n revision BLOB, \n decrypted_group BLOB, \n expected_v2_id TEXT DEFAULT NULL, \n former_v1_members TEXT DEFAULT NULL, \n distribution_id TEXT DEFAULT NULL, \n display_as_story INTEGER DEFAULT 0, \n auth_service_id TEXT DEFAULT NULL, \n last_force_update_timestamp INTEGER DEFAULT 0\n )"
164 ),
165 Statement(
166 name = "group_membership",
167 sql = "CREATE TABLE group_membership ( _id INTEGER PRIMARY KEY, group_id TEXT NOT NULL, recipient_id INTEGER NOT NULL, UNIQUE(group_id, recipient_id) )"
168 ),
169 Statement(
170 name = "recipient",
171 sql = "CREATE TABLE recipient (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n uuid TEXT UNIQUE DEFAULT NULL,\n username TEXT UNIQUE DEFAULT NULL,\n phone TEXT UNIQUE DEFAULT NULL,\n email TEXT UNIQUE DEFAULT NULL,\n group_id TEXT UNIQUE DEFAULT NULL,\n group_type INTEGER DEFAULT 0,\n blocked INTEGER DEFAULT 0,\n message_ringtone TEXT DEFAULT NULL, \n message_vibrate INTEGER DEFAULT 0, \n call_ringtone TEXT DEFAULT NULL, \n call_vibrate INTEGER DEFAULT 0, \n notification_channel TEXT DEFAULT NULL, \n mute_until INTEGER DEFAULT 0, \n color TEXT DEFAULT NULL, \n seen_invite_reminder INTEGER DEFAULT 0,\n default_subscription_id INTEGER DEFAULT -1,\n message_expiration_time INTEGER DEFAULT 0,\n registered INTEGER DEFAULT 0,\n system_given_name TEXT DEFAULT NULL, \n system_family_name TEXT DEFAULT NULL, \n system_display_name TEXT DEFAULT NULL, \n system_photo_uri TEXT DEFAULT NULL, \n system_phone_label TEXT DEFAULT NULL, \n system_phone_type INTEGER DEFAULT -1, \n system_contact_uri TEXT DEFAULT NULL, \n system_info_pending INTEGER DEFAULT 0, \n profile_key TEXT DEFAULT NULL, \n profile_key_credential TEXT DEFAULT NULL, \n signal_profile_name TEXT DEFAULT NULL, \n profile_family_name TEXT DEFAULT NULL, \n profile_joined_name TEXT DEFAULT NULL, \n signal_profile_avatar TEXT DEFAULT NULL, \n profile_sharing INTEGER DEFAULT 0, \n last_profile_fetch INTEGER DEFAULT 0, \n unidentified_access_mode INTEGER DEFAULT 0, \n force_sms_selection INTEGER DEFAULT 0, \n storage_service_key TEXT UNIQUE DEFAULT NULL, \n mention_setting INTEGER DEFAULT 0, \n storage_proto TEXT DEFAULT NULL,\n capabilities INTEGER DEFAULT 0,\n last_session_reset BLOB DEFAULT NULL,\n wallpaper BLOB DEFAULT NULL,\n wallpaper_file TEXT DEFAULT NULL,\n about TEXT DEFAULT NULL,\n about_emoji TEXT DEFAULT NULL,\n extras BLOB DEFAULT NULL,\n groups_in_common INTEGER DEFAULT 0,\n chat_colors BLOB DEFAULT NULL,\n custom_chat_colors_id INTEGER DEFAULT 0,\n badges BLOB DEFAULT NULL,\n pni TEXT DEFAULT NULL,\n distribution_list_id INTEGER DEFAULT NULL,\n needs_pni_signature INTEGER DEFAULT 0,\n unregistered_timestamp INTEGER DEFAULT 0,\n hidden INTEGER DEFAULT 0,\n reporting_token BLOB DEFAULT NULL,\n system_nickname TEXT DEFAULT NULL\n)"
172 ),
173 Statement(
174 name = "group_receipts",
175 sql = "CREATE TABLE group_receipts (\n _id INTEGER PRIMARY KEY, \n mms_id INTEGER, \n address INTEGER, \n status INTEGER, \n timestamp INTEGER, \n unidentified INTEGER DEFAULT 0\n )"
176 ),
177 Statement(
178 name = "one_time_prekeys",
179 sql = "CREATE TABLE one_time_prekeys (\n _id INTEGER PRIMARY KEY,\n account_id TEXT NOT NULL,\n key_id INTEGER UNIQUE, \n public_key TEXT NOT NULL, \n private_key TEXT NOT NULL,\n UNIQUE(account_id, key_id)\n )"
180 ),
181 Statement(
182 name = "signed_prekeys",
183 sql = "CREATE TABLE signed_prekeys (\n _id INTEGER PRIMARY KEY,\n account_id TEXT NOT NULL,\n key_id INTEGER UNIQUE, \n public_key TEXT NOT NULL,\n private_key TEXT NOT NULL,\n signature TEXT NOT NULL, \n timestamp INTEGER DEFAULT 0,\n UNIQUE(account_id, key_id)\n )"
184 ),
185 Statement(
186 name = "sessions",
187 sql = "CREATE TABLE sessions (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id TEXT NOT NULL,\n address TEXT NOT NULL,\n device INTEGER NOT NULL,\n record BLOB NOT NULL,\n UNIQUE(account_id, address, device)\n )"
188 ),
189 Statement(
190 name = "sender_keys",
191 sql = "CREATE TABLE sender_keys (\n _id INTEGER PRIMARY KEY AUTOINCREMENT, \n address TEXT NOT NULL, \n device INTEGER NOT NULL, \n distribution_id TEXT NOT NULL,\n record BLOB NOT NULL, \n created_at INTEGER NOT NULL, \n UNIQUE(address,device, distribution_id) ON CONFLICT REPLACE\n )"
192 ),
193 Statement(
194 name = "sender_key_shared",
195 sql = "CREATE TABLE sender_key_shared (\n _id INTEGER PRIMARY KEY AUTOINCREMENT, \n distribution_id TEXT NOT NULL, \n address TEXT NOT NULL, \n device INTEGER NOT NULL, \n timestamp INTEGER DEFAULT 0, \n UNIQUE(distribution_id,address, device) ON CONFLICT REPLACE\n )"
196 ),
197 Statement(
198 name = "pending_retry_receipts",
199 sql = "CREATE TABLE pending_retry_receipts(_id INTEGER PRIMARY KEY AUTOINCREMENT, author TEXT NOT NULL, device INTEGER NOT NULL, sent_timestamp INTEGER NOT NULL, received_timestamp TEXT NOT NULL, thread_id INTEGER NOT NULL, UNIQUE(author,sent_timestamp) ON CONFLICT REPLACE)"
200 ),
201 Statement(
202 name = "sticker",
203 sql = "CREATE TABLE sticker (_id INTEGER PRIMARY KEY AUTOINCREMENT, pack_id TEXT NOT NULL, pack_key TEXT NOT NULL, pack_title TEXT NOT NULL, pack_author TEXT NOT NULL, sticker_id INTEGER, cover INTEGER, pack_order INTEGER, emoji TEXT NOT NULL, content_type TEXT DEFAULT NULL, last_used INTEGER, installed INTEGER,file_path TEXT NOT NULL, file_length INTEGER, file_random BLOB, UNIQUE(pack_id, sticker_id, cover) ON CONFLICT IGNORE)"
204 ),
205 Statement(
206 name = "storage_key",
207 sql = "CREATE TABLE storage_key (_id INTEGER PRIMARY KEY AUTOINCREMENT, type INTEGER, key TEXT UNIQUE)"
208 ),
209 Statement(
210 name = "mention",
211 sql = "CREATE TABLE mention(_id INTEGER PRIMARY KEY AUTOINCREMENT, thread_id INTEGER, message_id INTEGER, recipient_id INTEGER, range_start INTEGER, range_length INTEGER)"
212 ),
213 Statement(
214 name = "payments",
215 sql = "CREATE TABLE payments(_id INTEGER PRIMARY KEY, uuid TEXT DEFAULT NULL, recipient INTEGER DEFAULT 0, recipient_address TEXT DEFAULT NULL, timestamp INTEGER, note TEXT DEFAULT NULL, direction INTEGER, state INTEGER, failure_reason INTEGER, amount BLOB NOT NULL, fee BLOB NOT NULL, transaction_record BLOB DEFAULT NULL, receipt BLOB DEFAULT NULL, payment_metadata BLOB DEFAULT NULL, receipt_public_key TEXT DEFAULT NULL, block_index INTEGER DEFAULT 0, block_timestamp INTEGER DEFAULT 0, seen INTEGER, UNIQUE(uuid) ON CONFLICT ABORT)"
216 ),
217 Statement(
218 name = "chat_colors",
219 sql = "CREATE TABLE chat_colors (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n chat_colors BLOB\n)"
220 ),
221 Statement(
222 name = "emoji_search",
223 sql = "CREATE TABLE emoji_search (\n _id INTEGER PRIMARY KEY,\n label TEXT NOT NULL,\n emoji TEXT NOT NULL,\n rank INTEGER DEFAULT 2147483647 \n )"
224 ),
225 Statement(
226 name = "avatar_picker",
227 sql = "CREATE TABLE avatar_picker (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n last_used INTEGER DEFAULT 0,\n group_id TEXT DEFAULT NULL,\n avatar BLOB NOT NULL\n)"
228 ),
229 Statement(
230 name = "group_call_ring",
231 sql = "CREATE TABLE group_call_ring (\n _id INTEGER PRIMARY KEY,\n ring_id INTEGER UNIQUE,\n date_received INTEGER,\n ring_state INTEGER\n)"
232 ),
233 Statement(
234 name = "reaction",
235 sql = "CREATE TABLE reaction (\n _id INTEGER PRIMARY KEY,\n message_id INTEGER NOT NULL REFERENCES message (_id) ON DELETE CASCADE,\n author_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,\n emoji TEXT NOT NULL,\n date_sent INTEGER NOT NULL,\n date_received INTEGER NOT NULL,\n UNIQUE(message_id, author_id) ON CONFLICT REPLACE\n)"
236 ),
237 Statement(
238 name = "donation_receipt",
239 sql = "CREATE TABLE donation_receipt (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n receipt_type TEXT NOT NULL,\n receipt_date INTEGER NOT NULL,\n amount TEXT NOT NULL,\n currency TEXT NOT NULL,\n subscription_level INTEGER NOT NULL\n)"
240 ),
241 Statement(
242 name = "story_sends",
243 sql = "CREATE TABLE story_sends (\n _id INTEGER PRIMARY KEY,\n message_id INTEGER NOT NULL REFERENCES message (_id) ON DELETE CASCADE,\n recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,\n sent_timestamp INTEGER NOT NULL,\n allows_replies INTEGER NOT NULL,\n distribution_id TEXT NOT NULL REFERENCES distribution_list (distribution_id) ON DELETE CASCADE\n)"
244 ),
245 Statement(
246 name = "cds",
247 sql = "CREATE TABLE cds (\n _id INTEGER PRIMARY KEY,\n e164 TEXT NOT NULL UNIQUE ON CONFLICT IGNORE,\n last_seen_at INTEGER DEFAULT 0\n )"
248 ),
249 Statement(
250 name = "remote_megaphone",
251 sql = "CREATE TABLE remote_megaphone (\n _id INTEGER PRIMARY KEY,\n uuid TEXT UNIQUE NOT NULL,\n priority INTEGER NOT NULL,\n countries TEXT,\n minimum_version INTEGER NOT NULL,\n dont_show_before INTEGER NOT NULL,\n dont_show_after INTEGER NOT NULL,\n show_for_days INTEGER NOT NULL,\n conditional_id TEXT,\n primary_action_id TEXT,\n secondary_action_id TEXT,\n image_url TEXT,\n image_uri TEXT DEFAULT NULL,\n title TEXT NOT NULL,\n body TEXT NOT NULL,\n primary_action_text TEXT,\n secondary_action_text TEXT,\n shown_at INTEGER DEFAULT 0,\n finished_at INTEGER DEFAULT 0,\n primary_action_data TEXT DEFAULT NULL,\n secondary_action_data TEXT DEFAULT NULL,\n snoozed_at INTEGER DEFAULT 0,\n seen_count INTEGER DEFAULT 0\n)"
252 ),
253 Statement(
254 name = "pending_pni_signature_message",
255 sql = "CREATE TABLE pending_pni_signature_message (\n _id INTEGER PRIMARY KEY,\n recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,\n sent_timestamp INTEGER NOT NULL,\n device_id INTEGER NOT NULL\n )"
256 ),
257 Statement(
258 name = "call",
259 sql = "CREATE TABLE call (\n _id INTEGER PRIMARY KEY,\n call_id INTEGER NOT NULL UNIQUE,\n message_id INTEGER NOT NULL REFERENCES message (_id) ON DELETE CASCADE,\n peer INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,\n type INTEGER NOT NULL,\n direction INTEGER NOT NULL,\n event INTEGER NOT NULL\n)"
260 ),
261 Statement(
262 name = "message_fts",
263 sql = "CREATE VIRTUAL TABLE message_fts USING fts5(body, thread_id UNINDEXED, content=message, content_rowid=_id)"
264 ),
265 Statement(
266 name = "remapped_recipients",
267 sql = "CREATE TABLE remapped_recipients (\n _id INTEGER PRIMARY KEY AUTOINCREMENT, \n old_id INTEGER UNIQUE, \n new_id INTEGER\n )"
268 ),
269 Statement(
270 name = "remapped_threads",
271 sql = "CREATE TABLE remapped_threads (\n _id INTEGER PRIMARY KEY AUTOINCREMENT, \n old_id INTEGER UNIQUE, \n new_id INTEGER\n )"
272 ),
273 Statement(
274 name = "msl_payload",
275 sql = "CREATE TABLE msl_payload (\n _id INTEGER PRIMARY KEY,\n date_sent INTEGER NOT NULL,\n content BLOB NOT NULL,\n content_hint INTEGER NOT NULL,\n urgent INTEGER NOT NULL DEFAULT 1\n )"
276 ),
277 Statement(
278 name = "msl_recipient",
279 sql = "CREATE TABLE msl_recipient (\n _id INTEGER PRIMARY KEY,\n payload_id INTEGER NOT NULL REFERENCES msl_payload (_id) ON DELETE CASCADE,\n recipient_id INTEGER NOT NULL, \n device INTEGER NOT NULL\n )"
280 ),
281 Statement(
282 name = "msl_message",
283 sql = "CREATE TABLE msl_message (\n _id INTEGER PRIMARY KEY,\n payload_id INTEGER NOT NULL REFERENCES msl_payload (_id) ON DELETE CASCADE,\n message_id INTEGER NOT NULL\n )"
284 ),
285 Statement(
286 name = "notification_profile",
287 sql = "CREATE TABLE notification_profile (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL UNIQUE,\n emoji TEXT NOT NULL,\n color TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n allow_all_calls INTEGER NOT NULL DEFAULT 0,\n allow_all_mentions INTEGER NOT NULL DEFAULT 0\n)"
288 ),
289 Statement(
290 name = "notification_profile_schedule",
291 sql = "CREATE TABLE notification_profile_schedule (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n notification_profile_id INTEGER NOT NULL REFERENCES notification_profile (_id) ON DELETE CASCADE,\n enabled INTEGER NOT NULL DEFAULT 0,\n start INTEGER NOT NULL,\n end INTEGER NOT NULL,\n days_enabled TEXT NOT NULL\n)"
292 ),
293 Statement(
294 name = "notification_profile_allowed_members",
295 sql = "CREATE TABLE notification_profile_allowed_members (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n notification_profile_id INTEGER NOT NULL REFERENCES notification_profile (_id) ON DELETE CASCADE,\n recipient_id INTEGER NOT NULL,\n UNIQUE(notification_profile_id, recipient_id) ON CONFLICT REPLACE\n)"
296 ),
297 Statement(
298 name = "distribution_list",
299 sql = "CREATE TABLE distribution_list (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT UNIQUE NOT NULL,\n distribution_id TEXT UNIQUE NOT NULL,\n recipient_id INTEGER UNIQUE REFERENCES recipient (_id),\n allows_replies INTEGER DEFAULT 1,\n deletion_timestamp INTEGER DEFAULT 0,\n is_unknown INTEGER DEFAULT 0,\n privacy_mode INTEGER DEFAULT 0\n )"
300 ),
301 Statement(
302 name = "distribution_list_member",
303 sql = "CREATE TABLE distribution_list_member (\n _id INTEGER PRIMARY KEY AUTOINCREMENT,\n list_id INTEGER NOT NULL REFERENCES distribution_list (_id) ON DELETE CASCADE,\n recipient_id INTEGER NOT NULL REFERENCES recipient (_id),\n privacy_mode INTEGER DEFAULT 0\n )"
304 ),
305 Statement(
306 name = "recipient_group_type_index",
307 sql = "CREATE INDEX recipient_group_type_index ON recipient (group_type)"
308 ),
309 Statement(
310 name = "recipient_pni_index",
311 sql = "CREATE UNIQUE INDEX recipient_pni_index ON recipient (pni)"
312 ),
313 Statement(
314 name = "recipient_service_id_profile_key",
315 sql = "CREATE INDEX recipient_service_id_profile_key ON recipient (uuid, profile_key) WHERE uuid NOT NULL AND profile_key NOT NULL"
316 ),
317 Statement(
318 name = "mms_read_and_notified_and_thread_id_index",
319 sql = "CREATE INDEX mms_read_and_notified_and_thread_id_index ON message (read, notified, thread_id)"
320 ),
321 Statement(
322 name = "mms_type_index",
323 sql = "CREATE INDEX mms_type_index ON message (type)"
324 ),
325 Statement(
326 name = "mms_date_sent_index",
327 sql = "CREATE INDEX mms_date_sent_index ON message (date_sent, recipient_id, thread_id)"
328 ),
329 Statement(
330 name = "mms_date_server_index",
331 sql = "CREATE INDEX mms_date_server_index ON message (date_server)"
332 ),
333 Statement(
334 name = "mms_thread_date_index",
335 sql = "CREATE INDEX mms_thread_date_index ON message (thread_id, date_received)"
336 ),
337 Statement(
338 name = "mms_reactions_unread_index",
339 sql = "CREATE INDEX mms_reactions_unread_index ON message (reactions_unread)"
340 ),
341 Statement(
342 name = "mms_story_type_index",
343 sql = "CREATE INDEX mms_story_type_index ON message (story_type)"
344 ),
345 Statement(
346 name = "mms_parent_story_id_index",
347 sql = "CREATE INDEX mms_parent_story_id_index ON message (parent_story_id)"
348 ),
349 Statement(
350 name = "mms_thread_story_parent_story_scheduled_date_index",
351 sql = "CREATE INDEX mms_thread_story_parent_story_scheduled_date_index ON message (thread_id, date_received, story_type, parent_story_id, scheduled_date)"
352 ),
353 Statement(
354 name = "message_quote_id_quote_author_scheduled_date_index",
355 sql = "CREATE INDEX message_quote_id_quote_author_scheduled_date_index ON message (quote_id, quote_author, scheduled_date)"
356 ),
357 Statement(
358 name = "mms_exported_index",
359 sql = "CREATE INDEX mms_exported_index ON message (exported)"
360 ),
361 Statement(
362 name = "mms_id_type_payment_transactions_index",
363 sql = "CREATE INDEX mms_id_type_payment_transactions_index ON message (_id,type) WHERE type & 12884901888 != 0"
364 ),
365 Statement(
366 name = "part_mms_id_index",
367 sql = "CREATE INDEX part_mms_id_index ON part (mid)"
368 ),
369 Statement(
370 name = "pending_push_index",
371 sql = "CREATE INDEX pending_push_index ON part (pending_push)"
372 ),
373 Statement(
374 name = "part_sticker_pack_id_index",
375 sql = "CREATE INDEX part_sticker_pack_id_index ON part (sticker_pack_id)"
376 ),
377 Statement(
378 name = "part_data_hash_index",
379 sql = "CREATE INDEX part_data_hash_index ON part (data_hash)"
380 ),
381 Statement(
382 name = "part_data_index",
383 sql = "CREATE INDEX part_data_index ON part (_data)"
384 ),
385 Statement(
386 name = "thread_recipient_id_index",
387 sql = "CREATE INDEX thread_recipient_id_index ON thread (recipient_id)"
388 ),
389 Statement(
390 name = "archived_count_index",
391 sql = "CREATE INDEX archived_count_index ON thread (archived, meaningful_messages)"
392 ),
393 Statement(
394 name = "thread_pinned_index",
395 sql = "CREATE INDEX thread_pinned_index ON thread (pinned)"
396 ),
397 Statement(
398 name = "thread_read",
399 sql = "CREATE INDEX thread_read ON thread (read)"
400 ),
401 Statement(
402 name = "draft_thread_index",
403 sql = "CREATE INDEX draft_thread_index ON drafts (thread_id)"
404 ),
405 Statement(
406 name = "group_id_index",
407 sql = "CREATE UNIQUE INDEX group_id_index ON groups (group_id)"
408 ),
409 Statement(
410 name = "group_recipient_id_index",
411 sql = "CREATE UNIQUE INDEX group_recipient_id_index ON groups (recipient_id)"
412 ),
413 Statement(
414 name = "expected_v2_id_index",
415 sql = "CREATE UNIQUE INDEX expected_v2_id_index ON groups (expected_v2_id)"
416 ),
417 Statement(
418 name = "group_distribution_id_index",
419 sql = "CREATE UNIQUE INDEX group_distribution_id_index ON groups(distribution_id)"
420 ),
421 Statement(
422 name = "group_receipt_mms_id_index",
423 sql = "CREATE INDEX group_receipt_mms_id_index ON group_receipts (mms_id)"
424 ),
425 Statement(
426 name = "sticker_pack_id_index",
427 sql = "CREATE INDEX sticker_pack_id_index ON sticker (pack_id)"
428 ),
429 Statement(
430 name = "sticker_sticker_id_index",
431 sql = "CREATE INDEX sticker_sticker_id_index ON sticker (sticker_id)"
432 ),
433 Statement(
434 name = "storage_key_type_index",
435 sql = "CREATE INDEX storage_key_type_index ON storage_key (type)"
436 ),
437 Statement(
438 name = "mention_message_id_index",
439 sql = "CREATE INDEX mention_message_id_index ON mention (message_id)"
440 ),
441 Statement(
442 name = "mention_recipient_id_thread_id_index",
443 sql = "CREATE INDEX mention_recipient_id_thread_id_index ON mention (recipient_id, thread_id)"
444 ),
445 Statement(
446 name = "timestamp_direction_index",
447 sql = "CREATE INDEX timestamp_direction_index ON payments (timestamp, direction)"
448 ),
449 Statement(
450 name = "timestamp_index",
451 sql = "CREATE INDEX timestamp_index ON payments (timestamp)"
452 ),
453 Statement(
454 name = "receipt_public_key_index",
455 sql = "CREATE UNIQUE INDEX receipt_public_key_index ON payments (receipt_public_key)"
456 ),
457 Statement(
458 name = "msl_payload_date_sent_index",
459 sql = "CREATE INDEX msl_payload_date_sent_index ON msl_payload (date_sent)"
460 ),
461 Statement(
462 name = "msl_recipient_recipient_index",
463 sql = "CREATE INDEX msl_recipient_recipient_index ON msl_recipient (recipient_id, device, payload_id)"
464 ),
465 Statement(
466 name = "msl_recipient_payload_index",
467 sql = "CREATE INDEX msl_recipient_payload_index ON msl_recipient (payload_id)"
468 ),
469 Statement(
470 name = "msl_message_message_index",
471 sql = "CREATE INDEX msl_message_message_index ON msl_message (message_id, payload_id)"
472 ),
473 Statement(
474 name = "date_received_index",
475 sql = "CREATE INDEX date_received_index on group_call_ring (date_received)"
476 ),
477 Statement(
478 name = "notification_profile_schedule_profile_index",
479 sql = "CREATE INDEX notification_profile_schedule_profile_index ON notification_profile_schedule (notification_profile_id)"
480 ),
481 Statement(
482 name = "notification_profile_allowed_members_profile_index",
483 sql = "CREATE INDEX notification_profile_allowed_members_profile_index ON notification_profile_allowed_members (notification_profile_id)"
484 ),
485 Statement(
486 name = "donation_receipt_type_index",
487 sql = "CREATE INDEX donation_receipt_type_index ON donation_receipt (receipt_type)"
488 ),
489 Statement(
490 name = "donation_receipt_date_index",
491 sql = "CREATE INDEX donation_receipt_date_index ON donation_receipt (receipt_date)"
492 ),
493 Statement(
494 name = "story_sends_recipient_id_sent_timestamp_allows_replies_index",
495 sql = "CREATE INDEX story_sends_recipient_id_sent_timestamp_allows_replies_index ON story_sends (recipient_id, sent_timestamp, allows_replies)"
496 ),
497 Statement(
498 name = "story_sends_message_id_distribution_id_index",
499 sql = "CREATE INDEX story_sends_message_id_distribution_id_index ON story_sends (message_id, distribution_id)"
500 ),
501 Statement(
502 name = "distribution_list_member_list_id_recipient_id_privacy_mode_index",
503 sql = "CREATE UNIQUE INDEX distribution_list_member_list_id_recipient_id_privacy_mode_index ON distribution_list_member (list_id, recipient_id, privacy_mode)"
504 ),
505 Statement(
506 name = "pending_pni_recipient_sent_device_index",
507 sql = "CREATE UNIQUE INDEX pending_pni_recipient_sent_device_index ON pending_pni_signature_message (recipient_id, sent_timestamp, device_id)"
508 ),
509 Statement(
510 name = "call_call_id_index",
511 sql = "CREATE INDEX call_call_id_index ON call (call_id)"
512 ),
513 Statement(
514 name = "call_message_id_index",
515 sql = "CREATE INDEX call_message_id_index ON call (message_id)"
516 ),
517 Statement(
518 name = "message_ai",
519 sql = "CREATE TRIGGER message_ai AFTER INSERT ON message BEGIN\n INSERT INTO message_fts(rowid, body, thread_id) VALUES (new._id, new.body, new.thread_id);\n END"
520 ),
521 Statement(
522 name = "message_ad",
523 sql = "CREATE TRIGGER message_ad AFTER DELETE ON message BEGIN\n INSERT INTO message_fts(message_fts, rowid, body, thread_id) VALUES('delete', old._id, old.body, old.thread_id);\n END"
524 ),
525 Statement(
526 name = "message_au",
527 sql = "CREATE TRIGGER message_au AFTER UPDATE ON message BEGIN\n INSERT INTO message_fts(message_fts, rowid, body, thread_id) VALUES('delete', old._id, old.body, old.thread_id);\n INSERT INTO message_fts(rowid, body, thread_id) VALUES (new._id, new.body, new.thread_id);\n END"
528 ),
529 Statement(
530 name = "msl_message_delete",
531 sql = "CREATE TRIGGER msl_message_delete AFTER DELETE ON message \n BEGIN \n \tDELETE FROM msl_payload WHERE _id IN (SELECT payload_id FROM msl_message WHERE message_id = old._id);\n END"
532 ),
533 Statement(
534 name = "msl_attachment_delete",
535 sql = "CREATE TRIGGER msl_attachment_delete AFTER DELETE ON part\n BEGIN\n \tDELETE FROM msl_payload WHERE _id IN (SELECT payload_id FROM msl_message WHERE message_id = old.mid);\n END"
536 )
537 )
538 }
539}