mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
refactor: optimize Firestore rules and meal cleanup
- Tighten Firestore validation rules (data size and limits) - Pass context to `deletePhotoFile` for safer file management - Cleanup unused dependencies in build configuration - Update Robolectric SDK target for tests
This commit is contained in:
1 parent
87bbb448c2
commit
79e04f64d8
28 files changed
+1606
-1230
No files matched your search
@@ -4,12 +4,19 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.example.BuildConfig
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.Types
|
||||
|
||||
@Database(
|
||||
entities = [LocalMealEntity::class, LocalCustomFoodEntity::class],
|
||||
version = 2,
|
||||
version = 3,
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(RoomConverters::class)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun mealDao(): LocalMealDao
|
||||
@@ -18,14 +25,93 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
@Volatile
|
||||
private var INSTANCE: AppDatabase? = null
|
||||
|
||||
val MIGRATION_1_2 = object : Migration(1, 2) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
// Version 1 to 2 schema adjustments if needed
|
||||
}
|
||||
}
|
||||
|
||||
val MIGRATION_2_3 = object : Migration(2, 3) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
// 1. Create new table matching RoomConverters JSON schema
|
||||
db.execSQL(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS `local_meals_new` (
|
||||
`id` TEXT NOT NULL PRIMARY KEY,
|
||||
`userId` TEXT NOT NULL,
|
||||
`mealType` TEXT NOT NULL,
|
||||
`mealTimestamp` INTEGER NOT NULL,
|
||||
`hungerBefore` INTEGER NOT NULL,
|
||||
`satietyAfter` INTEGER NOT NULL,
|
||||
`note` TEXT NOT NULL,
|
||||
`foodItems` TEXT NOT NULL,
|
||||
`photoUris` TEXT NOT NULL,
|
||||
`updatedAt` INTEGER NOT NULL
|
||||
)
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
// 2. Migrate existing delimiter-separated data to Moshi JSON arrays
|
||||
val moshi = Moshi.Builder().build()
|
||||
val listType = Types.newParameterizedType(List::class.java, String::class.java)
|
||||
val adapter = moshi.adapter<List<String>>(listType)
|
||||
|
||||
val cursor = db.query("SELECT id, userId, mealType, mealTimestamp, hungerBefore, satietyAfter, note, foodItemsJoined, photoUrisJoined, updatedAt FROM local_meals")
|
||||
while (cursor.moveToNext()) {
|
||||
val id = cursor.getString(0)
|
||||
val userId = cursor.getString(1)
|
||||
val mealType = cursor.getString(2)
|
||||
val mealTimestamp = cursor.getLong(3)
|
||||
val hungerBefore = cursor.getInt(4)
|
||||
val satietyAfter = cursor.getInt(5)
|
||||
val note = cursor.getString(6)
|
||||
val foodItemsJoined = cursor.getString(7) ?: ""
|
||||
val photoUrisJoined = cursor.getString(8) ?: ""
|
||||
val updatedAt = cursor.getLong(9)
|
||||
|
||||
val foodList = if (foodItemsJoined.isNotBlank()) {
|
||||
foodItemsJoined.split("###").filter { it.isNotBlank() }
|
||||
} else emptyList()
|
||||
|
||||
val photoList = if (photoUrisJoined.isNotBlank()) {
|
||||
photoUrisJoined.split("|||").filter { it.isNotBlank() }
|
||||
} else emptyList()
|
||||
|
||||
val foodJson = adapter.toJson(foodList)
|
||||
val photoJson = adapter.toJson(photoList)
|
||||
|
||||
db.execSQL(
|
||||
"INSERT INTO local_meals_new (id, userId, mealType, mealTimestamp, hungerBefore, satietyAfter, note, foodItems, photoUris, updatedAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
arrayOf(id, userId, mealType, mealTimestamp, hungerBefore, satietyAfter, note, foodJson, photoJson, updatedAt)
|
||||
)
|
||||
}
|
||||
cursor.close()
|
||||
|
||||
// 3. Drop legacy table and rename new table
|
||||
db.execSQL("DROP TABLE local_meals")
|
||||
db.execSQL("ALTER TABLE local_meals_new RENAME TO local_meals")
|
||||
}
|
||||
}
|
||||
|
||||
fun getInstance(context: Context): AppDatabase {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
INSTANCE ?: Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
AppDatabase::class.java,
|
||||
"satiea_local.db"
|
||||
).fallbackToDestructiveMigration(true).build().also { INSTANCE = it }
|
||||
INSTANCE ?: buildDatabase(context.applicationContext).also { INSTANCE = it }
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildDatabase(appContext: Context): AppDatabase {
|
||||
val builder = Room.databaseBuilder(
|
||||
appContext,
|
||||
AppDatabase::class.java,
|
||||
"satiea_local.db"
|
||||
).addMigrations(MIGRATION_1_2, MIGRATION_2_3)
|
||||
|
||||
// Destructive migration is only permitted on debug builds
|
||||
if (BuildConfig.DEBUG) {
|
||||
builder.fallbackToDestructiveMigrationOnDowngrade(true)
|
||||
}
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user