[FIX] dumpdb: error when using force

This commit is contained in:
Rémy Taymans 2023-07-10 19:26:59 +02:00
parent f130ecf078
commit 757cc19576

View File

@ -1,4 +1,5 @@
from pathlib import Path import pathlib
import shutil
import click import click
@ -52,14 +53,8 @@ def dumpdb(
force, force,
): ):
"""Create an dump of an Odoo database and its filestore.""" """Create an dump of an Odoo database and its filestore."""
database_path = Path(database_path) database_path = pathlib.Path(database_path)
filestore_path = Path(filestore_path) filestore_path = pathlib.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.")
# Check that database_path is inside the env_folder_path # Check that database_path is inside the env_folder_path
absolute_database_path = database_path.absolute() absolute_database_path = database_path.absolute()
@ -72,6 +67,24 @@ def dumpdb(
"postgresql to write to it." "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 # Normalise database_path
database_path = absolute_database_path.relative_to( database_path = absolute_database_path.relative_to(
absolute_env_folder_path absolute_env_folder_path