Fix DatabaseMigrationTest

Complete removal of unneeded index, and remove default value for `remote_playlists.display_index`.
This commit is contained in:
Stypox 2024-03-29 20:43:55 +01:00
parent 4591c09637
commit 92e9c3e42e
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
5 changed files with 37 additions and 48 deletions

View file

@ -2,7 +2,7 @@
"formatVersion": 1, "formatVersion": 1,
"database": { "database": {
"version": 9, "version": 9,
"identityHash": "94596ea2227c63dd78b472ea4a83f1c4", "identityHash": "7591e8039faa74d8c0517dc867af9d3e",
"entities": [ "entities": [
{ {
"tableName": "subscriptions", "tableName": "subscriptions",
@ -362,17 +362,7 @@
"uid" "uid"
] ]
}, },
"indices": [ "indices": [],
{
"name": "index_playlists_name",
"unique": false,
"columnNames": [
"name"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_playlists_name` ON `${TABLE_NAME}` (`name`)"
}
],
"foreignKeys": [] "foreignKeys": []
}, },
{ {
@ -511,15 +501,6 @@
] ]
}, },
"indices": [ "indices": [
{
"name": "index_remote_playlists_name",
"unique": false,
"columnNames": [
"name"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_remote_playlists_name` ON `${TABLE_NAME}` (`name`)"
},
{ {
"name": "index_remote_playlists_service_id_url", "name": "index_remote_playlists_service_id_url",
"unique": true, "unique": true,
@ -743,7 +724,7 @@
"views": [], "views": [],
"setupQueries": [ "setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '94596ea2227c63dd78b472ea4a83f1c4')" "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '7591e8039faa74d8c0517dc867af9d3e')"
] ]
} }
} }

View file

@ -122,8 +122,10 @@ class DatabaseMigrationTest {
) )
testHelper.runMigrationsAndValidate( testHelper.runMigrationsAndValidate(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6, AppDatabase.DATABASE_NAME,
true, Migrations.MIGRATION_5_6 Migrations.DB_VER_9,
true,
Migrations.MIGRATION_8_9
) )
val migratedDatabaseV3 = getMigratedDatabase() val migratedDatabaseV3 = getMigratedDatabase()
@ -209,6 +211,11 @@ class DatabaseMigrationTest {
true, Migrations.MIGRATION_7_8 true, Migrations.MIGRATION_7_8
) )
testHelper.runMigrationsAndValidate(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_9,
true, Migrations.MIGRATION_8_9
)
val migratedDatabaseV8 = getMigratedDatabase() val migratedDatabaseV8 = getMigratedDatabase()
val listFromDB = migratedDatabaseV8.searchHistoryDAO().all.blockingFirst() val listFromDB = migratedDatabaseV8.searchHistoryDAO().all.blockingFirst()
@ -220,25 +227,27 @@ class DatabaseMigrationTest {
@Test @Test
fun migrateDatabaseFrom8to9() { fun migrateDatabaseFrom8to9() {
val databaseInV5 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_5) val databaseInV8 = testHelper.createDatabase(AppDatabase.DATABASE_NAME, Migrations.DB_VER_8)
val localUid1: Long val localUid1: Long
val localUid2: Long val localUid2: Long
val remoteUid1: Long val remoteUid1: Long
val remoteUid2: Long val remoteUid2: Long
databaseInV5.run { databaseInV8.run {
localUid1 = insert( localUid1 = insert(
"playlists", SQLiteDatabase.CONFLICT_FAIL, "playlists", SQLiteDatabase.CONFLICT_FAIL,
ContentValues().apply { ContentValues().apply {
put("name", DEFAULT_NAME + "1") put("name", DEFAULT_NAME + "1")
put("thumbnail_url", DEFAULT_THUMBNAIL) put("is_thumbnail_permanent", false)
put("thumbnail_stream_id", -1)
} }
) )
localUid2 = insert( localUid2 = insert(
"playlists", SQLiteDatabase.CONFLICT_FAIL, "playlists", SQLiteDatabase.CONFLICT_FAIL,
ContentValues().apply { ContentValues().apply {
put("name", DEFAULT_NAME + "2") put("name", DEFAULT_NAME + "2")
put("thumbnail_url", DEFAULT_THUMBNAIL) put("is_thumbnail_permanent", false)
put("thumbnail_stream_id", -1)
} }
) )
delete( delete(
@ -267,33 +276,35 @@ class DatabaseMigrationTest {
} }
testHelper.runMigrationsAndValidate( testHelper.runMigrationsAndValidate(
AppDatabase.DATABASE_NAME, Migrations.DB_VER_6, AppDatabase.DATABASE_NAME,
true, Migrations.MIGRATION_5_6 Migrations.DB_VER_9,
true,
Migrations.MIGRATION_8_9
) )
val migratedDatabaseV6 = getMigratedDatabase() val migratedDatabaseV9 = getMigratedDatabase()
var localListFromDB = migratedDatabaseV6.playlistDAO().all.blockingFirst() var localListFromDB = migratedDatabaseV9.playlistDAO().all.blockingFirst()
var remoteListFromDB = migratedDatabaseV6.playlistRemoteDAO().all.blockingFirst() var remoteListFromDB = migratedDatabaseV9.playlistRemoteDAO().all.blockingFirst()
assertEquals(1, localListFromDB.size) assertEquals(1, localListFromDB.size)
assertEquals(localUid2, localListFromDB[0].uid) assertEquals(localUid2, localListFromDB[0].uid)
assertEquals(0, localListFromDB[0].displayIndex) assertEquals(-1, localListFromDB[0].displayIndex)
assertEquals(1, remoteListFromDB.size) assertEquals(1, remoteListFromDB.size)
assertEquals(remoteUid1, remoteListFromDB[0].uid) assertEquals(remoteUid1, remoteListFromDB[0].uid)
assertEquals(0, remoteListFromDB[0].displayIndex) assertEquals(-1, remoteListFromDB[0].displayIndex)
val localUid3 = migratedDatabaseV6.playlistDAO().insert( val localUid3 = migratedDatabaseV9.playlistDAO().insert(
PlaylistEntity(DEFAULT_NAME + "3", DEFAULT_THUMBNAIL, -1) PlaylistEntity(DEFAULT_NAME + "3", false, -1, -1)
) )
val remoteUid3 = migratedDatabaseV6.playlistRemoteDAO().insert( val remoteUid3 = migratedDatabaseV9.playlistRemoteDAO().insert(
PlaylistRemoteEntity( PlaylistRemoteEntity(
DEFAULT_THIRD_SERVICE_ID, DEFAULT_NAME, DEFAULT_THIRD_URL, DEFAULT_THIRD_SERVICE_ID, DEFAULT_NAME, DEFAULT_THIRD_URL,
DEFAULT_THUMBNAIL, DEFAULT_UPLOADER_NAME, -1, 10 DEFAULT_THUMBNAIL, DEFAULT_UPLOADER_NAME, -1, 10
) )
) )
localListFromDB = migratedDatabaseV6.playlistDAO().all.blockingFirst() localListFromDB = migratedDatabaseV9.playlistDAO().all.blockingFirst()
remoteListFromDB = migratedDatabaseV6.playlistRemoteDAO().all.blockingFirst() remoteListFromDB = migratedDatabaseV9.playlistRemoteDAO().all.blockingFirst()
assertEquals(2, localListFromDB.size) assertEquals(2, localListFromDB.size)
assertEquals(localUid3, localListFromDB[1].uid) assertEquals(localUid3, localListFromDB[1].uid)
assertEquals(-1, localListFromDB[1].displayIndex) assertEquals(-1, localListFromDB[1].displayIndex)

View file

@ -278,12 +278,13 @@ public final class Migrations {
+ "(`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + "(`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ "`service_id` INTEGER NOT NULL, `name` TEXT, `url` TEXT, " + "`service_id` INTEGER NOT NULL, `name` TEXT, `url` TEXT, "
+ "`thumbnail_url` TEXT, `uploader` TEXT, " + "`thumbnail_url` TEXT, `uploader` TEXT, "
+ "`display_index` INTEGER NOT NULL DEFAULT 0," + "`display_index` INTEGER NOT NULL,"
+ "`stream_count` INTEGER)"); + "`stream_count` INTEGER)");
database.execSQL("INSERT INTO `remote_playlists_tmp` (`uid`, `service_id`, " database.execSQL("INSERT INTO `remote_playlists_tmp` (`uid`, `service_id`, "
+ "`name`, `url`, `thumbnail_url`, `uploader`, `stream_count`)" + "`name`, `url`, `thumbnail_url`, `uploader`, `display_index`, "
+ "`stream_count`)"
+ "SELECT `uid`, `service_id`, `name`, `url`, `thumbnail_url`, `uploader`, " + "SELECT `uid`, `service_id`, `name`, `url`, `thumbnail_url`, `uploader`, "
+ "`stream_count` FROM `remote_playlists`"); + "-1, `stream_count` FROM `remote_playlists`");
// Replace the old table, note that this also removes the index on the name which // Replace the old table, note that this also removes the index on the name which
// we don't need anymore. // we don't need anymore.

View file

@ -3,17 +3,14 @@ package org.schabi.newpipe.database.playlist.model;
import androidx.room.ColumnInfo; import androidx.room.ColumnInfo;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.Ignore; import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_NAME;
import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE; import static org.schabi.newpipe.database.playlist.model.PlaylistEntity.PLAYLIST_TABLE;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry; import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry;
@Entity(tableName = PLAYLIST_TABLE, @Entity(tableName = PLAYLIST_TABLE)
indices = {@Index(value = {PLAYLIST_NAME})})
public class PlaylistEntity { public class PlaylistEntity {
public static final String DEFAULT_THUMBNAIL = "drawable://" public static final String DEFAULT_THUMBNAIL = "drawable://"

View file

@ -21,7 +21,6 @@ import static org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity.RE
@Entity(tableName = REMOTE_PLAYLIST_TABLE, @Entity(tableName = REMOTE_PLAYLIST_TABLE,
indices = { indices = {
@Index(value = {REMOTE_PLAYLIST_NAME}),
@Index(value = {REMOTE_PLAYLIST_SERVICE_ID, REMOTE_PLAYLIST_URL}, unique = true) @Index(value = {REMOTE_PLAYLIST_SERVICE_ID, REMOTE_PLAYLIST_URL}, unique = true)
}) })
public class PlaylistRemoteEntity implements PlaylistLocalItem { public class PlaylistRemoteEntity implements PlaylistLocalItem {