Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scan): fix workspaces can't overlap nor be renamed #2334

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
18 changes: 10 additions & 8 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,16 +954,17 @@ def sync_studies_on_disk(
all_studies = [raw_study for raw_study in all_studies if directory in Path(raw_study.path).parents]
else:
all_studies = [raw_study for raw_study in all_studies if directory == Path(raw_study.path).parent]
studies_by_path = {study.path: study for study in all_studies}
studies_by_path_workspace = {(study.workspace, study.path): study for study in all_studies}

# delete orphan studies on database
paths = [str(f.path) for f in folders]
# key should be workspace, path to sync correctly studies with same path in different workspace
workspace_paths = [(f.workspace, str(f.path)) for f in folders]

for study in all_studies:
if (
isinstance(study, RawStudy)
and not study.archived
and (study.workspace != DEFAULT_WORKSPACE_NAME and study.path not in paths)
and (study.workspace != DEFAULT_WORKSPACE_NAME and (study.workspace, study.path) not in workspace_paths)
):
if not study.missing:
logger.info(
Expand All @@ -990,11 +991,12 @@ def sync_studies_on_disk(
self.repository.delete(study.id)

# Add new studies
study_paths = [study.path for study in all_studies if study.missing is None]
study_paths = [(study.workspace, study.path) for study in all_studies if study.missing is None]
missing_studies = {study.path: study for study in all_studies if study.missing is not None}
for folder in folders:
study_path = str(folder.path)
if study_path not in study_paths:
workspace = folder.workspace
if (workspace, study_path) not in study_paths:
try:
if study_path not in missing_studies.keys():
base_path = self.config.storage.workspaces[folder.workspace].path
Expand All @@ -1004,7 +1006,7 @@ def sync_studies_on_disk(
name=folder.path.name,
path=study_path,
folder=str(dir_name),
workspace=folder.workspace,
workspace=workspace,
owner=None,
groups=folder.groups,
public_mode=PublicMode.FULL if len(folder.groups) == 0 else PublicMode.NONE,
Expand Down Expand Up @@ -1039,8 +1041,8 @@ def sync_studies_on_disk(
)
except Exception as e:
logger.error(f"Failed to add study {folder.path}", exc_info=e)
elif directory and study_path in studies_by_path:
existing_study = studies_by_path[study_path]
elif directory and (workspace, study_path) in studies_by_path_workspace:
existing_study = studies_by_path_workspace[(workspace, study_path)]
if self.storage_service.raw_study_service.update_name_and_version_from_raw_meta(existing_study):
self.repository.save(existing_study)

Expand Down
4 changes: 2 additions & 2 deletions tests/storage/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ def test_study_listing(db_session: Session) -> None:
@pytest.mark.unit_test
def test_sync_studies_from_disk() -> None:
now = datetime.utcnow()
ma = RawStudy(id="a", path="a")
fa = StudyFolder(path=Path("a"), workspace="", groups=[])
ma = RawStudy(id="a", path="a", workspace="workspace1")
fa = StudyFolder(path=Path("a"), workspace="workspace1", groups=[])
mb = RawStudy(id="b", path="b")
mc = RawStudy(
id="c",
Expand Down
Loading