Skip to content

fix(org): make a failed CSV upload change nothing - #9451

Open
henry-rystic wants to merge 1 commit into
apache:mainfrom
henry-rystic:fix-9447-org-csv-upload
Open

henry-rystic wants to merge 1 commit into
apache:mainfrom
henry-rystic:fix-9447-org-csv-upload

Conversation

@henry-rystic

Copy link
Copy Markdown

Summary

A failed CSV upload in the org plugin returns success and deletes all rows. After that, every upload of the same type fails until DevLake restarts. This affects teams.csv, users.csv and user_account_mapping.csv.

Cause:

  • Each handler deletes all rows first. Then it calls dbStore.save.
  • save writes the rows when it calls Close(), but it ignores the error from Close(). So a failed write returns success, and the table is empty.
  • dbStore keeps one BatchSaveDivider for its whole life. The divider keeps one BatchSave per type, and BatchSave keeps its last error. After one failed write, Add rejects every row with add slot failed due to previous err.

Fix:

  • The store has a new method, replace. It runs the delete and the save in one transaction, with dbhelper.TxHelper. If any step fails, the transaction is rolled back, and nothing changes.
  • replace creates a new BatchSaveDivider for each upload. So an error from one upload does not affect later uploads.
  • replace returns the error from Close().
  • The divider writes through the transaction. A small wrapper gives it the transaction as its Dal.
  • save now calls replace with no delete. The project_mapping.csv upload still uses save, so its write errors are now returned too.

Does this close any open issues?

Closes #9447

Screenshots

N/A

Other Information

Tests added in backend/plugins/org/e2e/csv_upload_test.go: TestCsvUploadFailureChangesNothing. It has one case for each upload: teams, users and user account mapping. It uses CSV files in e2e/raw_tables/ and checks the tables with VerifyTable against files in e2e/snapshot_tables/. Each case does these steps:

  1. Upload a valid file. Check the tables.
  2. Upload the same rows and one row with a value of 300 characters. The column is varchar(255), so MySQL in strict mode (the default) and PostgreSQL reject it. The upload must return an error, and the tables must not change.
  3. Upload the valid file again. It must work, and the tables must not change.

Without the fix, all three cases fail for the reasons in the issue, on MySQL and on PostgreSQL: step 2 returns no error, the tables are empty, and step 3 fails with add slot failed due to previous err. With the fix, they pass. go test ./plugins/org/... passes on MySQL 8.4 and PostgreSQL 17, and go vet passes.

The test calls the upload handlers, because the store methods are not exported. A test with a fake store would not test the bug, because the bug is in how the store uses the database.

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.

Step Before the fix After the fix
1. Send PUT /plugins/org/teams.csv with three teams. 200, success. Three teams are listed. 200, success. Three teams are listed.
2. Send the three teams and a fourth team whose Name has 300 characters. 200, success. GET /plugins/org/teams.csv returns only the header row. The teams table has 0 rows. 500, Data too long for column 'name' at row 4. The three teams are still listed.
3. Send the file from step 1 again. 500, add slot failed due to previous err. The table is still empty. 200, success. Three teams are listed.
Send PUT /plugins/org/users.csv with one user. 200 200

Note. When two uploads of the same type run at the same time on an empty table, MySQL can report a deadlock for one of them. That upload returns an error and changes nothing, so it can be sent again. Before this change, two uploads at the same time shared one divider, which was not safe.

The PR for #9449 builds on this PR. It uses replace to delete only the mappings of the projects in the file.

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

🤖 Generated with Claude Code

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>
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] A failed teams or users CSV upload returns success, deletes all rows, and blocks later uploads

1 participant

⌂ Proxy