--- title: "Will Claude Code delete your production database?" author: "@chris" author_url: https://elements.dev/u/@chris url: https://elements.dev/blog/will-claude-code-delete-your-production-database-qy7ox9v9 kind: post format: video video: https://elements.dev/blog/will-claude-code-delete-your-production-database-qy7ox9v9/video.mp4 poster: https://elements.dev/blog/will-claude-code-delete-your-production-database-qy7ox9v9/poster.jpg duration: "10:49" transcript_vtt: https://elements.dev/blog/will-claude-code-delete-your-production-database-qy7ox9v9/transcript.vtt --- # Will Claude Code delete your production database? by [@chris](https://elements.dev/u/@chris) ## Description Will Claude Code delete your production database? In most reports of an AI agent deleting a database, the agent was running a migration tool, and it ran that tool's reset command. This video shows that step, then shows how database migrations work with Claude Code on Elements, where the build applies each migration and the agent never runs a migration tool. A migration in Elements is a SQL file. When you save the file, the build applies the migration to the test database first, then runs your tests, and only then migrates the app database. We create a users table and a posts table, misspell a table name and see Postgres undo every statement in the file, write a query with a join using the sql() function, add seed rows that apply only in development, and ask Claude Code to write a test. Then a migration renames a column that a page still queries. The test fails on the test database, so the build never runs that migration on the app database, and the deploy stops before any file is sent to the app servers. The production database still has its title column and its 200 posts. Last, we edit a migration that production has already applied. Elements keeps a hash of every migration it applies, so the deploy stops with an error and the edited file is never applied. A deploy never applies a migration twice, and it never resets your database. Elements is a build server and tooling, app framework, Postgres database and deployment, engineered from the ground up as an integrated system for building web apps with AI. ## Notes 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`: ```bash 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: ```ts app/pages/home/index.ts let posts = sql(` 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: ```bash Terminal elements create migration "add some seed data" ``` Under the file's comment we add a build tag: ```sql /** @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: ```text Agent 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: ```bash Terminal elements create migration "rename title to headline" ``` ```sql 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: ```bash 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: ```bash 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 ```text Agent 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](https://elements.dev) and build your app with Claude Code on Elements. ## Transcript Claude deleted this person's database, and nobody asked it to. It deleted this person's production database. Twice. And when it deleted this person's database, it said that was fine, because the database was small and could be rebuilt. Uh. Okay. Thanks? I've read a lot of these stories, and most of them go wrong in the same place. So let me show you where that is, and then how I built Elements to handle it. In most of these stories, 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. And the migration tool applies those files in order. Sometimes the tool can't apply a change. When that happened in these stories, the tool offered a command that deletes every table and rebuilds the database from the migration files. Any data that didn't come from a migration file is deleted. And an agent that's stuck will try the command that gets it unstuck. Here's one agent explaining what it did: "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. There's a second problem. A migration tool is a separate program that runs outside your build, so it changes your database without type checking your code or running your tests. So here's what I did in Elements. This is a new app. The build is on the left, my code is on the right, and the page is under it. In a second terminal, I'll create a migration that adds two tables, users and posts. Claude Code would normally run this command for you, but I'm doing it by hand so you can see each step. And there's the new migration file, in the migrations folder. A migration in Elements is a SQL file. There's no ORM to learn, and Claude Code already knows SQL very well. This is the SQL the create command wrote into the file. It starts with a comment that describes the migration. Then it has two create table statements, one for users and one for posts. The command gave each table three columns to start with: an id, a created at time, and an updated at time. And under each table there's a trigger, which sets updated at whenever a row changes. Now I'll add my own columns. In users: an email, which is text, not null, and unique. Then a username, and a password hash. And in posts: a user id, which references the users table. Then a title, and a body. So how do I apply this migration? I just save the file, and the build applies it automatically. Oops. The build failed. The error says the relation "usres" doesn't exist. I misspelled the users table. Elements runs all the SQL in a migration file inside one transaction by default. So when one statement fails, Postgres undoes the others. The users table wasn't created either. My database is exactly the way it was before I saved. So I fix the spelling. I save again, and this time the build applies the migration. I didn't run a command. In Elements the build applies your migrations, and it's the same build that type checks your code and runs your tests. That's how you work with a migration here. You edit the file, you save, and you check the build for errors. Claude Code works the same way. It never runs a migration tool. There's no rows in the database yet, and our HTML page isn't using the database. So let's update the page to show the posts. In the page's template, I export an interface called FeedPost. It's the type for one row on this page. It has a title and a body, which come from the posts table. And a username, which comes from the users table. On the html tag, I add a constructor attribute called posts. It's a list of feed posts, and it defaults to an empty list. Then a paragraph with an if directive: when the list is empty, it says no posts yet. Then an article with the for directive, which repeats once for each post. Inside it goes the post's title in a heading, the body in a paragraph, and the username. I save the file, and the page is updated immediately. It says no posts yet, because nothing is giving the template any posts. So where does the template get its posts? From its route function. In the app's index file, we call app dot route, and we pass it a URL and a function. When the user navigates to this URL, this home function is called. And here's where that function is defined, right next to the page it serves. Right now, all it does is return the template. So inside this function I'll get the posts from the database, and pass them to the template. I'll make a variable called posts, and call the sql function. I pass it a type argument, FeedPost, which tells the TypeScript compiler that every row coming back is a FeedPost. Then I write the query. Select posts dot title, posts dot body, and users dot username. From posts. And here I join the two tables: join users, on users dot id equals posts dot user id. That gives each post its author. Order by created at, descending, so the newest post is first. Now I'll call all, which gives me an array of the rows. Notice I wrote that join myself. That's one of the reasons Elements doesn't use an ORM. An ORM is a library that turns each table into a class in your code. So it gives a type for each table, but a page usually needs rows from two or three tables joined together. Here you write the join in SQL, and you write an interface for exactly the rows this page needs. No unnecessary abstractions. It's just SQL. Then I pass posts to the template. I save, and the page still says no posts yet, because there's no rows in the database. So how do I get some rows in? With a second migration. Now there are two migration files. Whenever you change the database, you add another file, and the build applies them in order, oldest first. An app we worked on last week has about 350 migration files, and that's fine. But I don't want these rows in production. So under the comment, I add a build tag. At migration, env equals development. That tells the build to apply this file in development, and skip it in production. Then the rows. I insert into users: an email, a username, and a password hash. The first user is Ada, and the second is Grace. The password hash is a placeholder, because nobody signs in as these two. And the posts. I insert into posts: a user id, a title, and a body. For the user id, I select the id of the user named Ada, and her post is Notes on the analytical engine. Then one for Grace: The first compiler. Now keep your eye on the page when I save. And our page updates immediately with the data. Now, how will I know if a change to the database breaks this page? We need a test. So I'll ask Claude Code to write one. Here's the test it wrote. It adds a user and a post. It calls the route. And it checks that the post and its author are on the page. In Elements, tests are part of the build. When I save a change to my code or to a migration, the build runs the tests that depend on what changed. And right now, this test passes. Now a mistake an agent really makes. Claude Code renames a column, in a third migration. Alter table posts, rename column title to headline. It's one line of SQL and it's valid, so Postgres will run it. But look at the query for the page. It still asks for title. So what happens when I save? The build is in the error state. The error comes from the test for the page: the column title does not exist. But look at the page. The posts are still there, so the app database still has its title column. Here's why. Elements made two databases for this app on my laptop, a test database and the app database. When I saved, the build ran the migration on the test database first, and then it ran the tests. The test for the page failed, because in the test database the title column was gone. So the build ended in the error state, and it never ran the migration on the app database. The build works in that order every time. It migrates the test database, it runs the tests, and only then does it migrate the app database. So far, all of this ran on my laptop, on my development database. But the people I mentioned at the start of this video lost a production database. So what happens if I deploy this migration? Let's try it. I deployed this app earlier, and its production database has 200 posts. And I haven't fixed anything. The migration that renames title to headline is still in my migrations folder, and the query still asks for title. A deploy is one command. And the deploy stopped. The build on my laptop has a failing test, so no file was sent to the app servers. But say the build on my laptop had passed. Then every app server does what my laptop did. It builds the new release in a pre-release folder. It migrates the test database and runs the tests. Only when the tests pass does it 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. So is the production database okay? Let me connect to it and check. I select the title from posts, and the title column is still there. I count the rows, and there are 200. Now I'll ask Claude Code to fix the build. It reads the failed test. It changes the query, the template and the test to use headline. When it saves those files, the build runs immediately. The tests pass, and the app database is migrated successfully. One more question. The agents I mentioned at the start of this video didn't rename a column. They reset the database. So can a deploy do that? On my laptop, I can edit a migration that's already been applied. I'll add a column to the first migration: is public, a boolean. When I save, Elements restores my development database to the way it was before that migration, and applies the new version. A deploy doesn't do that. Elements keeps a hash of every migration it applies. A hash is a fingerprint of the file. So I deploy the edited migration. The hash doesn't match, so the deploy stops with an error. The migration is stale, and it can't be run again. So how do I fix that? I put the migration file back the way it was, and I add my new column in a new migration. Or I can set ignore stale migrations for that environment, and Elements will ignore the edited file. Either way, the edited file never applies to production. A deploy never applies a migration twice, and it never resets your database. So will Claude Code drop your production database? On Elements, no. An agent never calls a tool to migrate your database. It only edits a migration SQL file. And Elements runs the migrations safely. You can install Elements at elements.dev. Subscribe, and tell us your craziest production database story in the comments below.