[ADD] dropdb

This commit is contained in:
Rémy Taymans 2024-02-15 22:22:14 +01:00
parent e5921fe4c7
commit 30198392c5
5 changed files with 97 additions and 0 deletions

View File

@ -41,6 +41,7 @@ and provides helpers to run (and replay) migrations until it works.
* [Command ``estimate-workload`` (BETA)](#command-estimate-workload) * [Command ``estimate-workload`` (BETA)](#command-estimate-workload)
* [Command ``psql``](#command-psql) * [Command ``psql``](#command-psql)
* [Command ``copydb``](#command-copydb) * [Command ``copydb``](#command-copydb)
* [Command ``dropdb``](#command-dropdb)
* [Command ``dumpdb``](#command-dumpdb) * [Command ``dumpdb``](#command-dumpdb)
<a name="installation"/> <a name="installation"/>
@ -455,6 +456,21 @@ Create an Odoo database by copying an existing one.
This script copies using postgres CREATEDB WITH TEMPLATE. It also copies This script copies using postgres CREATEDB WITH TEMPLATE. It also copies
the filestore. the filestore.
<a name="command-dropdb"/>
## 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.
<a name="command-dumpdb"/> <a name="command-dumpdb"/>
## Command: ``dumpdb`` ## Command: ``dumpdb``

View File

@ -11,6 +11,7 @@ from loguru import logger
import odoo_openupgrade_wizard import odoo_openupgrade_wizard
from odoo_openupgrade_wizard.cli.cli_copydb import copydb 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_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_dumpdb import dumpdb
from odoo_openupgrade_wizard.cli.cli_estimate_workload import estimate_workload from odoo_openupgrade_wizard.cli.cli_estimate_workload import estimate_workload
from odoo_openupgrade_wizard.cli.cli_execute_script_python import ( 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(copydb)
main.add_command(docker_build) main.add_command(docker_build)
main.add_command(dropdb)
main.add_command(dumpdb) main.add_command(dumpdb)
main.add_command(estimate_workload) main.add_command(estimate_workload)
main.add_command(execute_script_python) main.add_command(execute_script_python)

View File

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

View File

@ -27,3 +27,15 @@ def copydb(ctx, source, dest):
# Copy Filestore # Copy Filestore
logger.info(f"Copy filestore of '{source}' into '{dest}' folder ...") logger.info(f"Copy filestore of '{source}' into '{dest}' folder ...")
shutil.copytree(source_path, dest_path) 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)

View File

@ -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()