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
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.

""".trimIndent(),
null
)
Expand All @@ -301,6 +302,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
SELECT *, i.$ROW_ID
FROM $list e, ${getRowIdTableName(list)} i
WHERE e._id = i._id AND $selectionColumn = ?
ORDER BY i.$ROW_ID
""".trimIndent(),
arrayOf(selectionArg)
)
Expand All @@ -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.

""".trimIndent()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,21 @@ abstract class EntitiesRepositoryTest {

@Test
fun `#save assigns an index to each entity in insert order when saving multiple entities`() {
val first = Entity.New("1", "Léoville Barton 2008")
val second = Entity.New("2", "Pontet Canet 2014")
/**
* first and second have alphabetically out of order IDs/names here so that any indexing on
* them is tested. We'd likely never see this fail if they were ordered.
*/
val first = Entity.New("2", "B")
val second = Entity.New("1", "A")

val repository = buildSubject()
repository.save("wines", first, second)

val entities = repository.getEntities("wines")
assertThat(entities[0].index, equalTo(0))
assertThat(entities[0].id, equalTo(first.id))
assertThat(entities[1].index, equalTo(1))
assertThat(entities[1].id, equalTo(second.id))
}

@Test
Expand Down