From 5b0901e3114f4a985bd375ed454a4d7761b4933f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Thu, 3 Oct 2019 14:00:07 +0200 Subject: [PATCH] Clear Semesters table (#535) --- app/build.gradle | 6 ++- .../db/migrations/AbstractMigrationTest.kt | 9 +--- .../data/db/migrations/Migration13Test.kt | 40 ++++++++++++++---- .../github/wulkanowy/data/db/AppDatabase.kt | 41 +++++++++++-------- .../data/db/migrations/Migration17.kt | 9 ++++ 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 51d83b814..0201f7fad 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,8 +28,10 @@ android { ] javaCompileOptions { annotationProcessorOptions { - arguments = ["room.schemaLocation": "$projectDir/schemas".toString(), - "room.incremental" : "true"] + arguments = [ + "room.schemaLocation": "$projectDir/schemas".toString(), + "room.incremental" : "true" + ] } } } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt index 42e098624..74cac078c 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt @@ -22,14 +22,7 @@ abstract class AbstractMigrationTest { fun getMigratedRoomDatabase(): AppDatabase { val database = Room.databaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java, dbName) - .addMigrations( - Migration12(), - Migration13(), - Migration14(), - Migration15(), - Migration16(), - Migration17() - ) + .addMigrations(*AppDatabase.getMigrations()) .build() // close the database and release any stream resources when the test finishes helper.closeWhenFinished(database) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt index eb9d02a59..8311f94b5 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt @@ -3,10 +3,14 @@ package io.github.wulkanowy.data.db.migrations import android.content.ContentValues import android.database.sqlite.SQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase +import io.github.wulkanowy.data.db.Converters +import io.github.wulkanowy.data.db.entities.Semester import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue import org.junit.Test +import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.of +import kotlin.test.assertFalse import kotlin.test.assertTrue class Migration13Test : AbstractMigrationTest() { @@ -97,11 +101,9 @@ class Migration13Test : AbstractMigrationTest() { close() } - helper.runMigrationsAndValidate(dbName, 13, true, Migration13()) + val db = helper.runMigrationsAndValidate(dbName, 13, true, Migration13()) - val db = getMigratedRoomDatabase() - - val semesters1 = db.semesterDao.loadAll(1, 5).blockingGet() + val semesters1 = getSemesters(db, "SELECT * FROM Semesters WHERE student_id = 1 AND class_id = 5") assertTrue { semesters1.single { it.isCurrent }.isCurrent } semesters1[0].run { assertFalse(isCurrent) @@ -119,7 +121,7 @@ class Migration13Test : AbstractMigrationTest() { assertEquals(2, diaryId) } - db.semesterDao.loadAll(2, 5).blockingGet().let { + getSemesters(db, "SELECT * FROM Semesters WHERE student_id = 2 AND class_id = 5").let { assertTrue { it.single { it.isCurrent }.isCurrent } assertEquals(1970, it[0].schoolYear) assertEquals(of(1970, 1, 1), it[0].end) @@ -130,7 +132,7 @@ class Migration13Test : AbstractMigrationTest() { assertTrue(it[3].isCurrent) } - db.semesterDao.loadAll(2, 5).blockingGet().let { + getSemesters(db, "SELECT * FROM Semesters WHERE student_id = 2 AND class_id = 5").let { assertTrue { it.single { it.isCurrent }.isCurrent } assertFalse(it[0].isCurrent) assertFalse(it[1].isCurrent) @@ -139,6 +141,30 @@ class Migration13Test : AbstractMigrationTest() { } } + private fun getSemesters(db: SupportSQLiteDatabase, query: String): List { + val semesters = mutableListOf() + + val cursor = db.query(query) + if (cursor.moveToFirst()) { + do { + semesters.add(Semester( + studentId = cursor.getInt(1), + diaryId = cursor.getInt(2), + diaryName = cursor.getString(3), + semesterId = cursor.getInt(4), + semesterName = cursor.getInt(5), + isCurrent = cursor.getInt(6) == 1, + classId = cursor.getInt(7), + unitId = cursor.getInt(8), + schoolYear = cursor.getInt(9), + start = Converters().timestampToDate(cursor.getLong(10))!!, + end = Converters().timestampToDate(cursor.getLong(11))!! + )) + } while (cursor.moveToNext()) + } + return semesters.toList() + } + private fun createStudent(db: SupportSQLiteDatabase, studentId: Int, schoolName: String = "", classId: Int = -1, schoolId: Int = 123) { db.insert("Students", SQLiteDatabase.CONFLICT_FAIL, ContentValues().apply { put("endpoint", "https://fakelog.cf") diff --git a/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt b/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt index ba6e151ef..4dcba3e15 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt @@ -6,6 +6,7 @@ import androidx.room.Room import androidx.room.RoomDatabase import androidx.room.RoomDatabase.JournalMode.TRUNCATE import androidx.room.TypeConverters +import androidx.room.migration.Migration import io.github.wulkanowy.data.db.dao.AttendanceDao import io.github.wulkanowy.data.db.dao.AttendanceSummaryDao import io.github.wulkanowy.data.db.dao.CompletedLessonsDao @@ -97,29 +98,33 @@ abstract class AppDatabase : RoomDatabase() { companion object { const val VERSION_SCHEMA = 17 + fun getMigrations(): Array { + return arrayOf( + Migration2(), + Migration3(), + Migration4(), + Migration5(), + Migration6(), + Migration7(), + Migration8(), + Migration9(), + Migration10(), + Migration11(), + Migration12(), + Migration13(), + Migration14(), + Migration15(), + Migration16(), + Migration17() + ) + } + fun newInstance(context: Context): AppDatabase { return Room.databaseBuilder(context, AppDatabase::class.java, "wulkanowy_database") .setJournalMode(TRUNCATE) .fallbackToDestructiveMigrationFrom(VERSION_SCHEMA + 1) .fallbackToDestructiveMigrationOnDowngrade() - .addMigrations( - Migration2(), - Migration3(), - Migration4(), - Migration5(), - Migration6(), - Migration7(), - Migration8(), - Migration9(), - Migration10(), - Migration11(), - Migration12(), - Migration13(), - Migration14(), - Migration15(), - Migration16(), - Migration17() - ) + .addMigrations(*getMigrations()) .build() } } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration17.kt b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration17.kt index 9276ed471..e2a2574db 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration17.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration17.kt @@ -6,6 +6,11 @@ import androidx.sqlite.db.SupportSQLiteDatabase class Migration17 : Migration(16, 17) { override fun migrate(database: SupportSQLiteDatabase) { + createGradesPointsStatisticsTable(database) + truncateSemestersTable(database) + } + + private fun createGradesPointsStatisticsTable(database: SupportSQLiteDatabase) { database.execSQL(""" CREATE TABLE IF NOT EXISTS GradesPointsStatistics( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -17,4 +22,8 @@ class Migration17 : Migration(16, 17) { ) """) } + + private fun truncateSemestersTable(database: SupportSQLiteDatabase) { + database.execSQL("DELETE FROM Semesters") + } }