WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@personalizedrefrigerator
Copy link
Collaborator

Summary

This pull request adds an ORDER BY counter to a query. Previously, the order in which changes were processed in some situations was undefined (though on my system, PostgreSQL seems to have returned the changes in the correct order).

As per the PostgreSQL documentation, when no order is specified,

If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce. (See ORDER BY Clause below.)

https://www.postgresql.org/docs/18/sql-select.html#:~:text=If%20the%20ORDER,below%2E%29

Testing plan

With PostgreSQL,

  1. Run DELETE FROM key_values WHERE key='ShareService::latestProcessedChange'; to force the share service to re-process all changes.
  2. Apply the following patch to ChangeModel.ts:
diff --git a/packages/server/src/models/ChangeModel.ts b/packages/server/src/models/ChangeModel.ts
index d19f70636..dab9a034d 100644
--- a/packages/server/src/models/ChangeModel.ts
+++ b/packages/server/src/models/ChangeModel.ts
@@ -87,6 +87,15 @@ export default class ChangeModel extends BaseModel<Change> {
  	  let results: Change[] = await query;
  	  const hasMore = !!results.length;
  	  const cursor = results.length ? results[results.length - 1].id : id;
+
+		let last = results[0];
+		for (const item of results) {
+			if (last.counter > item.counter) {
+				throw new Error('Results are in the wrong order.');
+			}
+			last = item;
+		}
+
  	  results = await this.removeDeletedItems(results);
  	  results = await this.compressChanges_(results);
  	  return {
  1. Start Joplin Server and verify that there are no "Results are in the wrong order" errors.
    • Note: Since the share service is re-processing items, there may be errors similar to "ShareModel: Could not remove a user item because it has already been removed". To show only output containing "wrong order", run DB_CLIENT=pg yarn start --env dev 2>&1 | grep -i 'wrong order'.

@personalizedrefrigerator personalizedrefrigerator changed the title Chore: Server: Ensure that shared items are processed in the correct order Server: Ensure that shared items are processed in the correct order Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant