Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(cli): fail `backup create --dry-run` when there is no store data (#2129)
Abdelhadi Salmaouifc64592260b3
diff --git a/semantica/cli.py b/semantica/cli.pyindex 72b73d50..dfb1e0bc 100644--- a/semantica/cli.py+++ b/semantica/cli.py@@ -5586,6 +5586,27 @@ def _warn_skipped(cli_ctx: CLIContext, skipped: List[Dict[str, str]]) -> None:)+def _raise_on_no_store_data(sources: List[Tuple[str, Path]], include: str) -> None:+ """Refuse a backup that would hold no store data at all (#1820).++ Called by the real command and by its dry run alike: a preview that+ reports success for an archive the real command refuses to write is the+ same surprise as a successful backup with nothing in it (#2122).+ """+ if include not in ("graph", "vector", "triplet", "all"):+ return+ if any(arcname.startswith(("graph/", "vector/", "triplet/"))+ for arcname, _ in sources):+ return+ stores = "graph, vector and triplet stores" if include == "all" else f"{include} store"+ raise click.ClickException(+ f"No store data to back up: found no local files for the {stores}. "+ "backup create only copies file-based stores (a graph_db / vector_store / "+ "triplet_store path that exists on disk); run `semantica backup info` "+ "to see how to back up other backends."+ )++def _raise_on_skipped(skipped: List[Dict[str, str]], *, target: str) -> None:"""Raise the --fail-on-skip error naming every skipped path."""if not skipped:@@ -5685,12 +5706,14 @@ def backup_create(def _action() -> None:if _is_dry(cli_ctx, local_dry):# A dry run is a preview of the real command, so it has to report- # the same skipped stores and honour --fail-on-skip (#2109).+ # the same skipped stores, honour --fail-on-skip and refuse an+ # archive the real run would not write (#2109, #2122).cfg = cli_ctx.config.to_dict()- _, skipped = _collect_backup_sources(+ sources, skipped = _collect_backup_sources(cfg, cli_ctx.config_path, include, strip_config,)_warn_skipped(cli_ctx, skipped)+ _raise_on_no_store_data(sources, include)if fail_on_skip:_raise_on_skipped(skipped, target="in the archive")_dry(@@ -5736,17 +5759,7 @@ def backup_create(# Fail before any prompt instead of writing an archive with no# store data in it (#1820).- if include in ("graph", "vector", "triplet", "all") and not any(- arcname.startswith(("graph/", "vector/", "triplet/"))- for arcname, _ in sources- ):- stores = "graph, vector and triplet stores" if include == "all" else f"{include} store"- raise click.ClickException(- f"No store data to back up: found no local files for the {stores}. "- "backup create only copies file-based stores (a graph_db / vector_store / "- "triplet_store path that exists on disk); run `semantica backup info` "- "to see how to back up other backends."- )+ _raise_on_no_store_data(sources, include)if fail_on_skip:_raise_on_skipped(skipped, target="in the archive")diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.pyindex bf120ba0..20cd09f7 100644--- a/tests/test_cli_commands.py+++ b/tests/test_cli_commands.py@@ -3377,8 +3377,17 @@ class TestBackup:def test_create_dry_run(self, runner):with runner.isolated_filesystem():- result = runner.invoke(cli_module.main, ["backup", "create", "backup.tar.gz", "--dry-run"])+ os.makedirs("kg")+ with open("kg/graph.db", "w") as f:+ f.write("data")+ with open("cfg.yaml", "w") as f:+ f.write("graph_db:\n backend: sqlite\n path: kg\n")+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "create", "backup.tar.gz", "--dry-run"],+ )_ok(result)+ assert "Dry run: would backup create" in _flatten(result.output)def test_create_unencrypted_with_config_requires_confirm(self, runner):with runner.isolated_filesystem():@@ -3631,6 +3640,67 @@ class TestBackup:assert "Configured store path does not exist" in flatassert not os.path.exists("out.tar.gz")+ @pytest.mark.parametrize("extra_args", [[], ["--fail-on-skip"]])+ def test_create_dry_run_fails_when_no_store_has_data(self, runner, extra_args):+ # #2122: --dry-run previewed a backup the real command refuses to+ # write, so it exited 0 on a config where nothing would be archived.+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: missing_vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "create", "out.tar.gz",+ "--strip-config", "--dry-run", *extra_args],+ )+ assert result.exit_code != 0+ flat = _flatten(result.output)+ assert "Skipping graph store" in flat+ assert "empty_graph" in flat+ assert "Skipping vector store" in flat+ assert "missing_vecs" in flat+ assert "No store data to back up" in flat+ assert "Dry run: would backup create" not in flat+ assert not os.path.exists("out.tar.gz")++ def test_create_dry_run_without_store_config_fails(self, runner):+ # Same #2122 case as above with no store configured at all: the real+ # command refuses it (#1820), so the preview must too.+ with runner.isolated_filesystem():+ with open("cfg.yaml", "w") as f:+ f.write("processing:\n batch_size: 8\n")+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "create", "out.tar.gz",+ "--strip-config", "--dry-run"],+ )+ assert result.exit_code != 0+ flat = _flatten(result.output)+ assert "No store data to back up" in flat+ assert "Skipping" not in flat++ def test_create_dry_run_json_fails_when_no_store_has_data(self, runner):+ # --json must not print a payload for a preview that cannot happen.+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ with open("cfg.yaml", "w") as f:+ f.write("graph_db:\n backend: local\n path: empty_graph\n")+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "--json", "backup", "create",+ "out.tar.gz", "--strip-config", "--dry-run"],+ )+ assert result.exit_code != 0+ assert result.stdout.strip() == ""+ assert "No store data to back up" in _flatten(result.stderr)+def test_create_dry_run_reports_a_skipped_store(self, runner):with runner.isolated_filesystem():os.makedirs("empty_graph")@@ -4056,8 +4126,14 @@ class TestBackup:def test_create_dry_run_json(self, runner):# backup create has no per-command --json; use global --json- result = runner.invoke(cli_module.main, ["--json", "backup", "create", "backup.tar.gz",- "--dry-run"])+ with runner.isolated_filesystem():+ os.makedirs("kg")+ with open("kg/graph.db", "w") as f:+ f.write("data")+ with open("cfg.yaml", "w") as f:+ f.write("graph_db:\n backend: sqlite\n path: kg\n")+ result = runner.invoke(cli_module.main, ["--config", "cfg.yaml", "--json",+ "backup", "create", "backup.tar.gz", "--dry-run"])_ok(result)data = _json_output(result)assert data["dry_run"] is True