(core) Fix undo error for automatically removed rows, especially in summary tables

Summary:
Fixes a bug noted here: https://grist.slack.com/archives/C069RUP71/p1662564341132349

This bug could happen quite easily as follows:

1. Have a formula in a summary table such as `$group.amount`. Typically there's also a `SUM` but that's not essential.
2. Find a group with nonzero values of `amount`.
3. Delete all rows in that group in the source table. Typically that just means one row in a lonely group.
4. The summary table row is automatically deleted.
5. Try to undo. This raises an error about trying to update a non-existent summary table row.

I tried to account for this undo problem in https://phab.getgrist.com/D3489 by not saving the updated value for `$group` when it was found to be empty. The reason this was insufficient is that `$group.amount` is immediately invalidated anyway when the source row(s) are deleted (I think because that's just how dependency relations involving references work) *and* the calculated value of `$group.amount` changes even if `$group` doesn't. For example, `$group.amount` may have previously been `[100, 200]`. After deleting the rows, `$group.amount` becomes `[0, 0]`. Keeping `$group` unchanged prevents `$group.amount` from just being `[]`, but deleting the source rows means that the amounts become the numeric default `0` which is still a change. This change in value is then noted which leads to saving an undo action to update the summary table record. All this happens in step 3 above, and the summary record is only deleted after that point.

This diff removes that special handling for `group` and instead adds a more general fix to `action_summary.py`. This inserts undo actions for deleted rows at the beginning of the undo list rather than at the end, which was already done for deleted tables and columns.

Test Plan: Python tests

Reviewers: dsagal

Reviewed By: dsagal

Subscribers: dsagal

Differential Revision: https://phab.getgrist.com/D3626
This commit is contained in:
Alex Hall
2022-09-09 20:41:31 +02:00
parent c9a81ea7ea
commit 56624c4a95
7 changed files with 113 additions and 42 deletions

View File

@@ -280,6 +280,24 @@ class TestDerived(test_engine.EngineTestCase):
[5, 2007, 1, 15.0, [1]],
])
self.assertPartialOutActions(out_actions_update, {
'stored': [
['UpdateRecord', 'Orders', 1, {'year': 2007}],
['AddRecord', 'Orders_summary_year', 5, {'year': 2007}],
['RemoveRecord', 'Orders_summary_year', 1],
['UpdateRecord', 'Orders_summary_year', 5, {'amount': 15.0}],
['UpdateRecord', 'Orders_summary_year', 5, {'count': 1}],
['UpdateRecord', 'Orders_summary_year', 5, {'group': ['L', 1]}],
],
'undo': [
['UpdateRecord', 'Orders_summary_year', 1, {'group': ['L', 1]}],
['UpdateRecord', 'Orders_summary_year', 1, {'count': 1}],
['UpdateRecord', 'Orders_summary_year', 1, {'amount': 15.0}],
['UpdateRecord', 'Orders', 1, {'year': 2012}],
['RemoveRecord', 'Orders_summary_year', 5],
['AddRecord', 'Orders_summary_year', 1, {'group': ['L'], 'year': 2012}],
]})
# Undo and ensure that the new line is gone from the summary table.
out_actions_undo = self.apply_undo_actions(out_actions_update.undo)
self.assertPartialData("Orders_summary_year", ["id", "year", "count", "amount", "group" ], [
@@ -289,13 +307,7 @@ class TestDerived(test_engine.EngineTestCase):
[4, 2015, 4, 106.0, [7,8,9,10]],
])
self.assertPartialOutActions(out_actions_undo, {
"stored": [
actions.AddRecord("Orders_summary_year", 1, {
"amount": 15.0, "count": 1, "group": [1], "year": 2012
}),
actions.RemoveRecord("Orders_summary_year", 5),
actions.UpdateRecord("Orders", 1, {"year": 2012}),
],
"stored": out_actions_update.undo[::-1],
"calls": {
"Orders_summary_year": {
"#lookup#": 1, "#lookup#year": 1, "group": 1, "amount": 1, "count": 1