Skip to content
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
12,885 changes: 12,885 additions & 0 deletions app/schemas/org.piramalswasthya.sakhi.database.room.InAppDb/78.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ import org.piramalswasthya.sakhi.model.dynamicEntity.mosquitonetEntity.MosquitoN
GCReferralFollowUpEntity::class
],
views = [BenBasicCache::class],
version = 77, exportSchema = true
version = 78, exportSchema = true
)

@TypeConverters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.piramalswasthya.sakhi.database.room.migrations.MigrationHelpers.colum
// are linked when the beneficiary has exactly one General Check-up referral.
object Migration74To75 : Migration(74, 75) {
override fun migrate(database: SupportSQLiteDatabase) {
Migration73To74.migrate(database)
if (!columnExists(database, "gc_referral_follow_up", "referralReason")) {
database.execSQL("ALTER TABLE gc_referral_follow_up ADD COLUMN referralReason TEXT NOT NULL DEFAULT ''")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.piramalswasthya.sakhi.database.room.migrations

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

object Migration77To78 : Migration(77, 78) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE TABLE IF NOT EXISTS `gc_referral_follow_up` (" +
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"`benId` INTEGER NOT NULL, " +
"`houseHoldId` INTEGER NOT NULL, " +
"`referredOnDate` TEXT NOT NULL, " +
"`followUpDate` TEXT NOT NULL, " +
"`followUpStatus` TEXT, " +
"`fieldsJson` TEXT NOT NULL, " +
"`isSynced` INTEGER NOT NULL, " +
"`updatedAt` INTEGER NOT NULL, " +
"`referralReason` TEXT NOT NULL DEFAULT '', " +
"`referralMonth` INTEGER NOT NULL DEFAULT 0)"
)
database.execSQL(
"CREATE UNIQUE INDEX IF NOT EXISTS `index_gc_referral_follow_up_benId_referralReason_referralMonth` " +
"ON `gc_referral_follow_up` (`benId`, `referralReason`, `referralMonth`)"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ val ALL_MIGRATIONS: Array<Migration> = arrayOf(
Migration73To74,
Migration74To75,
Migration75To76,
Migration76To77
Migration76To77,
Migration77To78
)
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class InAppDbRecentMigrationsTest {
@Test
fun `migrations form one unbroken chain up to the database version`() {
val recent = ALL_MIGRATIONS.filter { it.startVersion >= 70 }.sortedBy { it.startVersion }
assertEquals((70..76).toList(), recent.map { it.startVersion })
assertEquals((70..77).toList(), recent.map { it.startVersion })
recent.forEach { assertEquals(it.startVersion + 1, it.endVersion) }
}

Expand Down Expand Up @@ -141,7 +141,9 @@ class InAppDbRecentMigrationsTest {
// The new key is created only after duplicates are removed.
assertTrue(
executedSql.indexOfFirst { it.startsWith("DELETE FROM gc_referral_follow_up") } <
executedSql.indexOfFirst { it.contains("CREATE UNIQUE INDEX") }
executedSql.indexOfFirst {
it.contains("CREATE UNIQUE INDEX") && it.contains("benId_referralReason_referralMonth")
}
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.piramalswasthya.sakhi.database.room.migrations

import android.database.Cursor
import androidx.sqlite.db.SupportSQLiteDatabase
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class Migration77To78Test {

private val executedSql = mutableListOf<String>()

private fun mockDb(existingColumns: List<String> = emptyList()): SupportSQLiteDatabase {
val db = mockk<SupportSQLiteDatabase>(relaxed = true)
every { db.execSQL(any<String>()) } answers {
executedSql.add(firstArg<String>())
Unit
}
every { db.query(any<String>()) } answers {
val cursor = mockk<Cursor>(relaxed = true)
var index = -1
every { cursor.moveToNext() } answers {
index += 1
index < existingColumns.size
}
every { cursor.getColumnIndexOrThrow("name") } returns 0
every { cursor.getString(0) } answers { existingColumns[index] }
cursor
}
return db
}

@Test
fun `migrates from 77 to 78`() {
assertEquals(77, Migration77To78.startVersion)
assertEquals(78, Migration77To78.endVersion)
}

@Test
fun `creates the gc referral follow up table with its final columns when missing`() {
Migration77To78.migrate(mockDb())

assertEquals(2, executedSql.size)
val createTable = executedSql[0]
assertTrue(createTable.startsWith("CREATE TABLE IF NOT EXISTS `gc_referral_follow_up`"))
assertTrue(createTable.contains("`referralReason` TEXT NOT NULL DEFAULT ''"))
assertTrue(createTable.contains("`referralMonth` INTEGER NOT NULL DEFAULT 0"))
assertTrue(createTable.contains("`followUpStatus` TEXT,"))
assertEquals(
"CREATE UNIQUE INDEX IF NOT EXISTS `index_gc_referral_follow_up_benId_referralReason_referralMonth` " +
"ON `gc_referral_follow_up` (`benId`, `referralReason`, `referralMonth`)",
executedSql[1]
)
}

@Test
fun `74 to 75 creates the table before altering it when an earlier build skipped it`() {
Migration74To75.migrate(mockDb())

assertTrue(executedSql[0].startsWith("CREATE TABLE IF NOT EXISTS `gc_referral_follow_up`"))
assertTrue(executedSql.indexOfFirst { it.startsWith("ALTER TABLE gc_referral_follow_up") } > 0)
}

@Test
fun `74 to 75 skips the column adds when the columns already exist`() {
Migration74To75.migrate(mockDb(listOf("referralReason", "referralMonth")))

assertTrue(executedSql.none { it.startsWith("ALTER TABLE") })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class MigrationsTest {
private fun pairs() = ALL_MIGRATIONS.map { it.startVersion to it.endVersion }

@Test
fun `registry lists all 64 migrations`() {
assertEquals(64, ALL_MIGRATIONS.size)
fun `registry lists all 65 migrations`() {
assertEquals(65, ALL_MIGRATIONS.size)
}

@Test
Expand All @@ -27,9 +27,9 @@ class MigrationsTest {
}

@Test
fun `oldest supported version is 13 and newest is 77`() {
fun `oldest supported version is 13 and newest is 78`() {
assertEquals(13, ALL_MIGRATIONS.minOf { it.startVersion })
assertEquals(77, ALL_MIGRATIONS.maxOf { it.endVersion })
assertEquals(78, ALL_MIGRATIONS.maxOf { it.endVersion })
}

@Test
Expand All @@ -39,28 +39,28 @@ class MigrationsTest {
}

@Test
fun `every version from 13 to 76 has a path up to 77`() {
fun `every version from 13 to 77 has a path up to 78`() {
val byStart = ALL_MIGRATIONS.groupBy { it.startVersion }
for (start in 13..76) {
for (start in 13..77) {
var version = start
while (version < 77) {
while (version < 78) {
val next = byStart[version]?.maxOf { it.endVersion }
assertNotNull("no migration from $version", next)
version = next!!
}
assertEquals(77, version)
assertEquals(78, version)
}
}

@Test
fun `current production version 64 upgrades through 65 to 77 one step at a time`() {
fun `current production version 64 upgrades through 65 to 78 one step at a time`() {
val path = mutableListOf(64)
var version = 64
while (version < 77) {
while (version < 78) {
version = ALL_MIGRATIONS.single { it.startVersion == version }.endVersion
path.add(version)
}
assertEquals(listOf(64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77), path)
assertEquals(listOf(64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78), path)
}

@Test
Expand All @@ -75,5 +75,6 @@ class MigrationsTest {
assertTrue(ALL_MIGRATIONS.contains(Migration74To75))
assertTrue(ALL_MIGRATIONS.contains(Migration75To76))
assertTrue(ALL_MIGRATIONS.contains(Migration76To77))
assertTrue(ALL_MIGRATIONS.contains(Migration77To78))
}
}
Loading