How to Update Doctypes After Data Entry using Frappe patches

Frappe Patches and data migrations

Automate Data Updates with Frappe Patches

Frappe patches
automate data updates
Frappe Framework
Frappe
patch scripts
Frappe development
Frappe patching system
doctype updates
Frappe migration

Patches are custom scripts that allow us to modify data in Frappe whenever we run the migrate command. The key feature of patches is that they only run once, making them perfect for updating data after making changes to your doctypes or system configurations.

Let’s explore this concept with a common scenario:

Scenario: Editing a Doctype After Data Entry

Imagine you have a doctype with over 100 entries, and you've just made some changes to the doctype structure. Now, you need to update the content of all the existing records. Doing this manually would be time-consuming and prone to errors. Luckily, we can use Frappe patches to handle this task efficiently. frappe-virtual-field

Steps to Implement a Frappe Patch

1. Identify the Doctype to Update

First, decide which doctype entries need to be updated. For this example, let's assume we're working with a Vehicle doctype and we want to update each entry.

2. Create the Patches Folder and Files

Within the specific doctype, create a folder named patches.

  • Inside this folder, create a file named __init__.py (leave this empty).
  • Next, create the file that will contain your patch logic. You can name this file according to the specific patch you’re applying. In our case, let's name it set_title.py. frappe-patches-directory-and-files

3. Write the Patch Function

In the set_title.py file, define a function called execute(). This function will contain the logic to update all relevant entries.

Here’s a basic template for the patch function:

import frappe

def execute():
    pass

4. Add the Patch to the patches.txt File

To ensure Frappe runs your patch, you need to register it in the patches.txt file. There are two sections in this file:

  • Pre-model sync: Patches that run before doctypes are migrated.
  • Post-model sync: Patches that run after doctypes are migrated.

In our case, we'll add the patch to the post_model_sync section since we want it to run after the migration:

[post_model_sync]
# Patches added in this section will be executed after doctypes are migrated
rental.rental.doctype.vehicle.patches.set_title

5. Run the Patch

Once the patch is ready, simply run the following command:

bench migrate

Frappe will automatically run your patch and update all the entries in the selected doctype.

Sample Patch Code

Here’s a complete example of a patch that updates the title of each vehicle in the Vehicle doctype:

import frappe

def execute():
    # Get all vehicle entries
    vehicles = frappe.db.get_all('Vehicle', pluck='name')

    # Loop through each vehicle and update the title
    for v in vehicles:
        vehicle = frappe.get_doc('Vehicle', v)
        vehicle.set_title()
        vehicle.save()

    # Commit the changes to the database
    frappe.db.commit()

This patch retrieves all Vehicle records, updates their title by calling the set_title() method, and then saves the changes. Finally, the changes are committed to the database.