create a db table for attachments
This commit is contained in:
parent
4cad413478
commit
0f33d3677b
4 changed files with 47 additions and 5 deletions
|
|
@ -4,14 +4,20 @@ import android.content.Context
|
|||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.simplemobiletools.smsmessenger.interfaces.AttachmentsDao
|
||||
import com.simplemobiletools.smsmessenger.interfaces.ConversationsDao
|
||||
import com.simplemobiletools.smsmessenger.models.Attachment
|
||||
import com.simplemobiletools.smsmessenger.models.Conversation
|
||||
|
||||
@Database(entities = [(Conversation::class)], version = 1)
|
||||
@Database(entities = [Conversation::class, Attachment::class], version = 2)
|
||||
abstract class MessagesDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun ConversationsDao(): ConversationsDao
|
||||
|
||||
abstract fun AttachmentsDao(): AttachmentsDao
|
||||
|
||||
companion object {
|
||||
private var db: MessagesDatabase? = null
|
||||
|
||||
|
|
@ -20,11 +26,20 @@ abstract class MessagesDatabase : RoomDatabase() {
|
|||
synchronized(MessagesDatabase::class) {
|
||||
if (db == null) {
|
||||
db = Room.databaseBuilder(context.applicationContext, MessagesDatabase::class.java, "conversations.db")
|
||||
.fallbackToDestructiveMigration()
|
||||
.addMigrations(MIGRATION_1_2)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
return db!!
|
||||
}
|
||||
|
||||
private val MIGRATION_1_2 = object : Migration(1, 2) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("CREATE TABLE IF NOT EXISTS `attachments` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `message_id` INTEGER NOT NULL, `uri_string` TEXT NOT NULL, `mimetype` TEXT NOT NULL, `width` INTEGER NOT NULL, `height` INTEGER NOT NULL, `filename` TEXT NOT NULL)")
|
||||
database.execSQL("CREATE UNIQUE INDEX `index_attachments_message_id` ON `attachments` (`message_id`)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue