All jobs

Add Soft Delete to an Actix Web API

Yes. An agent adds a deleted_at column with a Diesel migration, turns DELETE into an update, filters every query, fixes the slug index and proves it on Postgres.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent clones the API into its own cloud sandbox and reads the schema and every query before it changes a delete. In an Actix Web app like snamiki1212/realworld-v1-rust-actix-web-diesel, Cargo.toml pins actix-web 4.3 and diesel 2.1.0 with the r2d2, postgres, chrono and uuid features, diesel.toml writes the schema to src/schema.rs, and migrations/2021-10-24-230744_create_articles creates articles with a UUID id, author_id, a UNIQUE slug, title, description, body, created_at and updated_at. src/app/features/article/entities.rs defines the Article struct and its queries; Article::delete runs diesel::delete filtered by slug and author and relies on DELETE CASCADE to remove tags, and src/app/drivers/routes.rs maps DELETE /api/articles/{article_title_slug} to controllers::delete. compose.yaml runs the app with cargo watch against postgres:14.0-alpine, and .github/workflows/ci.yml runs cargo clippy -- -D warnings, cargo test and a Postman collection through newman against a live server.

The migration is the easy part: a new migrations/<timestamp>_add_deleted_at_to_articles whose up.sql adds deleted_at TIMESTAMP NULL and an index on it, and whose down.sql removes both. The agent regenerates src/schema.rs with diesel print_schema so articles gains deleted_at -> Nullable<Timestamp>, adds pub deleted_at: Option<NaiveDateTime> to Article, and rewrites Article::delete as diesel::update(...).set(articles::deleted_at.eq(diesel::dsl::now)) on the same slug and author filter, so the authorisation rule the old delete enforced is kept. Because tags are no longer removed by cascade, the response is unchanged and the rows stay, which is what a soft delete is for.

The reads are where soft delete goes wrong, so the agent lists them from the code rather than from memory: the two boxed articles::table.inner_join(users::table) queries in repositories.rs that count and page the list, fetch_following_articles, Article::fetch_by_slug_with_author, fetch_by_slug_and_author_id, fetch_ids_by_author_name and find_with_author in entities.rs, and the lookup in comment/entities.rs that resolves an article by slug and author before inserting a comment. Each gets .filter(articles::deleted_at.is_null()), added through one Article::live() helper so the condition is written once. The ids returned by Tag::fetch_article_ids_by_name and Favorite::fetch_favorited_article_ids_by_username may include deleted articles, which is fine because the final articles query filters them. One constraint changes: slug is UNIQUE, so a deleted article would block re-creating one with the same title. The migration drops that constraint and adds a partial unique index on slug WHERE deleted_at IS NULL, which down.sql reverses.

Proof runs against a real database. The agent starts Postgres with docker compose up -d db, copies .env.example to .env as scripts/copy-env.sh does, runs diesel migration run and then diesel migration redo to prove down.sql works, and runs cargo clippy -- -D warnings and cargo test. The repository's unit tests live only in auth.rs and converter.rs, so the behavioural proof is a small e2e/soft-delete.sh in the style of e2e/run-api-tests.sh: it registers a user, creates an article, deletes it, shows GET by slug returns 404 and the list no longer includes it, checks with psql that the row still exists with deleted_at set, and creates a second article with the same title to show the partial index allows it. The existing Postman collection still passes because the public behaviour of DELETE is unchanged.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add soft delete for articles to this Actix Web and Diesel API. Read Cargo.toml, diesel.toml, every migration, src/schema.rs, src/app/drivers/routes.rs, every file under src/app/features/article, comment, favorite and tag, compose.yaml, .env.example, scripts/ and e2e/ first. Add a migration that adds a nullable deleted_at timestamp with an index to articles, replaces the unique constraint on slug with a partial unique index where deleted_at is null, and reverses both in down.sql. Regenerate src/schema.rs with diesel print_schema, add deleted_at to the Article struct, and change Article::delete to set deleted_at with the same slug and author filter instead of deleting. Add a single live() filter helper and apply it to every query that reads articles, including the list count and page queries, the feed, slug lookups, author id lookups and the comment module's article lookup. Keep the HTTP responses the same. Start Postgres with docker compose up -d db, run diesel migration run and diesel migration redo, cargo clippy -- -D warnings and cargo test, and write e2e/soft-delete.sh that creates, deletes, checks 404 and list exclusion, confirms the row remains with deleted_at via psql, and re-creates an article with the same title. Open a pull request listing the migration, every query you filtered, the index change and the command output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request on its own branch that adds the migration directory with up.sql and down.sql, the regenerated src/schema.rs, the deleted_at field, live() helper and rewritten delete in article/entities.rs, the filters in article/repositories.rs and comment/entities.rs, and e2e/soft-delete.sh. routes.rs, the controllers, the presenters and the request types are unchanged, because the API's shape does not move.

The description lists every query that now carries the deleted_at IS NULL filter with its file and function, explains the swap from a unique constraint to a partial unique index, and quotes the diesel migration redo, cargo clippy, cargo test and e2e/soft-delete.sh output. The transcript at GET /o/{org}/sessions/{id}/messages keeps each command and its output, including the psql row showing deleted_at set, so a reviewer can check the proof instead of the summary.

Before merging, a reviewer greps for articles::table once more to confirm no read path was missed, and decides whether comments and favourites on a deleted article should disappear from counts. A follow-up on the same session can add an authenticated restore route, a purge job that hard-deletes rows older than a retention period, or the same treatment for comments.

Run it on every model that needs soft delete

A team with several Diesel-backed services runs this once per repository, or once per model. From a script, send the body above to POST /o/{org}/sessions with each repository in resources, one run per repository, and a request_id that is a UUID v5 of the repository and model name so a retry is safe. Give the key a webhook_url and every run the key starts posts run.completed with data.session_url when its turn ends; a handler reads the pull request link from the session.

To stop a new query from leaking deleted rows later, add an automation to the Project with a github trigger on pull_request.opened and pull_request.synchronize and a prompt that reads the pull request's diff for a new articles::table query without the live() helper and comments on the line. The trigger clones that branch and places the event after the prompt, and the automation starts at most max_runs_per_hour runs in an hour.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Soft delete is one column and a dozen filters, and the dozen is the risk: miss one read path and deleted data is back on a public endpoint. The agent enumerates the queries from the code, writes the filter once, and proves the result against a real Postgres in its own sandbox, with Rust, Diesel's CLI and Docker already there, including the down.sql that most pull requests never run.

Every diesel, cargo and psql command and its output stay in the run's transcript, so the pull request's claim that the row survives and the slug can be reused is checkable, and the laptop that handed off the task is free while the crate compiles. Several services can get the same change at once, each in its own sandbox and its own pull request.

Frequently asked questions

Why change the unique constraint on slug?

Because a soft-deleted article keeps its slug, so the existing UNIQUE constraint would reject a new article with the same title forever. A partial unique index on slug WHERE deleted_at IS NULL keeps live slugs unique and lets a deleted one be reused, and down.sql restores the original constraint.

What happens to comments, favourites and tags of a deleted article?

They stay in their tables, since nothing is deleted any more, and they are hidden because every path to them resolves the article first with the deleted_at IS NULL filter. Whether they should count towards totals is a product decision the pull request flags rather than makes.

Does the API's response change?

No. DELETE /api/articles/{slug} returns what it returned before, a subsequent GET is 404, and the list and feed omit the article. The Postman collection in e2e/ keeps passing, and the new e2e/soft-delete.sh checks the parts that differ underneath.

Can a deleted article be restored?

The data is there, so yes, but the pull request adds no route for it because the RealWorld specification has none. A follow-up on the same session can add an authenticated restore endpoint that clears deleted_at for the article's author, with its own filter helper for queries that should see deleted rows.

Will clippy still pass with -D warnings?

That is part of the check: CI runs cargo clippy -- -D warnings, so the agent runs it after the change and fixes anything the new field or helper introduces before opening the pull request. The regenerated schema.rs comes from diesel print_schema, not from hand edits, so it matches what the migration created.

More Actix Web jobs

The same job on other stacks

Keep reading