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

Include index_name column in index for tables used in sorting #2753

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1,032 changes: 1,032 additions & 0 deletions engine/schemas/com.google.android.fhir.db.impl.ResourceDatabase/10.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -418,6 +418,72 @@ class ResourceDatabaseMigrationTest {
assertThat(retrievedTask).isEqualTo(bedNetTask)
}

@Test
fun migrate9To10_should_execute_with_no_exception(): Unit = runBlocking {
val patient1Id = "patient-001"
val patient1ResourceUuid = "e2c79e28-ed4d-4029-a12c-108d1eb5bedb"
val patient1: String =
Patient()
.apply {
id = patient1Id
addName(HumanName().apply { addGiven("Brad") })
}
.let { iParser.encodeResourceToString(it) }

val patient2Id = "patient-002"
val patient2ResourceUuid = "541782b3-48f5-4c36-bd20-cae265e974e7"
val patient2: String =
Patient()
.apply {
id = patient2Id
addName(HumanName().apply { addGiven("Alex") })
}
.let { iParser.encodeResourceToString(it) }

helper.createDatabase(DB_NAME, 9).apply {
execSQL(
"INSERT INTO ResourceEntity (resourceUuid, resourceType, resourceId, serializedResource) VALUES ('$patient1ResourceUuid', 'Patient', '$patient1', '$patient1');",
)
execSQL(
"INSERT INTO ResourceEntity (resourceUuid, resourceType, resourceId, serializedResource) VALUES ('$patient2ResourceUuid', 'Patient', '$patient2', '$patient2');",
)

close()
}

val migratedDatabase = helper.runMigrationsAndValidate(DB_NAME, 10, true, Migration_9_10)

val patientResult1: String?
val patientResult2: String?

migratedDatabase.let { database ->
database
.query(
"""
SELECT a.serializedResource
FROM ResourceEntity a
LEFT JOIN StringIndexEntity b
ON a.resourceUuid = b.resourceUuid AND b.index_name = 'name'
WHERE a.resourceType = 'Patient'
GROUP BY a.resourceUuid
HAVING MAX(IFNULL(b.index_value,0)) >= -9223372036854775808
ORDER BY IFNULL(b.index_value, -9223372036854775808) ASC
"""
.trimIndent(),
)
.let {
it.moveToFirst()
patientResult1 = it.getString(0)
it.moveToNext()
patientResult2 = it.getString(0)
}
}
migratedDatabase.close()

assertThat(patientResult1).isEqualTo(patient2)
assertThat(patientResult2).isEqualTo(patient1)
}

companion object {
const val DB_NAME = "migration_tests.db"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,6 +115,7 @@ internal class DatabaseImpl(
MIGRATION_6_7,
MIGRATION_7_8,
Migration_8_9,
Migration_9_10,
)
}
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,7 @@ import org.json.JSONObject
PositionIndexEntity::class,
LocalChangeResourceReferenceEntity::class,
],
version = 9,
version = 10,
exportSchema = true,
)
@TypeConverters(DbTypeConverters::class)
Expand Down Expand Up @@ -226,3 +226,32 @@ internal val Migration_8_9 =
}
}
}

internal val Migration_9_10 =
object : Migration(9, 10) {
override fun migrate(database: SupportSQLiteDatabase) {
database.beginTransaction()
try {
database.execSQL("DROP INDEX IF EXISTS `index_DateIndexEntity_resourceUuid`;")
database.execSQL("DROP INDEX IF EXISTS `index_DateTimeIndexEntity_resourceUuid`;")
database.execSQL("DROP INDEX IF EXISTS `index_NumberIndexEntity_resourceUuid`;")
database.execSQL("DROP INDEX IF EXISTS `index_StringIndexEntity_resourceUuid`;")

database.execSQL(
"CREATE INDEX IF NOT EXISTS `index_DateIndexEntity_resourceUuid_index_name_index_from` ON `DateIndexEntity` (`resourceUuid`, `index_name`, `index_from`);",
)
database.execSQL(
"CREATE INDEX IF NOT EXISTS `index_DateTimeIndexEntity_resourceUuid_index_name_index_from` ON `DateTimeIndexEntity` (`resourceUuid`, `index_name`, `index_from`);",
)
database.execSQL(
"CREATE INDEX IF NOT EXISTS `index_NumberIndexEntity_resourceUuid_index_name_index_value` ON `NumberIndexEntity` (`resourceUuid`, `index_name`, `index_value`);",
)
database.execSQL(
"CREATE INDEX IF NOT EXISTS `index_StringIndexEntity_resourceUuid_index_name_index_value` ON `StringIndexEntity` (`resourceUuid`, `index_name`, `index_value`);",
)
database.setTransactionSuccessful()
} finally {
database.endTransaction()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,7 @@ import org.hl7.fhir.r4.model.ResourceType
// https://github.com/google/android-fhir/issues/2040
Index(value = ["resourceType", "index_name", "resourceUuid", "index_from", "index_to"]),
// Keep this index for faster foreign lookup
Index(value = ["resourceUuid"]),
Index(value = ["resourceUuid", "index_name", "index_from"]),
],
foreignKeys =
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,7 +34,7 @@ import org.hl7.fhir.r4.model.ResourceType
// https://github.com/google/android-fhir/issues/2040
Index(value = ["resourceType", "index_name", "resourceUuid", "index_from", "index_to"]),
// Keep this index for faster foreign lookup
Index(value = ["resourceUuid"]),
Index(value = ["resourceUuid", "index_name", "index_from"]),
],
foreignKeys =
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Google LLC
* Copyright 2021-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@ import org.hl7.fhir.r4.model.ResourceType
[
Index(value = ["resourceType", "index_name", "index_value"]),
// keep this index for faster foreign lookup
Index(value = ["resourceUuid"]),
Index(value = ["resourceUuid", "index_name", "index_value"]),
],
foreignKeys =
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 Google LLC
* Copyright 2021-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@ import org.hl7.fhir.r4.model.ResourceType
[
Index(value = ["resourceType", "index_name", "index_value"]),
// keep this index for faster foreign lookup
Index(value = ["resourceUuid"]),
Index(value = ["resourceUuid", "index_name", "index_value"]),
],
foreignKeys =
[
Expand Down
Loading