Adds option to delete existing data when importing records

This commit is contained in:
Oliver Walters 2022-05-02 10:45:26 +10:00
parent 422e22b8e6
commit cfb523adec

View File

@ -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: