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

Fix property names clashing with DB column names #6495

Merged
merged 4 commits into from
Nov 6, 2024

Conversation

seadowg
Copy link
Member

@seadowg seadowg commented Nov 5, 2024

Closes #6486

Why is this the best possible solution? Were any other approaches considered?

Other alternatives might have been to index properties in some way (in the lists table for example), but using a prefix felt like the simplest way to fix this to me. Even if a property clashes with the prefix (a property named p_id for example) we would still add/remove the prefix correctly as it's isolated to DatabaseEntitiesRepository (the previous example would end up with a column named p_p_id).

We also decided (on a call) to go with just clearing out the entities DB when upgrading rather than writing proper migration code. This mitigates any risk of migrating data incorrectly, and has minimal impact as the entities DB only exists for beta users at this time.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

Along with fixing the bug, any local entities on device will be wiped. It's definitely worth playing with upgrades from the last beta to check this works as expected.

Before submitting this PR, please make sure you have:

  • added or modified tests for any new or changed behavior
  • run ./gradlew connectedAndroidTest (or ./gradlew testLab) and confirmed all checks still pass
  • added a comment above any new strings describing it for translators
  • added any new strings with date formatting to DateFormatsTest
  • verified that any code or assets from external sources are properly credited in comments and/or in the about file.
  • verified that any new UI elements use theme colors. UI Components Style guidelines

@lognaturel lognaturel added the high priority Should be looked at before other PRs/issues label Nov 5, 2024
Copy link
Member

@lognaturel lognaturel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be human-readable so let's go with an even shorter prefix like p_

@seadowg seadowg requested a review from lognaturel November 5, 2024 17:26
@seadowg seadowg marked this pull request as ready for review November 5, 2024 17:32
@seadowg seadowg requested a review from grzesiek2010 November 5, 2024 17:32
@lognaturel lognaturel removed their request for review November 5, 2024 17:53
@lognaturel lognaturel dismissed their stale review November 5, 2024 17:53

Looks great, thank you!

val savedEntity = repository.getEntities("things")[0]
assertThat(savedEntity, sameEntityAs(entity))

repository.save("things", savedEntity)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to save twice?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're checking creating and updating.

@@ -15,4 +19,21 @@ class DatabaseEntitiesRepositoryTest : EntitiesRepositoryTest() {
TempFiles.createTempDir().absolutePath
)
}

@Test
fun `#save supports properties with db column names saving new entities and updating existing ones`() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't it be simply: #save supports properties with db column names?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -399,29 +397,28 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
}
}

private fun addPropertiesToContentValues(contentValues: ContentValues, entity: Entity) {
entity.properties.forEach { (name, value) ->
contentValues.put("\"${EntitiesTable.getPropertyColumn(name)}\"", value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be just:
contentValues.put(EntitiesTable.getPropertyColumn(name), value)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll see that #save supports properties with dots and dashes when saving new entities and updating existing ones fails if you change to that. We need to quote the column property column names as they might contain special characters that SQL queries don't support.

@seadowg seadowg requested a review from grzesiek2010 November 6, 2024 10:38
@grzesiek2010 grzesiek2010 merged commit 90d41ae into getodk:master Nov 6, 2024
6 checks passed
@seadowg seadowg deleted the prop-prefix branch November 6, 2024 13:50
@WKobus
Copy link

WKobus commented Nov 8, 2024

Tested with Success

Verified on device with Android 15
Dominika also verified on device with Android 10

Verified cases:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
behavior verified high priority Should be looked at before other PRs/issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Entity property "_id" crashes the app after refreshing the list of blank forms
4 participants