[IMP] cli restoredb: manage filestore in subfolder

This commit is contained in:
Simon Maillard 2025-01-31 13:31:24 +00:00
parent a00ee58b62
commit 7d1183b140
6 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1 @@
Handles the case where the filestore data provided in the source is in a "filestore" subfolder

View File

@ -59,6 +59,15 @@ def restoredb(
"database-path should be inside the project path to allow " "database-path should be inside the project path to allow "
"postgresql to read to it." "postgresql to read to it."
) )
# Check db path exists
if not database_path.exists():
ctx.fail(f"Database path {database_path} does not exist.")
# Check filestore path exists
if not filestore_path.exists():
ctx.fail(f"Filestore path {filestore_path} does not exist.")
# Restore the database # Restore the database
output = execute_pg_restore( output = execute_pg_restore(
ctx, ctx,

View File

@ -173,3 +173,12 @@ def restore_filestore(
else: # works for "t" and "tgz" else: # works for "t" and "tgz"
tar = tarfile.open(src_path) tar = tarfile.open(src_path)
tar.extractall(path=filestore_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)

View File

@ -45,6 +45,38 @@ def test_cli_restoredb(mocker):
# check filestore exists # check filestore exists
assert dest_filestore_path.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 # Check database exists
assert_database(ctx, db_name, "present") assert_database(ctx, db_name, "present")

Binary file not shown.