Commit graph

2679 commits

Author SHA1 Message Date
Sagar Vora
56c7295b8c
perf: commit only if file is imported (#33114) 2025-06-26 15:51:03 +05:30
Faris Ansari
d584b8691d
Merge pull request #32381 from netchampfaris/api-v2-fixes
feat!: Apply permissions in frappe.qb.get_query

Enhancements:
- Supports applying permissions
- Strict input parsing

BREAKING CHANGE:
```py
# Before
frappe.qb.get_query("Task", fields=["sum(is_completed) as count"])

# After
frappe.qb.get_query("Task", fields=[{"SUM": "is_completed", "as": "count"}])
```
2025-06-23 15:50:25 +05:30
Sagar Vora
18dde69ab0 perf: dont serialize caches 2025-06-21 20:54:11 +05:30
Sagar Vora
d9fc9f21f9 perf: faster meta serialisation 2025-06-21 20:12:39 +05:30
Faris Ansari
09ae9ad086 fix: rename method typing 2025-06-18 15:51:13 +05:30
Sagar Vora
b3e1eda4c8
feat: global frappe.in_test flag (#32960)
* feat: global `frappe.in_test` flag

* feat: helper utility to toggle `frappe.in_test`

* fix: use `toggle_test_mode` util

* fix: use `frappe.in_test`

* chore: add comment explaining global `in_test`

* chore: ignore commit replacing flag usage

* test: temporarily disable `frappe.in_test`

this worked earlier because flag was set in werkzeug.local which was separate for API test client

* test: add comment explaining change
2025-06-17 19:19:31 +05:30
Sagar Vora
5210348322 perf: assume key to be table fieldname 2025-06-14 01:49:07 +05:30
Sagar Vora
d79ca9560b chore: revert _init_* methods of document 2025-06-13 22:24:03 +05:30
Ankush Menat
b162c0d6ec
fix!: Don't update document from keyword args (#32898)
Right now if you do this:

`get_doc(doctype, name, field=value)` then it loads document from DB and
then updates field to value.

This is absurd API and I don't think it was ever done intentionally.
2025-06-11 10:15:35 +00:00
Ankush Menat
44681433c9
fix: for_update for lazy loaded documents (#32897)
* fix: Support for_update on lazy documents

* fix: Pop for_update from arguments

* fix: Don't init empty child tables
2025-06-11 10:02:36 +00:00
Sagar Vora
a6b2f7c991
perf: dont override doc.extend (#32890) 2025-06-11 07:26:46 +00:00
Ankush Menat
893a87df86 fix: append and extend without touching attributes 2025-06-11 10:07:48 +05:30
Ankush Menat
10c3c9ea9b fix: Limit getattr to lazy loaded child tables only 2025-06-11 09:55:45 +05:30
Ankush Menat
5c886ef091 test: test internal implementation of LazyChildTable 2025-06-11 09:55:45 +05:30
Ankush Menat
26cf77562c refactor: rearrange checks, inline and cast super args
Co-authored-by: Sagar Vora <16315650+sagarvora@users.noreply.github.com>
2025-06-11 09:55:45 +05:30
Ankush Menat
e69f79e2ae fix: Support dict as key ~ filters
Triggers full fetch indirectly, but there's no other option for this.
2025-06-11 09:55:45 +05:30
Ankush Menat
bebabc3211 fix: Handle support for DocType and virtual
By falling back to original impl with warning.
This is just to allow "fearless usage".
2025-06-11 09:55:45 +05:30
Ankush Menat
49f582ae57 perf: Use lazy doc in a lot more places 2025-06-11 09:55:45 +05:30
Ankush Menat
4d7348d3c0 refactor: avoid code duplication 2025-06-11 09:55:36 +05:30
Ankush Menat
5b8f7bba2b fix: Ensure some basic checks are present to prevent misuse 2025-06-11 09:55:01 +05:30
Ankush Menat
3e4e944ae9 fix: dont db_update what hasn't changed 2025-06-11 09:55:01 +05:30
Ankush Menat
022dbf444d fix: make doc.save work and empty tables 2025-06-11 09:55:01 +05:30
Ankush Menat
eb77ddab69 feat: Lazy loaded documents
https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance

After first invokation, object's __dict__ will be used... then we can
assume rest of the code works as expected (?)
2025-06-11 09:55:01 +05:30
Ankush Menat
e4bc7f361b
Revert: DocRef (#32866)
- Hardly used anywhere
- Too many hardcoded `__value__` calls without which it's not usable.
- Another type to worry about
2025-06-10 05:20:56 +00:00
Ankush Menat
49a11a0112
Merge pull request #32865 from ankush/check_link_value
fix: Assert type of link field values
2025-06-10 10:22:00 +05:30
Ankush Menat
22827056ce fix: Assert type of link field values 2025-06-10 10:10:51 +05:30
Sagar Vora
7c4b6b3dc1 fix: check permissions as per specified user 2025-06-09 23:31:50 +05:30
Ankush Menat
8c1ee68ce6
perf: Skip order_by in link validation (#32848)
~10% faster because no QB overhead
2025-06-09 07:35:00 +00:00
Ankush Menat
f9ff807e4c
refactor: Rename get_settings -> get_single_value (#32840)
Not all single doctypes are settings, so this is better. Implicit
caching is fine, same is done for `db` APIs on singles. We *should* aim
for 100% correctness of caching implementation, especially for singles.

Thanks to @netchampfaris for the suggestion.
2025-06-09 04:20:29 +00:00
Ankush Menat
caf415f13e
feat: get_settings (#32821)
* feat: get_settings

get_cached_value doesn't work well with singles because you either need
to pass `None` or repeat doctype name... both are awekward and easy to
shoot yourself in foot with.

* refactor: Use cached settings
2025-06-06 18:34:17 +05:30
Ankush Menat
515d9006a4 refactor: simplify link field checks
Also: perf: only fetch docstatus if required

This will let us do index-only scans.
2025-06-05 21:38:12 +05:30
Ankush Menat
4dd21f175e perf: Avoid seprately fetching docstatus 2025-06-05 15:24:29 +05:30
Ankush Menat
c434f4ca14 Revert "Revert "perf: cache fetch from value too (#32700)" (#32708)"
This reverts commit 8a3f65338c.
2025-06-05 15:24:29 +05:30
Ankush Menat
47a47a9b5d refactor!: Change internal datastructure of db.value_cache
It's now a defaultdictionary of `[doctype][name/filters][fieldname]`

This allows us to implement granular clearing and improve usage of this cache.
2025-06-05 14:46:03 +05:30
Ankush Menat
a3d5b4af77
Revert "perf: cache docstatus check for invalid links" (#32799)
This reverts commit be5c96acf22f208c4ec8fbcde92a721b0e84d561.
2025-06-05 08:52:57 +00:00
Ankush Menat
ddbaf09125
fix: Standard field falsy comparisons in db_query (#32791)
Extends the fix to standard fields. e0f63a928f
2025-06-05 09:55:05 +05:30
Ankush Menat
7d7d77d762
perf: misc small cruft (#32778)
* perf: cache docstatus check for invalid links

* perf: avoid querying if doctype is single

* perf: cache is_single
2025-06-04 19:18:19 +05:30
Ankush Menat
bc63c17287
perf: don't load doc before save for child tables (#32770)
Consider this:

```python
for row in doc.get_children():
    row.db_set("amount", 0)
```

This sounds like it will do one write query for each row but it does 2
because of this unnecessary locking of child tables.
2025-06-04 07:52:37 +00:00
Ankush Menat
c78e31cb0b
perf: cache link validation for new doc (#32747) 2025-06-02 06:11:26 +00:00
Ankush Menat
f17658c4c6
fix: Sample link counts and flush frequently (#32713)
We recently applied limit on how many links can be buffered. That
pretty much "samples" only records created at start of the hour.

This change makes it flush 4x frequently and samples 10% of input to
reduce updates. Again, statistically this serves same purpose.
2025-05-29 12:18:28 +05:30
Ankush Menat
4d8ebcce29
perf: Apply backpressure on link counts (#32712)
Context: This is QoL feature to highlight most used items. But in high
throughput environments where a lot of new documents are being created
this becomes a bottleneck.

Fix: Limit the size of counts that can be buffered before they're flushed.

Statistically this will still work just as well as it did before.
2025-05-29 11:55:35 +05:30
Ankush Menat
8a3f65338c
Revert "perf: cache fetch from value too (#32700)" (#32708)
This reverts commit f8e9e82260.
2025-05-28 13:31:07 +00:00
Ankush Menat
ba96408d23
fix: checkboxes can never be mandatory (#32707) 2025-05-28 13:24:49 +00:00
Ankush Menat
f8e9e82260
perf: cache fetch from value too (#32700)
Consider any "items" table in ERNext, same item and warehouses are used
10s of times in a single document, every one of them triggers fetch from
query, even though it can't possibly change during the DB transaction.

This change now caches the fetched value too.
2025-05-28 15:56:52 +05:30
Ankush Menat
78f7fa1429
perf: hoist constant computations for child table column order (#32677)
These don't/shouldn't change during execution of the function.
2025-05-26 19:39:29 +05:30
Ankush Menat
e2d619504f
perf: batch bulk_insert (#32675)
* fix: reduce bulk insert batch size

Back when this feature was added it used to lazily evaluate the input.
Now the iterator is consumed upfront so large batch sizes == huge memory usage.

* perf: bring back iterator for bulk_insert

Bulk insert used to support iterator for consuming arbitrarily large
amount of data and inserting it. Since child table support was added, it
can't do it anymore because that requires collecting values.

This change now brings back iterators by batching input iterator (by
default 1000) documents.

This is almost as good as original change from design POV. Performance
is still meh for flat documents.
2025-05-26 13:36:53 +00:00
Ankush Menat
9920c8d033 fix: duplicate follow
This exists in `doc.insert()` not need to add it here too
2025-05-26 14:43:06 +05:30
Ankush Menat
c2d2fda212 perf: avoid clearing cache for newly imported documents 2025-05-26 14:42:54 +05:30
Ankush Menat
d3c88556bc perf: Skip version checking on new docs 2025-05-26 14:38:26 +05:30
Ankush Menat
cc1e843260 perf: Skip realtime notifications while importing 2025-05-26 14:31:46 +05:30