Task 7: Create StationPreference entity and DAO
- Add StationPreference entity with FK to Station, unique index on stationId - Add StationPreferenceDao with getByStationId, upsert, deleteByStationId - Add station_preferences table via MIGRATION_4_5 Made-with: Cursor
This commit is contained in:
@@ -15,7 +15,7 @@ class RadioApplication : Application() {
|
||||
val database: RadioDatabase by lazy {
|
||||
Room.databaseBuilder(this, RadioDatabase::class.java, "radio_database")
|
||||
.addCallback(SomaFmSeedData)
|
||||
.addMigrations(RadioDatabase.MIGRATION_1_2, RadioDatabase.MIGRATION_2_3, RadioDatabase.MIGRATION_3_4)
|
||||
.addMigrations(RadioDatabase.MIGRATION_1_2, RadioDatabase.MIGRATION_2_3, RadioDatabase.MIGRATION_3_4, RadioDatabase.MIGRATION_4_5)
|
||||
.build()
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import xyz.cottongin.radio247.data.model.ListeningSession
|
||||
import xyz.cottongin.radio247.data.model.MetadataSnapshot
|
||||
import xyz.cottongin.radio247.data.model.Playlist
|
||||
import xyz.cottongin.radio247.data.model.Station
|
||||
import xyz.cottongin.radio247.data.model.StationPreference
|
||||
import xyz.cottongin.radio247.data.model.StationStream
|
||||
|
||||
@Database(
|
||||
@@ -18,9 +19,10 @@ import xyz.cottongin.radio247.data.model.StationStream
|
||||
MetadataSnapshot::class,
|
||||
ListeningSession::class,
|
||||
ConnectionSpan::class,
|
||||
StationStream::class
|
||||
StationStream::class,
|
||||
StationPreference::class
|
||||
],
|
||||
version = 4,
|
||||
version = 5,
|
||||
exportSchema = true
|
||||
)
|
||||
abstract class RadioDatabase : RoomDatabase() {
|
||||
@@ -30,6 +32,7 @@ abstract class RadioDatabase : RoomDatabase() {
|
||||
abstract fun listeningSessionDao(): ListeningSessionDao
|
||||
abstract fun connectionSpanDao(): ConnectionSpanDao
|
||||
abstract fun stationStreamDao(): StationStreamDao
|
||||
abstract fun stationPreferenceDao(): StationPreferenceDao
|
||||
|
||||
companion object {
|
||||
val MIGRATION_1_2 = object : Migration(1, 2) {
|
||||
@@ -63,5 +66,19 @@ abstract class RadioDatabase : RoomDatabase() {
|
||||
SomaFmSeedData.seedStreamsForExistingStations(db)
|
||||
}
|
||||
}
|
||||
|
||||
val MIGRATION_4_5 = object : Migration(4, 5) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL(
|
||||
"""CREATE TABLE IF NOT EXISTS station_preferences (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
stationId INTEGER NOT NULL,
|
||||
qualityOverride TEXT DEFAULT NULL,
|
||||
FOREIGN KEY(stationId) REFERENCES stations(id) ON DELETE CASCADE
|
||||
)"""
|
||||
)
|
||||
db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS index_station_preferences_stationId ON station_preferences(stationId)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package xyz.cottongin.radio247.data.db
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Upsert
|
||||
import xyz.cottongin.radio247.data.model.StationPreference
|
||||
|
||||
@Dao
|
||||
interface StationPreferenceDao {
|
||||
@Query("SELECT * FROM station_preferences WHERE stationId = :stationId")
|
||||
suspend fun getByStationId(stationId: Long): StationPreference?
|
||||
|
||||
@Upsert
|
||||
suspend fun upsert(pref: StationPreference)
|
||||
|
||||
@Query("DELETE FROM station_preferences WHERE stationId = :stationId")
|
||||
suspend fun deleteByStationId(stationId: Long)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package xyz.cottongin.radio247.data.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(
|
||||
tableName = "station_preferences",
|
||||
foreignKeys = [ForeignKey(
|
||||
entity = Station::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["stationId"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)],
|
||||
indices = [Index("stationId", unique = true)]
|
||||
)
|
||||
data class StationPreference(
|
||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||
val stationId: Long,
|
||||
val qualityOverride: String? = null
|
||||
)
|
||||
Reference in New Issue
Block a user