diff --git a/newsfragments/+63.bugfix b/newsfragments/+63.bugfix new file mode 100644 index 0000000..20f54be --- /dev/null +++ b/newsfragments/+63.bugfix @@ -0,0 +1 @@ +Handles the case where the filestore data provided in the source is in a "filestore" subfolder diff --git a/newsfragments/+cli-restoredb-check-if-provided-paths-exist.misc b/newsfragments/+cli-restoredb-check-if-provided-paths-exist.misc new file mode 100644 index 0000000..0bef3cf --- /dev/null +++ b/newsfragments/+cli-restoredb-check-if-provided-paths-exist.misc @@ -0,0 +1 @@ +restoredb: Use builtins click feature to check if provided paths exist diff --git a/odoo_openupgrade_wizard/cli/cli_restoredb.py b/odoo_openupgrade_wizard/cli/cli_restoredb.py index 5d607a8..5d474ec 100644 --- a/odoo_openupgrade_wizard/cli/cli_restoredb.py +++ b/odoo_openupgrade_wizard/cli/cli_restoredb.py @@ -12,7 +12,7 @@ from odoo_openupgrade_wizard.tools.tools_system import restore_filestore @click.option( "--database-path", required=True, - type=click.Path(readable=True, resolve_path=True), + type=click.Path(readable=True, resolve_path=True, exists=True), help="Path to the database dump relative project folder.", ) @click.option( @@ -27,7 +27,7 @@ from odoo_openupgrade_wizard.tools.tools_system import restore_filestore @click.option( "--filestore-path", required=True, - type=click.Path(readable=True, resolve_path=True), + type=click.Path(readable=True, resolve_path=True, exists=True), help="Path to the filestore backup.", ) @click.option( @@ -59,6 +59,7 @@ def restoredb( "database-path should be inside the project path to allow " "postgresql to read to it." ) + # Restore the database output = execute_pg_restore( ctx, diff --git a/odoo_openupgrade_wizard/tools/tools_system.py b/odoo_openupgrade_wizard/tools/tools_system.py index 4f43de3..057736e 100644 --- a/odoo_openupgrade_wizard/tools/tools_system.py +++ b/odoo_openupgrade_wizard/tools/tools_system.py @@ -173,3 +173,12 @@ def restore_filestore( else: # works for "t" and "tgz" tar = tarfile.open(src_path) tar.extractall(path=filestore_path) + + # If a filestore/filestore/database/filestore directory exists, + # it means that the filestore was in a "filestore" subdirectory + # and we need to move this content to the parent directory. + filestore_subfolder = filestore_path / "filestore" + if filestore_subfolder.exists(): + for file in filestore_subfolder.iterdir(): + shutil.move(file, filestore_path) + shutil.rmtree(filestore_subfolder) diff --git a/tests/cli_23_restoredb_test.py b/tests/cli_23_restoredb_test.py index f452c5a..5d735cf 100644 --- a/tests/cli_23_restoredb_test.py +++ b/tests/cli_23_restoredb_test.py @@ -45,6 +45,38 @@ def test_cli_restoredb(mocker): # check filestore exists assert dest_filestore_path.exists() + # check the filestore content is at the right place + assert (dest_filestore_path / "01").exists() + + # Check database exists + assert_database(ctx, db_name, "present") + + # Delete filestore and database + shutil.rmtree(dest_filestore_path) + ensure_database(ctx, db_name, state="absent") + + shutil.copyfile( + pathlib.Path("../restoredb/test_with_nested_filestore_dir.tar.gz"), + filestore_path, + ) + + cli_runner_invoke( + [ + "restoredb", + f"--database={db_name}", + f"--database-path={database_path}", + "--database-format=c", + f"--filestore-path={filestore_path}", + "--filestore-format=tgz", + ], + ) + + # check filestore exists + assert dest_filestore_path.exists() + + # check the filestore content is at the right place + assert (dest_filestore_path / "01").exists() + # Check database exists assert_database(ctx, db_name, "present") diff --git a/tests/data/restoredb/test.tar.gz b/tests/data/restoredb/test.tar.gz index 7f5af3f..f755fe5 100644 Binary files a/tests/data/restoredb/test.tar.gz and b/tests/data/restoredb/test.tar.gz differ diff --git a/tests/data/restoredb/test_with_nested_filestore_dir.tar.gz b/tests/data/restoredb/test_with_nested_filestore_dir.tar.gz new file mode 100644 index 0000000..7f5af3f Binary files /dev/null and b/tests/data/restoredb/test_with_nested_filestore_dir.tar.gz differ