10:49

Will Claude Code delete your production database?

4

In this video we read reports of an AI agent deleting a database and find the step where most of them went wrong. Then we build an app on Elements and follow one migration to the test database, through the tests, and to the production database.

Where an AI agent deletes a database

In most of the reports we read, the agent was using a migration tool. A migration is a file that changes your database. It adds a table, or it renames a column. The migration tool applies those files in order.

Sometimes the tool cannot apply a change. In these reports, the tool then offered a command that deletes every table and rebuilds the database from the migration files. Any data that did not come from a migration file is deleted. An agent that is stuck runs the command that lets it continue. One agent explained, "I chose the nuclear option of resetting the database." We found a command like that in five different migration tools, and in each case an agent ran it.

A migration tool is also a separate program that runs outside your build. It changes your database without type checking your code or running your tests.

A migration in Elements is a SQL file

We create a migration that adds two tables, users and posts:

Terminal
elements create migration "create initial tables" -tables=users,posts

The command writes a SQL file into app/migrations. The file starts with a comment that describes the migration. Then it has a create table statement for each table, with an id, a createdAt and an updatedAt column, and a trigger that sets updatedAt whenever a row changes. We add our own columns: an email, a username and a password hash in users, and a user id, a title and a body in posts.

To apply the migration, we save the file. The build applies it, and it is the same build that type checks the code and runs the tests. Claude Code works the same way on Elements. Claude Code edits the migration file and saves it, and the build applies the migration, so Claude Code never runs a migration tool.

A misspelled table name and one transaction per file

In the posts table we misspell the table that userId references, usres in place of users. When we save, the build is in the error state, and the error from Postgres says relation "usres" does not exist.

Elements runs all of the SQL in a migration file inside one transaction by default. When one statement fails, Postgres undoes the others, so the users table was not created either. The database is the way it was before the save. We fix the spelling and save again, and the build applies the migration.

A query with the sql() function

The home page's template exports an interface called FeedPost, the type for one row on the page: a title and a body from the posts table, and a username from the users table. The page's route function gets the rows with the sql() function and passes them to the template:

app/pages/home/index.ts
let posts = sql<FeedPost>(` select posts.title, posts.body, users.username from posts join users on users.id = posts.userId order by posts.createdAt desc `).all();

The type argument tells the TypeScript compiler that every row coming back is a FeedPost. Elements does not use an ORM, a library that turns each table into a class in your code. A page usually needs rows from two or three tables joined together, so we write the join in SQL and write an interface for exactly the rows the page needs.

Seed rows that apply only in development

The page says "No posts yet." because the database has no rows. We add the rows in a second migration:

Terminal
elements create migration "add some seed data"

Under the file's comment we add a build tag:

/** @migration env=development */

The build tag tells the build to apply this file in development and skip it in production. Then we insert two users and one post for each user. When we save the file, the page shows both posts.

A test for the page

We ask Claude Code to write a test:

Agent Prompt
Write a test for the home page.

The test adds a user and a post, calls the route, and checks that the post and its author are on the page. In Elements, tests are part of the build. When we save a change to the code or to a migration, the build runs the tests that depend on the files that changed.

A migration that breaks the page

Now a mistake an agent can make. A third migration renames a column:

Terminal
elements create migration "rename title to headline"
alter table posts rename column title to headline;

The statement is valid SQL, so Postgres will run it. The query for the page still selects posts.title.

When we save, the build is in the error state. The error comes from the test for the page: column "title" does not exist. The page in the browser still lists both posts, because the app database still has its title column.

Elements made two databases for this app on the laptop, a test database and the app database. The build works in the same order every time:

  1. It migrates the test database.
  2. It runs the tests.
  3. Only then does it migrate the app database.

The test for the page failed, because the test database no longer had the title column. So the build ended in the error state, and it never ran the migration on the app database.

The same migration on a deploy

The app was deployed earlier, and its production database has 200 posts. We deploy without fixing anything:

Terminal
elements deploy

The deploy stops. The build on the laptop has a failing test, so no file is sent to the app servers.

When the build on the laptop passes, every app server does what the laptop did. Each app server builds the new release in a pre-release folder, migrates the test database and runs the tests. Only when the tests pass does the app server migrate the production database, and then the app servers switch to the new release. If any step fails, the app servers keep running the old release.

We connect to the production database to check it:

Terminal
elements db -remote=production

A select of title from posts returns titles, so the title column was not renamed. A count of the rows in posts returns 200.

Fix the build with Claude Code on Elements

Agent Prompt
Fix the build.

Claude Code reads the failed test and changes the query, the template and the test to use headline. When Claude Code saves those files, the build runs. The tests pass, and the build migrates the app database.

Can a deploy reset the database?

The agents in the reports did not rename a column. They reset the database.

On the laptop, we can edit a migration that has already been applied. We add a column, isPublic, to the first migration. When we save, Elements restores the development database to the way it was before that migration, and applies the new version of the file.

A deploy does not do that. Elements keeps a hash of every migration it applies. A hash is a fingerprint of the file. When we deploy the edited migration, the hash does not match, and the deploy stops with an error: "This migration file does not match what's in the database, and it's too late to apply it now."

There are two ways to continue:

  • Put the migration file back the way it was, and add the new column in a new migration.
  • Set ignoreStaleMigrations for that environment, and Elements ignores the edited file.

Either way, the edited file is never applied to production. A deploy never applies a migration twice, and a deploy never resets your database.

Will Claude Code delete your production database?

On Elements, no. An agent never calls a tool to migrate your database. The agent edits a migration SQL file, and the build applies that file to the test database, runs the tests, and only then migrates the production database.

Install Elements from elements.dev (opens in a new tab) and build your app with Claude Code on Elements.

4 comments

How do I roll back a migration that's already in production?

What stops Claude Code from running a destructive command with elements db?

If I have more than one app server, which one migrates the production database?

How do I put up a maintenance page while a long migration runs?

Add a comment