Migrate database (1)
This commit is contained in:
@@ -6,6 +6,8 @@ import android.os.Parcel
|
||||
import androidx.media3.common.MediaItem
|
||||
import androidx.room.*
|
||||
import androidx.room.migration.AutoMigrationSpec
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import it.vfsfitvnm.vimusic.models.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@@ -135,13 +137,16 @@ interface Database {
|
||||
Playlist::class,
|
||||
Info::class,
|
||||
SongWithAuthors::class,
|
||||
Album::class,
|
||||
Artist::class,
|
||||
SongArtistMap::class,
|
||||
SearchQuery::class,
|
||||
QueuedMediaItem::class,
|
||||
],
|
||||
views = [
|
||||
SortedSongInPlaylist::class
|
||||
],
|
||||
version = 6,
|
||||
version = 9,
|
||||
exportSchema = true,
|
||||
autoMigrations = [
|
||||
AutoMigration(from = 1, to = 2),
|
||||
@@ -149,6 +154,8 @@ interface Database {
|
||||
AutoMigration(from = 3, to = 4, spec = DatabaseInitializer.From3To4Migration::class),
|
||||
AutoMigration(from = 4, to = 5),
|
||||
AutoMigration(from = 5, to = 6),
|
||||
AutoMigration(from = 6, to = 7),
|
||||
AutoMigration(from = 7, to = 8, spec = DatabaseInitializer.From7To8Migration::class),
|
||||
],
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
@@ -163,6 +170,7 @@ abstract class DatabaseInitializer protected constructor() : RoomDatabase() {
|
||||
if (!::Instance.isInitialized) {
|
||||
Instance = Room
|
||||
.databaseBuilder(this@Context, DatabaseInitializer::class.java, "data.db")
|
||||
.addMigrations(From8To9Migration())
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -170,6 +178,15 @@ abstract class DatabaseInitializer protected constructor() : RoomDatabase() {
|
||||
|
||||
@DeleteTable.Entries(DeleteTable(tableName = "QueuedMediaItem"))
|
||||
class From3To4Migration : AutoMigrationSpec
|
||||
|
||||
@RenameColumn.Entries(RenameColumn("Song", "albumInfoId", "albumId"))
|
||||
class From7To8Migration : AutoMigrationSpec
|
||||
|
||||
class From8To9Migration : Migration(8, 9) {
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TypeConverters
|
||||
|
||||
13
app/src/main/kotlin/it/vfsfitvnm/vimusic/models/Album.kt
Normal file
13
app/src/main/kotlin/it/vfsfitvnm/vimusic/models/Album.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package it.vfsfitvnm.vimusic.models
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity
|
||||
data class Album(
|
||||
@PrimaryKey val id: String,
|
||||
val title: String,
|
||||
val thumbnailUrl: String?,
|
||||
val year: String?,
|
||||
val authorsText: String?
|
||||
)
|
||||
12
app/src/main/kotlin/it/vfsfitvnm/vimusic/models/Artist.kt
Normal file
12
app/src/main/kotlin/it/vfsfitvnm/vimusic/models/Artist.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package it.vfsfitvnm.vimusic.models
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity
|
||||
data class Artist(
|
||||
@PrimaryKey val id: String,
|
||||
val name: String,
|
||||
val thumbnailUrl: String?,
|
||||
val info: String?
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
package it.vfsfitvnm.vimusic.models
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Junction
|
||||
import androidx.room.Relation
|
||||
|
||||
|
||||
data class DetailedSong(
|
||||
@Embedded val song: Song,
|
||||
@Relation(
|
||||
entity = Album::class,
|
||||
parentColumn = "albumId",
|
||||
entityColumn = "id"
|
||||
) val album: Album?,
|
||||
@Relation(
|
||||
entity = Artist::class,
|
||||
parentColumn = "id",
|
||||
entityColumn = "id",
|
||||
associateBy = Junction(
|
||||
value = SongArtistMap::class,
|
||||
parentColumn = "songId",
|
||||
entityColumn = "artistId"
|
||||
)
|
||||
)
|
||||
val artists: List<Artist>?
|
||||
)
|
||||
@@ -7,7 +7,8 @@ import androidx.room.*
|
||||
data class Song(
|
||||
@PrimaryKey val id: String,
|
||||
val title: String,
|
||||
val albumInfoId: Long?,
|
||||
val albumId: String?,
|
||||
val artistsText: String? = null,
|
||||
val durationText: String,
|
||||
val thumbnailUrl: String?,
|
||||
val lyrics: String? = null,
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package it.vfsfitvnm.vimusic.models
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
|
||||
|
||||
@Entity(
|
||||
primaryKeys = ["songId", "artistId"],
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = Song::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["songId"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
),
|
||||
ForeignKey(
|
||||
entity = Artist::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["artistId"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
]
|
||||
)
|
||||
data class SongArtistMap(
|
||||
@ColumnInfo(index = true) val songId: String,
|
||||
@ColumnInfo(index = true) val artistId: String
|
||||
)
|
||||
@@ -8,7 +8,7 @@ open class SongWithInfo(
|
||||
@Embedded val song: Song,
|
||||
@Relation(
|
||||
entity = Info::class,
|
||||
parentColumn = "albumInfoId",
|
||||
parentColumn = "albumId",
|
||||
entityColumn = "id"
|
||||
) val album: Info?,
|
||||
@Relation(
|
||||
|
||||
@@ -52,7 +52,7 @@ fun Database.insert(mediaItem: MediaItem): Song {
|
||||
val song = Song(
|
||||
id = mediaItem.mediaId,
|
||||
title = mediaItem.mediaMetadata.title!!.toString(),
|
||||
albumInfoId = albumInfoId,
|
||||
albumId = albumInfoId.toString(),
|
||||
durationText = mediaItem.mediaMetadata.extras?.getString("durationText")!!,
|
||||
thumbnailUrl = mediaItem.mediaMetadata.artworkUri!!.toString(),
|
||||
loudnessDb = mediaItem.mediaMetadata.extras?.getFloat("loudnessDb"),
|
||||
|
||||
Reference in New Issue
Block a user