GnuCash
Contact   Instructions
Bug 746937 - Template transaction splits are loaded in reverse order and then not sorted before saving
Summary: Template transaction splits are loaded in reverse order and then not sorted b...
Status: RESOLVED FIXED
Alias: None
Product: GnuCash
Classification: Unclassified
Component: Backend - XML (show other bugs)
Version: 2.6.x
Hardware: Other Linux
: Normal minor
Target Milestone: ---
Assignee: core
QA Contact: core
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-28 08:56 EDT by Simon Arlott
Modified: 2020-06-01 15:26 EDT (History)
5 users (show)

See Also:


Attachments
Patch against 2.6.4 (442 bytes, patch)
2015-03-28 08:56 EDT, Simon Arlott
jralls: rejected+
Details
Example GnuCash file 1 (14.19 KB, application/xml)
2015-07-23 15:54 EDT, Simon Arlott
no flags Details
Example GnuCash file 2 (14.19 KB, application/xml)
2015-07-23 15:56 EDT, Simon Arlott
no flags Details
Sort template transaction account splits when loading from XML files (2.6.19) (1.77 KB, patch)
2019-07-19 15:05 EDT, Simon Arlott
jralls: rejected+
Details
Sort template transaction account splits when loading from XML files (3.6) (1.82 KB, patch)
2019-07-19 15:22 EDT, Simon Arlott
jralls: rejected+
Details

Description Simon Arlott 2015-03-28 08:56:36 EDT
Created attachment 300514 [details]
Patch against 2.6.4

Template transaction splits are loaded in reverse order and then not sorted because the sort_dirty states of template accounts are only checked when the scheduled transaction editor is opened (via gnc_tree_model_account_get_value).

If there are two or more transactions for a template account then this causes them to get written in a different order every time the file is loaded and saved. This causes spurious changes to be detected when tracking changes to the XML file.

Before writing out the transactions for an account, xaccAccountSortSplits() should be called to ensure that the splits are sorted.
Comment 1 John Ralls 2015-07-12 14:03:47 EDT
Comment on attachment 300514 [details]
Patch against 2.6.4

Comment on attachment 300514 [details]
Patch against 2.6.4

I imagine you focused on the call to xaccAccountTreeForEachTransaction() in write_template_transaction_data() in io-gncxml-v2.c. Unfortunately it's also used for writing out (more than once) and reading the whole book, both of which are already pretty slow.

That aside, it takes a const Account*, so mutating the account inside the call isn't allowed.
Comment 2 John Ralls 2015-07-12 15:55:29 EDT
Not only that, I can't replicate the problem with on Debian or OSX. diffing a file and its backup doesn't show any changes in the gnc:template-transactions section of the file.

Perhaps my test file doesn't have a sufficient number of SXes to trigger it; please attach a test file that demonstrates the problem.
Comment 3 Simon Arlott 2015-07-23 15:55:00 EDT
Created attachment 308030 [details]
Example GnuCash file 1
Comment 4 Simon Arlott 2015-07-23 15:56:30 EDT
Created attachment 308031 [details]
Example GnuCash file 2

Tested with GnuCash 2.6.7:
1. Open example file 1
2. Save the file
3. The output file contains the same content as example file 2
4. Open example file 2
5. Save the file
6. The output file contains the same content as example file 1
Comment 5 John Ralls 2015-08-03 15:28:23 EDT
Got it. I still don't know if we can fix this without bogging down program write and load, but I'll look into it.
Comment 6 Simon Arlott 2019-07-16 14:31:28 EDT
Would it be possible to explicitly sort transactions in template transaction accounts on save if they haven't been sorted yet?
Comment 7 John Ralls 2019-07-16 20:04:26 EDT
Maybe, but maybe it would be cleaner to just maintain the template accounts in sorted order like the regular accounts.
Comment 8 Simon Arlott 2019-07-19 15:05:42 EDT
Created attachment 373329 [details]
Sort template transaction account splits when loading from XML files (2.6.19)
Comment 9 Simon Arlott 2019-07-19 15:22:14 EDT
Created attachment 373330 [details]
Sort template transaction account splits when loading from XML files (3.6)

The issue still occurs on 3.6 so I've made patches for both versions.

Passing FALSE as the value for a gpointer to gnc_account_foreach_descendant works but probably isn't the right way to do this.
Comment 10 John Ralls 2019-07-26 18:34:04 EDT
Comment on attachment 373329 [details]
Sort template transaction account splits when loading from XML files (2.6.19)

The 2.6 branch is closed to further development.
Comment 11 John Ralls 2019-07-27 18:21:17 EDT
Comment on attachment 373330 [details]
Sort template transaction account splits when loading from XML files (3.6)

Found it. xaccAccountBeginEdit() gets called on the template accounts but xaccAccountCommitEdit() wasn't, so the template splits were never sorted.

Fixed for 3.7.

Note You need to log in before you can comment on or make changes to this bug.