Will Claude Code delete your production database?
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:
Terminalelements 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.tslet 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:
Terminalelements 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 PromptWrite 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:
Terminalelements 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:
- It migrates the test database.
- It runs the tests.
- 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:
Terminalelements 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:
Terminalelements 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 PromptFix 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
ignoreStaleMigrationsfor 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.
7 comments · 7 replies
How do I roll back a migration that's already in production?
In production, migrations only go forward. To undo one, add a new migration that corrects the change, and deploy it. If a migration renamed a column and you want the old name, the new migration renames the column again.
On your laptop you have another option. Edit or delete the migration file, and Elements restores your development database to the way it was before that migration applied.
A migration that breaks a test never gets as far as production. The deploy migrates the test database and runs the tests first, and it stops if a test fails.
What stops Claude Code from running a destructive command with
elements db?Agents usually don't do destructive things on purpose. They're trying to reach a goal. The problem comes when you tell an agent to update the database schema and give it no constraints, and it runs a migration tool's reset command to get there. On Elements that never comes up, because the build applies the migration automatically. The agent only edits a SQL file.
For queries and updates through
elements db, you choose your own comfort level with Claude doing that correctly. We have not had a problem with it. If it's a repeatable business function, consider making an API route that Claude can call instead. Over time you build up your business functions that way, each one tested and built like the rest of your app.If I have more than one app server, which one migrates the production database?
Whichever server gets to the database first. Every app server builds the new release and runs the tests, and then migrates the production database. Elements holds a Postgres lock on the database while one server migrates it. The other servers wait for the lock, find the migrations already applied, and skip them.
So each migration applies once, with two servers or twenty. You don't configure any of this. The servers only need to share the same Postgres database.
How do I put up a maintenance page while a long migration runs?
Deploy with the
-maintflag:The build, the test database migration and the tests all run with your site up. The maintenance page goes up on every machine just before the production database is migrated, and it comes down when the new release is live.
The page is
static/maint.htmlin your project, so you can make it look like your app.You only need the flag for a long migration. A normal deploy never shows the page, because the previous release keeps serving requests until the new release is ready. You can also turn the page on and off yourself at any time with
elements maint onandelements maint off.Do I have to write the migration SQL myself, or does Claude Code write it?
Claude Code writes it. In the video I typed each migration by hand so you could see every step. Normally you tell Claude what you want, like "add a due date to tasks". Claude runs
elements create migration, writes the SQL into the new file and saves it. The build applies the migration to the test database, runs the tests, and then migrates the app database.A migration is plain SQL, and Claude already knows SQL very well. You can open the file in
app/migrationsand read exactly what changed in your database.How do I get seed data into staging but keep it out of production?
Name the environments in the build tag at the top of the migration file:
The build applies that migration in development and in staging, and a production deploy skips the file. The environment names are separated by a comma with no space.
A migration with no build tag applies in every environment. Rows that every environment needs, like a list of countries or plans, go in a migration with no build tag.
What if a migration needs
create index concurrently, which can't run in a transaction?Add
tx=falseto the build tag at the top of that migration file:Elements runs each statement in that file on its own, and the build and the deploy apply the file like any other migration. Your production tables keep taking writes while the index builds.
If a statement fails, the next build runs the file again from the top, so write each statement to be safe to repeat, like the drop and create pair above. Keep your other schema changes in a separate migration, where they run inside one transaction.
Add a comment