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..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 @@ -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) 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) + Timber.i("Finish 19-20 migration") + } +} + 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