From 2872aca28c46a3d8ca76ee457a00d3aecf48e414 Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Mon, 26 Feb 2024 23:58:17 +0700 Subject: [PATCH] [messages] expire old messages in pending state --- .../8.json | 144 ++++++++++++++++++ .../me/capcom/smsgateway/data/AppDatabase.kt | 3 +- .../me/capcom/smsgateway/data/Migrations.kt | 16 ++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 app/schemas/me.capcom.smsgateway.data.AppDatabase/8.json create mode 100644 app/src/main/java/me/capcom/smsgateway/data/Migrations.kt diff --git a/app/schemas/me.capcom.smsgateway.data.AppDatabase/8.json b/app/schemas/me.capcom.smsgateway.data.AppDatabase/8.json new file mode 100644 index 0000000..42a15e4 --- /dev/null +++ b/app/schemas/me.capcom.smsgateway.data.AppDatabase/8.json @@ -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')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/me/capcom/smsgateway/data/AppDatabase.kt b/app/src/main/java/me/capcom/smsgateway/data/AppDatabase.kt index 2ef576e..9317806 100644 --- a/app/src/main/java/me/capcom/smsgateway/data/AppDatabase.kt +++ b/app/src/main/java/me/capcom/smsgateway/data/AppDatabase.kt @@ -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), @@ -32,6 +32,7 @@ abstract class AppDatabase : RoomDatabase() { AppDatabase::class.java, "gateway" ) + .addMigrations(MIGRATION_7_8) .allowMainThreadQueries() .build() } diff --git a/app/src/main/java/me/capcom/smsgateway/data/Migrations.kt b/app/src/main/java/me/capcom/smsgateway/data/Migrations.kt new file mode 100644 index 0000000..6806400 --- /dev/null +++ b/app/src/main/java/me/capcom/smsgateway/data/Migrations.kt @@ -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() + ) + } +} \ No newline at end of file