From 7dcf45652599a22772ae0904f794985da389b1fe Mon Sep 17 00:00:00 2001 From: Gabriel Pickenhayn Date: Tue, 7 May 2024 14:48:44 +0000 Subject: [PATCH] [ADD] restore-sql --- ...sql-option-for-database-format-cli.feature | 1 + odoo_openupgrade_wizard/cli/cli_restoredb.py | 6 ++--- .../tools/tools_postgres.py | 26 ++++++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 newsfragments/add-sql-option-for-database-format-cli.feature diff --git a/newsfragments/add-sql-option-for-database-format-cli.feature b/newsfragments/add-sql-option-for-database-format-cli.feature new file mode 100644 index 0000000..e101a5e --- /dev/null +++ b/newsfragments/add-sql-option-for-database-format-cli.feature @@ -0,0 +1 @@ +Add option ``p`` (SQL format) allowing use from ``--database-format`` CLI. This allows you to restore database in SQL format (used by odoo full backup) diff --git a/odoo_openupgrade_wizard/cli/cli_restoredb.py b/odoo_openupgrade_wizard/cli/cli_restoredb.py index 2f479a1..5d607a8 100644 --- a/odoo_openupgrade_wizard/cli/cli_restoredb.py +++ b/odoo_openupgrade_wizard/cli/cli_restoredb.py @@ -18,11 +18,11 @@ from odoo_openupgrade_wizard.tools.tools_system import restore_filestore @click.option( "--database-format", required=True, - type=click.Choice(("c", "d", "t")), + type=click.Choice(("c", "d", "t", "p")), default="c", help="Database format (see pg_dump options): " - "custom format compressed (c), directory (d), tar file (t)." - "plain sql text (p) is not implemented", + "custom format compressed (c), directory (d), tar file (t)," + " plain sql text (p).", ) @click.option( "--filestore-path", diff --git a/odoo_openupgrade_wizard/tools/tools_postgres.py b/odoo_openupgrade_wizard/tools/tools_postgres.py index 84f8822..6189308 100644 --- a/odoo_openupgrade_wizard/tools/tools_postgres.py +++ b/odoo_openupgrade_wizard/tools/tools_postgres.py @@ -250,15 +250,23 @@ def execute_pg_restore( container = get_postgres_container(ctx) ensure_database(ctx, database, "absent") ensure_database(ctx, database, "present") - command = ( - "pg_restore" - f" {Path('/env') / filepath}" - f" --dbname={database}" - " --schema=public" - " --username=odoo" - " --no-owner" - f" --format {database_format}" - ) + if database_format == "p": + command = ( + "psql" + f" --file='{Path('/env') / filepath}'" + " --username odoo" + f" --dbname={database}" + ) + else: + command = ( + "pg_restore" + f" {Path('/env') / filepath}" + f" --dbname={database}" + " --schema=public" + " --username=odoo" + " --no-owner" + f" --format {database_format}" + ) logger.info(f"Restoring database '{database}'...") pg_dump_result = exec_container(container, command) return pg_dump_result.output.decode("utf8")