From 757cc195767736b12633894cf914c23a1265d503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Mon, 10 Jul 2023 19:26:59 +0200 Subject: [PATCH] [FIX] dumpdb: error when using force --- odoo_openupgrade_wizard/cli/cli_dumpdb.py | 31 ++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/odoo_openupgrade_wizard/cli/cli_dumpdb.py b/odoo_openupgrade_wizard/cli/cli_dumpdb.py index e092675..b6aaec1 100644 --- a/odoo_openupgrade_wizard/cli/cli_dumpdb.py +++ b/odoo_openupgrade_wizard/cli/cli_dumpdb.py @@ -1,4 +1,5 @@ -from pathlib import Path +import pathlib +import shutil import click @@ -52,14 +53,8 @@ def dumpdb( force, ): """Create an dump of an Odoo database and its filestore.""" - database_path = Path(database_path) - filestore_path = Path(filestore_path) - - # Fail if dumps already exists and force argument not given - if not force and database_path.exists(): - ctx.fail(f"{database_path} exists, use --force to overwrite it.") - if not force and filestore_path.exists(): - ctx.fail(f"{filestore_path} exists, use --force to overwrite it.") + database_path = pathlib.Path(database_path) + filestore_path = pathlib.Path(filestore_path) # Check that database_path is inside the env_folder_path absolute_database_path = database_path.absolute() @@ -72,6 +67,24 @@ def dumpdb( "postgresql to write to it." ) + # Fail if dumps already exists and force argument not given + # Remove file if already exists and force is given + if not force and database_path.exists(): + ctx.fail(f"{database_path} exists, use --force to overwrite it.") + elif force and database_path.exists(): + if database_path.is_dir(): + shutil.rmtree(database_path) + else: + database_path.unlink() + + if not force and filestore_path.exists(): + ctx.fail(f"{filestore_path} exists, use --force to overwrite it.") + elif force and filestore_path.exists(): + if filestore_path.is_dir(): + shutil.rmtree(filestore_path) + else: + filestore_path.unlink() + # Normalise database_path database_path = absolute_database_path.relative_to( absolute_env_folder_path