From cfb523adecb7630094a5bdfc352202407762d7d0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 2 May 2022 10:45:26 +1000 Subject: [PATCH] Adds option to delete existing data when importing records --- tasks.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tasks.py b/tasks.py index 0578f69acd..f079672199 100644 --- a/tasks.py +++ b/tasks.py @@ -6,6 +6,7 @@ import sys import pathlib import re + try: from invoke import ctask as task except: @@ -380,8 +381,8 @@ def export_records(c, filename='data.json'): print("Data export completed") -@task(help={'filename': 'Input filename'}, post=[rebuild_models, rebuild_thumbnails]) -def import_records(c, filename='data.json'): +@task(help={'filename': 'Input filename', 'clear': 'Clear existing data before import'}, post=[rebuild_models, rebuild_thumbnails]) +def import_records(c, filename='data.json', clear=False): """ Import database records from a file """ @@ -394,6 +395,9 @@ def import_records(c, filename='data.json'): print(f"Error: File '{filename}' does not exist") sys.exit(1) + if clear: + delete_data(c, force=True) + print(f"Importing database records from '{filename}'") # Pre-process the data, to remove any "permissions" specified for a user or group @@ -432,6 +436,8 @@ def delete_data(c, force=False): Warning: This will REALLY delete all records in the database!! """ + print(f"Deleting all data from InvenTree database...") + if force: manage(c, 'flush --noinput') else: