From 30198392c5ed40769b7c422ea15abb339cc24d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Thu, 15 Feb 2024 22:22:14 +0100 Subject: [PATCH] [ADD] dropdb --- README.md | 16 ++++++ odoo_openupgrade_wizard/cli/cli.py | 2 + odoo_openupgrade_wizard/cli/cli_dropdb.py | 14 +++++ .../tools/tools_click_odoo_contrib.py | 12 +++++ tests/cli_32_dropdb_test.py | 53 +++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 odoo_openupgrade_wizard/cli/cli_dropdb.py create mode 100644 tests/cli_32_dropdb_test.py diff --git a/README.md b/README.md index 8a9a1b3..54fc29c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ and provides helpers to run (and replay) migrations until it works. * [Command ``estimate-workload`` (BETA)](#command-estimate-workload) * [Command ``psql``](#command-psql) * [Command ``copydb``](#command-copydb) + * [Command ``dropdb``](#command-dropdb) * [Command ``dumpdb``](#command-dumpdb) @@ -455,6 +456,21 @@ Create an Odoo database by copying an existing one. This script copies using postgres CREATEDB WITH TEMPLATE. It also copies the filestore. + + +## Command: ``dropdb`` + +**Prerequites:** init + +``` +odoo-openupgrade-wizard dropdb + --database DB_NAME +``` + +Delete an Odoo database and its filestore. + +This command will always success even if DB_NAME does not exists. + ## Command: ``dumpdb`` diff --git a/odoo_openupgrade_wizard/cli/cli.py b/odoo_openupgrade_wizard/cli/cli.py index c2b5d4e..c6f1508 100644 --- a/odoo_openupgrade_wizard/cli/cli.py +++ b/odoo_openupgrade_wizard/cli/cli.py @@ -11,6 +11,7 @@ from loguru import logger import odoo_openupgrade_wizard from odoo_openupgrade_wizard.cli.cli_copydb import copydb from odoo_openupgrade_wizard.cli.cli_docker_build import docker_build +from odoo_openupgrade_wizard.cli.cli_dropdb import dropdb from odoo_openupgrade_wizard.cli.cli_dumpdb import dumpdb from odoo_openupgrade_wizard.cli.cli_estimate_workload import estimate_workload from odoo_openupgrade_wizard.cli.cli_execute_script_python import ( @@ -114,6 +115,7 @@ def main(ctx, env_folder, filestore_folder, log_level): main.add_command(copydb) main.add_command(docker_build) +main.add_command(dropdb) main.add_command(dumpdb) main.add_command(estimate_workload) main.add_command(execute_script_python) diff --git a/odoo_openupgrade_wizard/cli/cli_dropdb.py b/odoo_openupgrade_wizard/cli/cli_dropdb.py new file mode 100644 index 0000000..95ac768 --- /dev/null +++ b/odoo_openupgrade_wizard/cli/cli_dropdb.py @@ -0,0 +1,14 @@ +import click + +from odoo_openupgrade_wizard.cli.cli_options import database_option_required +from odoo_openupgrade_wizard.tools import ( + tools_click_odoo_contrib as click_odoo_contrib, +) + + +@click.command() +@database_option_required +@click.pass_context +def dropdb(ctx, database): + """Delete a database and its filestore.""" + click_odoo_contrib.dropdb(ctx, database) diff --git a/odoo_openupgrade_wizard/tools/tools_click_odoo_contrib.py b/odoo_openupgrade_wizard/tools/tools_click_odoo_contrib.py index 03b91d4..5343634 100644 --- a/odoo_openupgrade_wizard/tools/tools_click_odoo_contrib.py +++ b/odoo_openupgrade_wizard/tools/tools_click_odoo_contrib.py @@ -27,3 +27,15 @@ def copydb(ctx, source, dest): # Copy Filestore logger.info(f"Copy filestore of '{source}' into '{dest}' folder ...") shutil.copytree(source_path, dest_path) + + +def dropdb(ctx, database): + """Drop a database and its filestore""" + # Drop database + logger.info(f"Drop database '{database}' if it exists...") + ensure_database(ctx, database, state="absent") + # Drop filestore + root_filestore_path = ctx.obj["filestore_folder_path"] / "filestore" + filestore_path = root_filestore_path / database + logger.info(f"Remove filestore of '{database}' if exists...") + shutil.rmtree(filestore_path, ignore_errors=True) diff --git a/tests/cli_32_dropdb_test.py b/tests/cli_32_dropdb_test.py new file mode 100644 index 0000000..631cba4 --- /dev/null +++ b/tests/cli_32_dropdb_test.py @@ -0,0 +1,53 @@ +import pathlib +import shutil + +from odoo_openupgrade_wizard.tools.tools_postgres import ( + ensure_database, + execute_sql_request, +) + +from . import ( + build_ctx_from_config_file, + cli_runner_invoke, + mock_odoo_rpc_url, + move_to_test_folder, +) + + +def test_cli_dropdb(mocker): + move_to_test_folder() + mock_odoo_rpc_url(mocker) + + db_name = "database_test_cli___dropdb" + ctx = build_ctx_from_config_file() + + # Ensure environment is clean + ensure_database(ctx, db_name, state="absent") + filestore_path = pathlib.Path(f"./filestore/filestore/{db_name}") + shutil.rmtree(filestore_path, ignore_errors=True) + + # Initialize database + cli_runner_invoke( + [ + "--log-level=DEBUG", + "install-from-csv", + f"--database={db_name}", + ], + ) + + # Drop database + cli_runner_invoke( + [ + "--log-level=DEBUG", + "dropdb", + f"--database={db_name}", + ], + ) + + # Check database does not exists + request = "select datname FROM pg_database WHERE datistemplate = false;" + results = execute_sql_request(ctx, request) + assert [db_name] not in results + + # Check filestore does not exists + assert not filestore_path.exists()