Skip to content

fix(org): replace the mappings of each project in an uploaded file - #9453

Open
henry-rystic wants to merge 2 commits into
apache:mainfrom
henry-rystic:fix-9449-org-project-mapping
Open

henry-rystic wants to merge 2 commits into
apache:mainfrom
henry-rystic:fix-9449-org-project-mapping

Conversation

@henry-rystic

Copy link
Copy Markdown

Summary

An upload of project_mapping.csv adds and updates rows, but it never removes rows. If a mapping is not in the new file, it stays in project_mapping. So a project keeps data scopes that the user removed.

Cause:

  • CreateProjectMapping in backend/plugins/org/api/project_mapping.go saves the rows without deleting first.
  • The other org uploads delete first. Blueprints also replace the mappings of a project: they delete by project_name, then save (backend/plugins/org/tasks/project_mapping.go).

This PR builds on #9451 (the fix for #9447), which adds replace to the org store. Please merge that PR first. Until then, this PR also shows its commit.

Fix:

  • The upload now finds the project names in the file. It deletes the mappings of these projects only, 1000 names in each statement. Then it saves the rows of the file.
  • The delete and the save run through replace, in one transaction. If the save fails, the old mappings stay.
  • Mappings of other projects do not change. This matters because blueprints write this table too.
  • The upload is rejected with 400 when a row has an empty ProjectName, Table or RowId. This usually means a wrong header, for example RowID. Without this check, such a file would delete the real mappings of a project.
  • The store's save method is removed, because nothing uses it now.

Does this close any open issues?

Closes #9449

Screenshots

N/A

Other Information

Tests added:

  • backend/plugins/org/api/project_mapping_test.go (unit tests, with a fake store):
    • TestCheckProjectMappings: empty and blank fields, and the row number in the error.
    • TestProjectMappingDeletions: each project name once, in file order, and 1000 names in each statement.
    • TestCreateProjectMapping: a valid file is passed to the store; a wrong header is rejected before the store is called; an error from the store is returned.
  • backend/plugins/org/e2e/project_mapping_upload_test.go (with a database, CSV files in e2e/raw_tables/ and VerifyTable against e2e/snapshot_tables/). Each test first adds a mapping for another project, as a blueprint does.
    • TestProjectMappingUploadReplacesProjectsInFile: upload three rows for Payments, then two. Payments must have two rows, and the other project must not change.
    • TestProjectMappingUploadFailureKeepsMappings: an upload with a RowId of 300 characters must return an error and keep the old mappings.
    • TestProjectMappingUploadAcceptsDuplicateRows: the same row twice in one file. PostgreSQL rejects an upsert that writes one key twice. BatchSave removes the duplicate, so this test makes sure that the upload keeps saving through it.

Without the fix, TestProjectMappingUploadReplacesProjectsInFile fails on MySQL and on PostgreSQL: Payments keeps three rows. With the fix, all tests pass on MySQL 8.4 and PostgreSQL 17. go vet passes.

Manual test. I built the backend image from this branch and ran it with MySQL 8.4. I ran the same steps on apache/devlake:main_261124_0230_52d63a1 to compare. Before step 1, I added one mapping for another project, Other, as a blueprint would.

Step Before the fix After the fix
1. Send PUT /plugins/org/project_mapping.csv with three rows for Payments (github:GithubRepo:1:7301, jira:JiraBoard:1:7302, github:GithubRepo:1:7303). 200, success 200, success
2. Send the file again with only the first two rows. 200, success 200, success
3. Send GET /plugins/org/project_mapping.csv. All three Payments rows are listed. Only the two rows from step 2 are listed for Payments.
Rows for Other in the table 1 1

This PR was prepared with AI assistance (Claude Code). The commits carry a Co-Authored-By: trailer.

🤖 Generated with Claude Code

henry-rystic and others added 2 commits November 25, 2026 12:43
A failed write in a teams, users or account mapping upload returned
success, because the error from Close was ignored. The handler had
already deleted all rows, so the table was left empty. The divider was
shared for the life of the store, so its saved error then blocked every
later upload of the same type.

Run the delete and the save in one transaction with TxHelper, use a new
divider for each upload, and return the error from Close.

Closes apache#9447

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Henry Starman <334243260+henry-rystic@users.noreply.github.com>
An upload of project_mapping.csv only added and updated rows. A mapping
that was not in the new file stayed in the table, so a project kept data
scopes that the user removed.

Delete the mappings of the projects named in the file, then save the
file, in the store's one transaction. Mappings of other projects do not
change, because blueprints also write this table. Reject a file with an
empty field, which usually means a wrong header, before it deletes
anything.

Closes apache#9449

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Henry Starman <334243260+henry-rystic@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Org] Uploading project_mapping.csv does not remove mappings that are not in the file

1 participant

⌂ Proxy