odoo-openupgrade-wizard/odoo_openupgrade_wizard/cli/cli_restoredb.py
2025-10-03 20:52:24 +02:00

85 lines
2.4 KiB
Python

from pathlib import Path
import click
from odoo_openupgrade_wizard.cli.cli_options import database_option_required
from odoo_openupgrade_wizard.tools.tools_postgres import execute_pg_restore
from odoo_openupgrade_wizard.tools.tools_system import restore_filestore
@click.command()
@database_option_required
@click.option(
"--database-path",
required=True,
type=click.Path(readable=True, resolve_path=True, exists=True),
help="Path to the database dump (inside the environment folder).",
)
@click.option(
"--database-format",
required=True,
type=click.Choice(("c", "d", "t", "p")),
default="c",
help="Format of the database dump: custom (c), directory (d), tar (t), "
"or plain SQL (p).",
)
@click.option(
"--filestore-path",
required=True,
type=click.Path(readable=True, resolve_path=True, exists=True),
help="Path to the filestore backup (inside the environment folder).",
)
@click.option(
"--filestore-format",
required=True,
type=click.Choice(("d", "t", "tgz")),
default="tgz",
help="Format of the filestore: directory (d), tar (t), or gzip-compressed "
"tar (tgz).",
)
@click.pass_context
def restoredb(
ctx,
database,
database_path,
database_format,
filestore_path,
filestore_format,
):
"""Restore a database and its associated filestore.
This command restores a PostgreSQL database and its matching Odoo
filestore into the current OOW environment. The filestore and
database dump must be accessible from within the environment directory
so that the Docker container can read them during restore.
"""
database_path = Path(database_path)
filestore_path = Path(filestore_path)
# Check that database_path is inside the env_folder_path
absolute_env_folder_path = ctx.obj["env_folder_path"].resolve().absolute()
if not str(database_path).startswith(str(absolute_env_folder_path)):
ctx.fail(
"database-path should be inside the project path to allow "
"PostgreSQL to read it."
)
# Restore the database
output = execute_pg_restore(
ctx,
database_path.relative_to(absolute_env_folder_path),
database,
database_format,
)
if output:
click.echo(output)
# Restore the filestore
restore_filestore(
ctx,
database,
filestore_path,
filestore_format,
)