Skip to content

Commit

Permalink
[messages] expire old messages in pending state
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Feb 26, 2024
1 parent a86385d commit 2872aca
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 1 deletion.
144 changes: 144 additions & 0 deletions app/schemas/me.capcom.smsgateway.data.AppDatabase/8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"formatVersion": 1,
"database": {
"version": 8,
"identityHash": "0638620d8ed8717433cb8e718cfc4646",
"entities": [
{
"tableName": "Message",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `text` TEXT NOT NULL, `withDeliveryReport` INTEGER NOT NULL DEFAULT 1, `simNumber` INTEGER, `validUntil` TEXT, `isEncrypted` INTEGER NOT NULL DEFAULT 0, `skipPhoneValidation` INTEGER NOT NULL DEFAULT 0, `source` TEXT NOT NULL DEFAULT 'Local', `state` TEXT NOT NULL, `createdAt` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "text",
"columnName": "text",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "withDeliveryReport",
"columnName": "withDeliveryReport",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "1"
},
{
"fieldPath": "simNumber",
"columnName": "simNumber",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "validUntil",
"columnName": "validUntil",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "isEncrypted",
"columnName": "isEncrypted",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "skipPhoneValidation",
"columnName": "skipPhoneValidation",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "source",
"columnName": "source",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "'Local'"
},
{
"fieldPath": "state",
"columnName": "state",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "createdAt",
"columnName": "createdAt",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "MessageRecipient",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`messageId` TEXT NOT NULL, `phoneNumber` TEXT NOT NULL, `state` TEXT NOT NULL, `error` TEXT, PRIMARY KEY(`messageId`, `phoneNumber`), FOREIGN KEY(`messageId`) REFERENCES `Message`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "messageId",
"columnName": "messageId",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "phoneNumber",
"columnName": "phoneNumber",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "state",
"columnName": "state",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "error",
"columnName": "error",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"messageId",
"phoneNumber"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": [
{
"table": "Message",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"messageId"
],
"referencedColumns": [
"id"
]
}
]
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '0638620d8ed8717433cb8e718cfc4646')"
]
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/me/capcom/smsgateway/data/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import me.capcom.smsgateway.data.entities.MessageRecipient

@Database(
entities = [Message::class, MessageRecipient::class],
version = 7,
version = 8,
autoMigrations = [
AutoMigration(from = 1, to = 2),
AutoMigration(from = 2, to = 3),
Expand All @@ -32,6 +32,7 @@ abstract class AppDatabase : RoomDatabase() {
AppDatabase::class.java,
"gateway"
)
.addMigrations(MIGRATION_7_8)
.allowMainThreadQueries()
.build()
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/data/Migrations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.capcom.smsgateway.data

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
UPDATE message
SET validUntil = strftime('%FT%TZ', createdAt / 1000 + 86400, 'unixepoch')
WHERE validUntil IS NULL AND state = 'Pending'
""".trimIndent()
)
}
}

0 comments on commit 2872aca

Please sign in to comment.