All jobs

Add Soft Delete to GORM Models

Yes. An agent adds a gorm.DeletedAt column, fixes the queries and unique indexes soft delete breaks, keeps hard deletes where data must go, and runs the suite.

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 reads the model, its store and every caller before adding a column. In a server laid out like gotify/server, model/application.go declares Application with an auto-increment ID, a Token under the unique index uix_applications_token, a UserID and a SortKey that share the composite unique index uix_application_user_id_sort_key, plus Name, Description, Internal, Image, DefaultPriority, CreatedAt and LastUsed. database/application.go holds the store: GetApplicationByToken and GetApplicationByID use Where(...).Find(app) and treat gorm.ErrRecordNotFound as nil, CreateApplication runs a serializable transaction that reads the user's highest sort_key and asks fracdex.KeyBetween for the next one, DeleteApplicationByID calls DeleteMessagesByApplication(id) and then Delete(&model.Application{}), and UpdateApplication goes through updateExisting, which is d.DB.Select("*").Updates(value). database/database.go opens GORM with DisableForeignKeyConstraintWhenMigrating: true, TranslateError: true and a custom NowFunc, and AutoMigrates the five models on start.

The column is DeletedAt gorm.DeletedAt with gorm:"index" and json:"-" on Application, which makes Delete write a timestamp instead of removing the row and makes every Find, First and Where on the model exclude deleted rows. AutoMigrate adds the column on the next start, on SQLite, MySQL and PostgreSQL alike, because the config already opens all three drivers. DeleteApplicationByID stops deleting the application's messages: a soft-deleted application keeps them so that restoring it brings them back. They must still leave the user's stream, and that does not happen by itself: GetMessagesByUser and GetMessagesByUserSince in database/message.go join applications with a raw JOIN applications ON applications.user_id = ? clause, which GORM's soft-delete scope does not touch, so the agent adds applications.deleted_at IS NULL to both joins. DeleteMessagesByUser needs nothing, since it loops over the scoped GetApplicationsByUser. The store gains RestoreApplicationByID, which uses Unscoped() to find the row and clears deleted_at, and PurgeApplicationByID, which deletes the messages and then the row for good.

Two places need Unscoped() on purpose. CreateApplication reads the highest sort_key to place the new application last, and a soft-deleted row still holds its key under the composite unique index, so a new key computed from live rows alone can collide with a deleted one; the agent reads the maximum across all rows. DeleteUserByID in database/user.go loops over the user's applications and calls DeleteApplicationByID, and a deleted account must leave no data behind, so that loop calls the purge instead. GetApplicationByToken needs no change: a deleted application's token resolves to nothing, which is what auth/authentication.go should see. The HTTP side is left as it is, because api/application.go reaches the store through an interface and make check-ci runs check-swagger, so a restore route is its own change with regenerated API docs, and the pull request says so.

The proof is the existing database suite. database/application_test.go already has TestDeleteAppDeletesMessages and TestUpdateDeletedApplicationDoesNotRecreateIt, the first of which the agent rewrites to assert the messages survive a soft delete and go with a purge, and TestUpdateDeletedApplicationDoesNotRecreateIt must still pass, since Updates on a scoped query touches zero rows. New cases cover a restore, a create after a soft delete that lands on a fresh sort key, a token lookup on a deleted application, a soft-deleted application's messages missing from GetMessagesByUser and GetMessagesByUserSince and back after a restore, and DeleteUserByID leaving no application or message rows under Unscoped(). make test runs go test --race -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... ./..., so the API tests under api/ run too and show the handlers unchanged.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add soft delete to the Application model in this GORM service. Read model/application.go, database/application.go, database/database.go, database/user.go, database/message.go, api/application.go, auth/authentication.go, database/application_test.go, test/testdb/database.go and the Makefile first. Add DeletedAt gorm.DeletedAt with an index to Application and keep it out of JSON. Make DeleteApplicationByID a soft delete that leaves the application's messages in place, and add applications.deleted_at IS NULL to the raw joins in GetMessagesByUser and GetMessagesByUserSince, which the soft-delete scope does not filter. Add RestoreApplicationByID, which finds the row with Unscoped and clears deleted_at, and PurgeApplicationByID, which deletes the messages and then the row. Read the highest sort_key in CreateApplication with Unscoped so a new key cannot collide with a soft-deleted row under the composite unique index. Make DeleteUserByID purge the user's applications. Do not add HTTP routes. Update TestDeleteAppDeletesMessages for the new behaviour and add tests for restore, create after soft delete, token lookup on a deleted application, a deleted application's messages leaving the user's stream and returning on restore, and user deletion leaving no rows. Run make test and go vet ./..., and open a pull request with the 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

The pull request changes model/application.go, database/application.go, database/user.go and database/application_test.go, and no file under api/ or ui/. Its description pastes the make test run with the race detector on, the new test names, and the go vet output, and explains the two Unscoped() calls so a reviewer does not read them as mistakes.

Deleting an application from the web interface now writes a timestamp, its messages stay in the table, and a restore from the store brings both back. Listing, token authentication, the message stream and the sort order behave as before for live rows, because GORM's default scope filters the deleted ones out of every model query and the two raw joins now check the column themselves.

Before merging, a reviewer decides what should purge soft-deleted applications after a retention period, since nothing in this change does, and whether the next release notes should mention that AutoMigrate adds a column to applications on first start.

Run it on every Linear issue labelled agent

Label the issue that names the model and what must survive its deletion, and an automation in the Project with a linear trigger on linear.issue.labeled and the label agent starts one run per labelled issue, with the issue's text after the automation's prompt. The prompt asks for the same shape every time: a gorm.DeletedAt column, every unique index and every Find on the model checked, hard deletes kept where data must go, and the existing suite extended. A run whose issue names a model with a gorm.Model embedded, which already has the column, opens no pull request and says so on the issue.

Each run opens its own pull request that links the issue, so the person who labelled it reviews the diff and the test output there. The automation's max_runs_per_hour caps a burst of labels, and a label added twice is deduplicated on Linear's delivery id.

The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.

Why run it on Rush?

Soft delete is one column and a dozen consequences: a unique index that a dead row still occupies, a cascade that should keep running, a lookup that must not see deleted rows, one that must, and a raw join the default scope never filters. The agent reads every query on the model and the indexes in its tags before writing, and runs the suite with the race detector against SQLite as the repository's own make test does.

The run keeps every command and its output, so the reviewer reads the test run without a Go toolchain or the JavaScript build that make test also runs. Several services can be changed at once, each in its own sandbox, and one whose model already embeds gorm.Model gets only the tests that prove its queries are scoped.

Frequently asked questions

Why keep the messages when the application is deleted?

Because a soft delete exists to be undone, and an application without its messages is not restored. They stay out of the user's stream because the two raw joins on applications now check deleted_at IS NULL, and the purge removes them when the row goes for good.

What about the unique token index?

A soft-deleted application keeps its token under uix_applications_token, and new applications get fresh random tokens, so there is no collision. The composite index on user_id and sort_key is the one that needs care, which is why CreateApplication reads the highest key with Unscoped().

Does deleting a user still remove everything?

Yes. DeleteUserByID now purges each application, which deletes its messages and then the row itself, so an account deletion leaves nothing under Unscoped(). The new test asserts exactly that.

Why no restore endpoint?

api/application.go is documented with swagger comments and make check-ci runs check-swagger, so a new route means regenerated API docs and a web interface change. The store method exists and is tested; the route is a separate pull request, as the description says.

More GORM jobs

The same job on other stacks

Keep reading