fix(patch): Merge roles: bulk insert (per chunk) (backport #6422)#6426
Conversation
(cherry picked from commit 8d03eb1)
|
| Filename | Overview |
|---|---|
| press/patches/v0_8_0/merge_press_admin_member_roles.py | Bulk-insert optimization: query object built and run once per chunk instead of per user; hash length bumped 8→10; timestamps are now shared across all users in a chunk. |
Sequence Diagram
sequenceDiagram
participant Patch as execute()
participant DB as Database
Patch->>DB: SELECT users without Press User role
DB-->>Patch: users[]
loop for each chunk of 100 users
Patch->>Patch: build INSERT query with all chunk rows
Patch->>DB: INSERT INTO has_role VALUES (u1), (u2), ...
DB-->>Patch: ok
Patch->>DB: COMMIT
end
Patch->>DB: DELETE FROM has_role WHERE role IN old_roles
Patch->>DB: "UPDATE has_role SET role=Press User"
loop for each old role
Patch->>DB: "UPDATE role SET disabled=1"
end
Patch->>DB: COMMIT
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
press/patches/v0_8_0/merge_press_admin_member_roles.py:51-65
If `total` is 0 the outer loop never executes, which is fine. But if PyPika's accumulated `query` object (with no `.insert()` calls) were ever reached, `.run()` would emit a bare `INSERT INTO has_role (...) VALUES` with no rows — a SQL syntax error. The inner loop guard prevents this today, but adding an explicit check makes the intent clear and protects against future refactors that might reorder the loops.
```suggestion
for i, user in enumerate(chunk, start=chunk_start):
name = frappe.generate_hash(length=10)
query = query.insert(
name,
now_str,
now_str,
"Administrator",
"Administrator",
user,
"roles",
"User",
"Press User",
)
update_progress_bar("Updating users", i, total)
if chunk:
query.run()
```
Reviews (1): Last reviewed commit: "fix(patch): Merge roles: bulk insert (pe..." | Re-trigger Greptile
| @@ -59,8 +60,9 @@ def execute(): | |||
| "roles", | |||
| "User", | |||
| "Press User", | |||
| ).run() | |||
| ) | |||
| update_progress_bar("Updating users", i, total) | |||
| query.run() | |||
There was a problem hiding this comment.
If
total is 0 the outer loop never executes, which is fine. But if PyPika's accumulated query object (with no .insert() calls) were ever reached, .run() would emit a bare INSERT INTO has_role (...) VALUES with no rows — a SQL syntax error. The inner loop guard prevents this today, but adding an explicit check makes the intent clear and protects against future refactors that might reorder the loops.
| for i, user in enumerate(chunk, start=chunk_start): | |
| name = frappe.generate_hash(length=10) | |
| query = query.insert( | |
| name, | |
| now_str, | |
| now_str, | |
| "Administrator", | |
| "Administrator", | |
| user, | |
| "roles", | |
| "User", | |
| "Press User", | |
| ) | |
| update_progress_bar("Updating users", i, total) | |
| if chunk: | |
| query.run() |
Prompt To Fix With AI
This is a comment left during a code review.
Path: press/patches/v0_8_0/merge_press_admin_member_roles.py
Line: 51-65
Comment:
If `total` is 0 the outer loop never executes, which is fine. But if PyPika's accumulated `query` object (with no `.insert()` calls) were ever reached, `.run()` would emit a bare `INSERT INTO has_role (...) VALUES` with no rows — a SQL syntax error. The inner loop guard prevents this today, but adding an explicit check makes the intent clear and protects against future refactors that might reorder the loops.
```suggestion
for i, user in enumerate(chunk, start=chunk_start):
name = frappe.generate_hash(length=10)
query = query.insert(
name,
now_str,
now_str,
"Administrator",
"Administrator",
user,
"roles",
"User",
"Press User",
)
update_progress_bar("Updating users", i, total)
if chunk:
query.run()
```
How can I resolve this? If you propose a fix, please make it concise.|
🎉 This PR is included in version 0.34.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This is an automatic backport of pull request #6422 done by [Mergify](https://mergify.com).