From 5bcfe031bb66a63258f40bbc153fbb2d8512a42b Mon Sep 17 00:00:00 2001 From: zuevval Date: Sat, 30 Oct 2021 19:01:17 +0300 Subject: [PATCH 1/4] #316 add 19-20 DB migration --- .../data/db/LearnBrailleDatabase.kt | 214 ++++++++++-------- 1 file changed, 114 insertions(+), 100 deletions(-) diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt index 75329794..ce38a806 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt @@ -31,7 +31,7 @@ import timber.log.Timber CurrentStep::class, LastCourseStep::class, LastLessonStep::class, Action::class ], - version = 19, + version = 20, exportSchema = true ) @TypeConverters( @@ -134,7 +134,8 @@ abstract class LearnBrailleDatabase : RoomDatabase(), KoinComponent { .addMigrations( MIGRATION_16_17, MIGRATION_17_18, - MIGRATION_18_19 + MIGRATION_18_19, + MIGRATION_19_20 ) .build() .init() @@ -209,119 +210,132 @@ private val MIGRATION_17_18 = object : Migration(17, 18) { } } -private val MIGRATION_18_19 = object : Migration(18, 19) { - override fun migrate(database: SupportSQLiteDatabase) { - Timber.i("Start 18-19 migration") - - database.execSQL("delete from lessons") - database.execSQL("delete from steps") - database.execSQL("delete from decks") - database.execSQL("delete from cards") - database.execSQL("delete from materials") - database.execSQL("delete from step_has_annotations") - database.execSQL("delete from step_annotations") - - Timber.i("Old data removed") - - prepopulationData.run { - lessons?.forEach { - database.insert( - "lessons", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("id", id) - put("course_id", courseId) - put("name", name) - put("description", description) - } +fun updateTheoryAndMaterials(database: SupportSQLiteDatabase) { + database.execSQL("delete from lessons") + database.execSQL("delete from steps") + database.execSQL("delete from decks") + database.execSQL("delete from cards") + database.execSQL("delete from materials") + database.execSQL("delete from step_has_annotations") + database.execSQL("delete from step_annotations") + + Timber.i("Old data removed") + + prepopulationData.run { + lessons?.forEach { + database.insert( + "lessons", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("id", id) + put("course_id", courseId) + put("name", name) + put("description", description) } - ) - } + } + ) + } - steps?.forEach { - database.insert( - "steps", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("id", id) - put("course_id", courseId) - put("lesson_id", lessonId) - put("data", StepDataConverters().to(data)) - } + steps?.forEach { + database.insert( + "steps", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("id", id) + put("course_id", courseId) + put("lesson_id", lessonId) + put("data", StepDataConverters().to(data)) } - ) - } + } + ) + } - decks?.forEach { - database.insert( - "decks", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("id", id) - put("tag", tag) - } + decks?.forEach { + database.insert( + "decks", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("id", id) + put("tag", tag) } - ) - } + } + ) + } - cards?.forEach { - database.insert( - "cards", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("deck_id", deckId) - put("material_id", materialId) - } + cards?.forEach { + database.insert( + "cards", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("deck_id", deckId) + put("material_id", materialId) } - ) - } + } + ) + } - materials?.forEach { - database.insert( - "materials", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("id", id) - put("data", MaterialDataTypeConverters().to(data)) - } + materials?.forEach { + database.insert( + "materials", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("id", id) + put("data", MaterialDataTypeConverters().to(data)) } - ) - } + } + ) + } - stepAnnotations?.forEach { - database.insert( - "step_annotations", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("id", id) - put("name", name) - } + stepAnnotations?.forEach { + database.insert( + "step_annotations", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("id", id) + put("name", name) } - ) - } + } + ) + } - stepsHasAnnotations?.forEach { - database.insert( - "step_has_annotations", - SQLiteDatabase.CONFLICT_ABORT, - it.run { - ContentValues().apply { - put("course_id", courseId) - put("lesson_id", lessonId) - put("step_id", stepId) - put("annotation_id", annotationId) - } + stepsHasAnnotations?.forEach { + database.insert( + "step_has_annotations", + SQLiteDatabase.CONFLICT_ABORT, + it.run { + ContentValues().apply { + put("course_id", courseId) + put("lesson_id", lessonId) + put("step_id", stepId) + put("annotation_id", annotationId) } - ) - } + } + ) } + } + + Timber.i("New data inserted") +} +private val MIGRATION_18_19 = object : Migration(18, 19) { + override fun migrate(database: SupportSQLiteDatabase) { + Timber.i("Start 18-19 migration") + updateTheoryAndMaterials(database = database) Timber.i("Finish 18-19 migration") } } + +private val MIGRATION_19_20 = object : Migration(19, 20) { + override fun migrate(database: SupportSQLiteDatabase) { + Timber.i("Start 19-20 migration") + updateTheoryAndMaterials(database = database) + Timber.i("Finish 19-20 migration") + } +} + From c3b28565a87289737b481f2fe977bb9b3b913257 Mon Sep 17 00:00:00 2001 From: zuevval Date: Sat, 30 Oct 2021 19:11:13 +0300 Subject: [PATCH 2/4] #316 detekt: add exception for 'updateTheoryAndMaterials' --- detekt-config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detekt-config.yml b/detekt-config.yml index 3cb39ad2..86265a83 100644 --- a/detekt-config.yml +++ b/detekt-config.yml @@ -6,7 +6,7 @@ complexity: LongMethod: excludes: ['**/*Fragment.kt', '**/*Application.kt', '**/LearnBrailleDatabase.kt'] ComplexMethod: - excludes: ['**/*Fragment.kt'] + excludes: ['**/*Fragment.kt', '**/LearnBrailleDatabase.kt'] LongParameterList: excludes: ['**/*Repository.kt'] # Dependency injection From 37d1bc2d2e1567e6c4157ac57ccd28056c594e92 Mon Sep 17 00:00:00 2001 From: zuevval Date: Sun, 31 Oct 2021 08:56:26 +0300 Subject: [PATCH 3/4] #319 DB migration: named args -> unnamed --- .../learnbraille/data/db/LearnBrailleDatabase.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt b/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt index ce38a806..90896701 100644 --- a/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt +++ b/app/src/main/java/com/github/braillesystems/learnbraille/data/db/LearnBrailleDatabase.kt @@ -326,7 +326,7 @@ fun updateTheoryAndMaterials(database: SupportSQLiteDatabase) { private val MIGRATION_18_19 = object : Migration(18, 19) { override fun migrate(database: SupportSQLiteDatabase) { Timber.i("Start 18-19 migration") - updateTheoryAndMaterials(database = database) + updateTheoryAndMaterials(database) Timber.i("Finish 18-19 migration") } } @@ -334,7 +334,7 @@ private val MIGRATION_18_19 = object : Migration(18, 19) { private val MIGRATION_19_20 = object : Migration(19, 20) { override fun migrate(database: SupportSQLiteDatabase) { Timber.i("Start 19-20 migration") - updateTheoryAndMaterials(database = database) + updateTheoryAndMaterials(database) Timber.i("Finish 19-20 migration") } } From 1156ef00ee28f6f5e44fe221dc8e2458ca61a047 Mon Sep 17 00:00:00 2001 From: zuevval Date: Sun, 31 Oct 2021 09:41:16 +0300 Subject: [PATCH 4/4] empty commit to repeat failed workflow