diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/AppDatabase.kt b/app/src/main/java/com/yogeshpaliyal/keypass/AppDatabase.kt index 9f93abc6..00fd2cec 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/AppDatabase.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/AppDatabase.kt @@ -7,6 +7,7 @@ import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import com.yogeshpaliyal.keypass.data.AccountModel import com.yogeshpaliyal.keypass.db.DbDao +import com.yogeshpaliyal.keypass.utils.getRandomString /* @@ -17,7 +18,7 @@ import com.yogeshpaliyal.keypass.db.DbDao */ @Database( entities = [AccountModel::class], - version = 3, exportSchema = false + version = 4, exportSchema = false ) abstract class AppDatabase : RoomDatabase() { @@ -33,34 +34,22 @@ abstract class AppDatabase : RoomDatabase() { synchronized(this) { - - _instance = Room.databaseBuilder( MyApplication.instance, AppDatabase::class.java, MyApplication.instance.getString(R.string.app_name) - ).addCallback(object : Callback() { - override fun onCreate(db: SupportSQLiteDatabase) { - super.onCreate(db) - } - - override fun onDestructiveMigration(db: SupportSQLiteDatabase) { - super.onDestructiveMigration(db) - onCreate(db) - } - - }) - /* .addMigrations(object : Migration(3, 4) { - override fun migrate(database: SupportSQLiteDatabase) { - val cursor = database.query("SELECT * FROM account") - while(cursor.moveToNext()) { - val id = cursor.getLong(cursor.getColumnIndex("id")) - val password = cursor.getLong(cursor.getColumnIndex("password")) - //-- Hash your password --// - database.execSQL("UPDATE account SET password = hashedPassword WHERE id = $id;") + ).addMigrations(object : Migration(3, 4) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE `account` ADD COLUMN `unique_id` TEXT") + database.query("select id,unique_id from `account` where unique_id IS NULL") + ?.use { + while (it.moveToNext()) { + val id = it.getInt(0) + database.execSQL("update `medias` set `file_unique_id` = '${getRandomString()}' where `id` = '$id'") + } } - } - })*/ + } + }) .build() } diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/data/AccountModel.kt b/app/src/main/java/com/yogeshpaliyal/keypass/data/AccountModel.kt index 13f93b6b..3a64e848 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/data/AccountModel.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/data/AccountModel.kt @@ -3,6 +3,7 @@ package com.yogeshpaliyal.keypass.data import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey +import com.google.gson.annotations.SerializedName import com.yogeshpaliyal.universal_adapter.model.BaseDiffUtil @@ -13,25 +14,43 @@ import com.yogeshpaliyal.universal_adapter.model.BaseDiffUtil * created on 30-01-2021 20:38 */ @Entity(tableName = "account") -class AccountModel( +data class AccountModel( @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") + @SerializedName("id") var id: Long? = null, + @ColumnInfo(name = "title") + @SerializedName("title") var title: String? = null, + + @ColumnInfo(name = "unique_id") + @SerializedName("unique_id") + var uniqueId: String? = null, + @ColumnInfo(name = "username") + @SerializedName("username") var username: String? = null, + @ColumnInfo(name = "password") + @SerializedName("password") var password: String? = null, + @ColumnInfo(name = "site") + @SerializedName("site") var site: String? = null, + @ColumnInfo(name = "notes") + @SerializedName("notes") var notes: String? = null, + @ColumnInfo(name = "tags") + @SerializedName("tags") var tags: String? = null ) : BaseDiffUtil { -fun getInitials() = (title?.firstOrNull() ?: username?.firstOrNull() ?: site?.firstOrNull() ?: notes?.firstOrNull() ?: 'K').toString() + fun getInitials() = (title?.firstOrNull() ?: username?.firstOrNull() ?: site?.firstOrNull() + ?: notes?.firstOrNull() ?: 'K').toString() override fun getDiffId(): Any? { return id diff --git a/app/src/main/java/com/yogeshpaliyal/keypass/db/DbDao.kt b/app/src/main/java/com/yogeshpaliyal/keypass/db/DbDao.kt index 91de9f0f..89b91e74 100644 --- a/app/src/main/java/com/yogeshpaliyal/keypass/db/DbDao.kt +++ b/app/src/main/java/com/yogeshpaliyal/keypass/db/DbDao.kt @@ -25,19 +25,19 @@ abstract class DbDao { abstract fun insertOrUpdateAccount(accountModel: List) - @Query("SELECT * from account") + @Query("SELECT * FROM account ORDER BY title ASC") abstract fun getAllAccounts() : Flow> - @Query("SELECT * from account where tags = :tag") + @Query("SELECT * FROM account WHERE tags = :tag ORDER BY title ASC") abstract fun getAllAccounts(tag: String) : Flow> - @Query("SELECT * from account where id = :id") + @Query("SELECT * FROM account WHERE id = :id") abstract fun getAccount(id: Long?) : AccountModel? - @Query("SELECT DISTINCT tags from account") + @Query("SELECT DISTINCT tags FROM account") abstract fun getTags() : Flow> - @Query("DELETE from account where id = :id") + @Query("DELETE from account WHERE id = :id") abstract fun deleteAccount(id: Long?)