Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#316 add 19-20 DB migration #325

Merged
merged 5 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import timber.log.Timber
CurrentStep::class, LastCourseStep::class, LastLessonStep::class,
Action::class
],
version = 19,
version = 20,
exportSchema = true
)
@TypeConverters(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
}

2 changes: 1 addition & 1 deletion detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down