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

Order entities by insertion #6408

Merged
merged 3 commits into from
Sep 11, 2024
Merged

Conversation

seadowg
Copy link
Member

@seadowg seadowg commented Sep 11, 2024

This makes sure entities are in insertion order (from the CSV and locally) by changing the way we generate index so that it's ordered. It appeared that index ordering was based on entity id which would appear "random" (as they would usually be UUIDs).

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

Comments inline.

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?

I've already checked that EntitiesBenchmarkTest still passes so it seems like we're ok in terms of performance. Other than that, I think the main thing is to check that the entities are always in the "expected" order in selects (filtered and unfiltered).

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

@@ -284,6 +284,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
SELECT *, i.$ROW_ID
FROM $list e, ${getRowIdTableName(list)} i
WHERE e._id = i._id
ORDER BY i.$ROW_ID
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we strictly need the ORDER BY here and in the other queryWithAttachedRowId overload and I can't work out a way to test it (I think the query is structured so that it always happens to works out). I feel safer having it there though so.

@seadowg seadowg changed the title Order entities by index Order entities by insertion Sep 11, 2024
@@ -325,7 +327,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep

writableDatabase.execSQL(
"""
CREATE TABLE ${getRowIdTableName(it)} AS SELECT _id FROM $it;
CREATE TABLE ${getRowIdTableName(it)} AS SELECT _id FROM $it ORDER BY _id;
Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I understand it, _id should have the same order as insertion.

Copy link
Member

Choose a reason for hiding this comment

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

There is a potential issue with this assumption: if we delete some entities, their IDs might be reused. While this shouldn't occur under normal circumstances, once we reach the maximum possible ID value of 9223372036854775807, IDs will start to be reused. However, this number is large enough that we don't need to worry about it for now I guess. Do you agree @lognaturel?

Copy link
Member

Choose a reason for hiding this comment

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

Ahh the table gets recreated every time we add/delete an entity so it won't be and issue.

Copy link
Member

Choose a reason for hiding this comment

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

Although, as mentioned above, this is not an issue at the moment, it might be a good idea to add a test that serves as documentation hmm?

Copy link
Member

@lognaturel lognaturel Sep 11, 2024

Choose a reason for hiding this comment

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

Preserving order is a nice to have more than a must have so I think it's ok to leave edge cases untested. It's only a nice to have because the order is not necessarily meaningful currently. Longer-term, we'll need to be able to let users specify a custom sort order.

@seadowg seadowg marked this pull request as ready for review September 11, 2024 10:44
@lognaturel
Copy link
Member

We can merge this, right, @grzesiek2010? I'll let you do it just in case you do still want to add a test!

@grzesiek2010 grzesiek2010 merged commit 3f94179 into getodk:master Sep 11, 2024
6 checks passed
@WKobus
Copy link

WKobus commented Sep 16, 2024

Tested with Success!

Verified on device with Android 14

Verified cases:

  • Order of entities after creating entity locally
  • Order of entities after downloading CSV from central
  • Order of entities in forms with filtered selects

@dbemke
Copy link

dbemke commented Sep 16, 2024

Tested with Success!

Verified on device with Android 10

@seadowg seadowg deleted the order-by-entities branch October 7, 2024 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants