Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ dm resources move --search payroll --to HR --check # Only list the
```

Resources move with their files and runs. `--project`, `--type`, `--search` and names combine; pass at least one.
A resource that clashes with the destination (same name, or a file in the way) stays where it is, the others move,
and the command exits with the `conflict` code, listing each one that stayed.
All of the resources move, or none do. A resource that clashes with the destination (same name, or a file in the way)
stops the move, and the command exits with the `conflict` code, listing each one. A resource already in the
destination stays there, and the others move.

### In-flight masking

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"typer>=0.16.0,<1",
"rich>=13.8",
"tomli-w>=1.0.0,<2",
"datamasque-python>=1.7.0.dev11,<2",
"datamasque-python>=1.7.0.dev12,<2",
"pydantic>=2.5,<3",
]
classifiers = [
Expand Down
21 changes: 10 additions & 11 deletions src/datamasque_cli/commands/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import typer
from datamasque.client import DataMasqueClient
from datamasque.client.exceptions import DataMasqueApiError
from datamasque.client.exceptions import DataMasqueApiError, ResourceMoveConflictError
from datamasque.client.models.project import Project
from datamasque.client.models.resource import Resource, ResourceMoveConflict
from datamasque.client.models.resource_type import ResourceTypeName
Expand All @@ -16,8 +16,7 @@

app = typer.Typer(help="Work with resources of every type.", no_args_is_help=True)

MOVE_CONFLICT_MESSAGE = "Some resources did not move"
MOVE_CHECK_CONFLICT_MESSAGE = "Some resources would not move"
MOVE_CHECK_CONFLICT_MESSAGE = "No resources would move"


def _describe_resource_count(count: int) -> str:
Expand Down Expand Up @@ -85,7 +84,8 @@ def move_resources(
Move resources to another project, with their files and runs.

Select them by project, type, a name search or exact names; the filters combine.
A resource that clashes with the destination stays where it is, and the others move.
All of them move, or none do: a resource that clashes with the destination stops the move.
A resource already in the destination stays there, and the others move.
Rulesets and configurations whose library imports resolve differently after the move become invalid.
"""
if not (names or sources or type_names or search):
Expand Down Expand Up @@ -139,14 +139,13 @@ def move_resources(

try:
result = client.move_resources(selected, target)
except ResourceMoveConflictError as exc:
abort(
f"No resources moved to '{target.name}'",
code=ErrorCode.CONFLICT,
details=_get_conflict_details(exc.conflicts),
)
except DataMasqueApiError as exc:
abort_api_error(f"Failed to move resources to '{target.name}'", exc)

if result.conflicts:
message = (
f"{MOVE_CONFLICT_MESSAGE}; {result.moved} moved to '{target.name}'"
if result.moved
else f"No resources moved to '{target.name}'"
)
abort(message, code=ErrorCode.CONFLICT, details=_get_conflict_details(result.conflicts))
print_success(f"Moved {_describe_resource_count(result.moved)} to '{target.name}'.")
23 changes: 8 additions & 15 deletions tests/commands/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import MagicMock, patch

import pytest
from datamasque.client.exceptions import ResourceMoveConflictError
from datamasque.client.models.resource import (
Resource,
ResourceMoveConflict,
Expand Down Expand Up @@ -43,7 +44,7 @@ def client() -> Iterator[MagicMock]:
ResourceTypeName.rulesets: [CUSTOMERS, ORDERS, ALREADY_THERE],
ResourceTypeName.seeds: [BUILT_IN_SEED],
}
client.move_resources.return_value = ResourceMoveResult(moved=2, conflicts=[])
client.move_resources.return_value = ResourceMoveResult(moved=2)
with patch(f"{MODULE}.get_client", return_value=client):
yield client

Expand Down Expand Up @@ -126,20 +127,12 @@ def test_move_with_nothing_outside_the_destination_is_not_found(client: MagicMoc
client.move_resources.assert_not_called()


def test_move_that_leaves_resources_behind_names_them_and_fails(client: MagicMock, runner: CliRunner) -> None:
client.move_resources.return_value = ResourceMoveResult(moved=1, conflicts=[CUSTOMERS_CLASH])

result = runner.invoke(app, ["resources", "move", "--project", "finance", "--to", "default", "-y"])

assert result.exit_code == ExitCode.CONFLICT
assert result.stderr == (
"Error: Some resources did not move; 1 moved to 'Default':\n Ruleset \"customers\" exists.\n"
def test_move_with_a_conflict_says_none_moved_and_names_it(client: MagicMock, runner: CliRunner) -> None:
response = MagicMock(status_code=409)
client.move_resources.side_effect = ResourceMoveConflictError(
"No resources moved.", response=response, conflicts=[CUSTOMERS_CLASH]
)


def test_move_that_moves_nothing_says_none_moved(client: MagicMock, runner: CliRunner) -> None:
client.move_resources.return_value = ResourceMoveResult(moved=0, conflicts=[CUSTOMERS_CLASH])

result = runner.invoke(app, ["resources", "move", "--project", "finance", "--to", "default", "-y"])

assert result.exit_code == ExitCode.CONFLICT
Expand All @@ -153,7 +146,7 @@ def test_move_lists_a_detail_shared_by_several_conflicts_once(client: MagicMock,
result = runner.invoke(app, ["resources", "move", "--project", "finance", "--to", "default", "--check"])

assert result.exit_code == ExitCode.CONFLICT
assert result.stderr == 'Error: Some resources would not move:\n Ruleset "customers" exists.\n'
assert result.stderr == 'Error: No resources would move:\n Ruleset "customers" exists.\n'


def test_move_declined_at_the_prompt_moves_nothing(client: MagicMock, runner: CliRunner) -> None:
Expand All @@ -170,7 +163,7 @@ def test_move_declined_at_the_prompt_moves_nothing(client: MagicMock, runner: Cl
(
[CUSTOMERS_CLASH],
ExitCode.CONFLICT,
'Error: Some resources would not move:\n Ruleset "customers" exists.\n',
'Error: No resources would move:\n Ruleset "customers" exists.\n',
),
],
ids=["no-conflicts", "conflict"],
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading