Use "factory" method for creating db migrations

This commit is contained in:
Yevhen Babiichuk (DustDFG) 2026-01-02 12:25:25 +02:00
parent 2fc5412ceb
commit 811c8f5bf6

View file

@ -34,8 +34,7 @@ object Migrations {
private val TAG = Migrations::class.java.getName() private val TAG = Migrations::class.java.getName()
private val isDebug = MainActivity.DEBUG private val isDebug = MainActivity.DEBUG
val MIGRATION_1_2 = object : Migration(DB_VER_1, DB_VER_2) { val MIGRATION_1_2 = Migration(DB_VER_1, DB_VER_2) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
if (isDebug) { if (isDebug) {
Log.d(TAG, "Start migrating database") Log.d(TAG, "Start migrating database")
} }
@ -149,10 +148,8 @@ object Migrations {
Log.d(TAG, "Stop migrating database") Log.d(TAG, "Stop migrating database")
} }
} }
}
val MIGRATION_2_3 = object : Migration(DB_VER_2, DB_VER_3) { val MIGRATION_2_3 = Migration(DB_VER_2, DB_VER_3) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
// Add NOT NULLs and new fields // Add NOT NULLs and new fields
db.execSQL( db.execSQL(
"CREATE TABLE IF NOT EXISTS streams_new " + "CREATE TABLE IF NOT EXISTS streams_new " +
@ -221,34 +218,26 @@ object Migrations {
"ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED)" "ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED)"
) )
} }
}
val MIGRATION_3_4 = object : Migration(DB_VER_3, DB_VER_4) { val MIGRATION_3_4 = Migration(DB_VER_3, DB_VER_4) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE streams ADD COLUMN uploader_url TEXT") db.execSQL("ALTER TABLE streams ADD COLUMN uploader_url TEXT")
} }
}
val MIGRATION_4_5 = object : Migration(DB_VER_4, DB_VER_5) { val MIGRATION_4_5 = Migration(DB_VER_4, DB_VER_5) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL( db.execSQL(
"ALTER TABLE `subscriptions` ADD COLUMN `notification_mode` " + "ALTER TABLE `subscriptions` ADD COLUMN `notification_mode` " +
"INTEGER NOT NULL DEFAULT 0" "INTEGER NOT NULL DEFAULT 0"
) )
} }
}
val MIGRATION_5_6 = object : Migration(DB_VER_5, DB_VER_6) { val MIGRATION_5_6 = Migration(DB_VER_5, DB_VER_6) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL( db.execSQL(
"ALTER TABLE `playlists` ADD COLUMN `is_thumbnail_permanent` " + "ALTER TABLE `playlists` ADD COLUMN `is_thumbnail_permanent` " +
"INTEGER NOT NULL DEFAULT 0" "INTEGER NOT NULL DEFAULT 0"
) )
} }
}
val MIGRATION_6_7 = object : Migration(DB_VER_6, DB_VER_7) { val MIGRATION_6_7 = Migration(DB_VER_6, DB_VER_7) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
// Create a new column thumbnail_stream_id // Create a new column thumbnail_stream_id
db.execSQL( db.execSQL(
"ALTER TABLE `playlists` ADD COLUMN `thumbnail_stream_id` " + "ALTER TABLE `playlists` ADD COLUMN `thumbnail_stream_id` " +
@ -290,20 +279,16 @@ object Migrations {
"`index_playlists_name` ON `playlists` (`name`)" "`index_playlists_name` ON `playlists` (`name`)"
) )
} }
}
val MIGRATION_7_8 = object : Migration(DB_VER_7, DB_VER_8) { val MIGRATION_7_8 = Migration(DB_VER_7, DB_VER_8) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL( db.execSQL(
"DELETE FROM search_history WHERE id NOT IN (SELECT id FROM (SELECT " + "DELETE FROM search_history WHERE id NOT IN (SELECT id FROM (SELECT " +
"MIN(id) as id FROM search_history GROUP BY trim(search), service_id ) tmp)" "MIN(id) as id FROM search_history GROUP BY trim(search), service_id ) tmp)"
) )
db.execSQL("UPDATE search_history SET search = trim(search)") db.execSQL("UPDATE search_history SET search = trim(search)")
} }
}
val MIGRATION_8_9 = object : Migration(DB_VER_8, DB_VER_9) { val MIGRATION_8_9 = Migration(DB_VER_8, DB_VER_9) { db ->
override fun migrate(db: SupportSQLiteDatabase) {
try { try {
db.beginTransaction() db.beginTransaction()
@ -365,4 +350,3 @@ object Migrations {
} }
} }
} }
}