WEBVTT

00:00:00.000 --> 00:00:02.160
Claude deleted this person's database,

00:00:02.180 --> 00:00:03.490
and nobody asked it to.

00:00:03.700 --> 00:00:04.720
It deleted this person's

00:00:04.740 --> 00:00:05.500
production database.

00:00:05.520 --> 00:00:06.250
Twice.

00:00:06.920 --> 00:00:08.480
And when it deleted this person's database,

00:00:08.500 --> 00:00:09.480
it said that was fine,

00:00:09.500 --> 00:00:10.780
because the database was small

00:00:10.800 --> 00:00:11.850
and could be rebuilt.

00:00:12.120 --> 00:00:13.510
Uh. Okay. Thanks?

00:00:14.200 --> 00:00:15.380
I've read a lot of these stories,

00:00:15.400 --> 00:00:17.470
and most of them go wrong in the same place.

00:00:17.600 --> 00:00:18.760
So let me show you where that is,

00:00:18.780 --> 00:00:20.950
and then how I built Elements to handle it.

00:00:22.060 --> 00:00:23.060
In most of these stories,

00:00:23.080 --> 00:00:24.850
the agent was using a migration tool.

00:00:25.140 --> 00:00:26.060
A migration is a file

00:00:26.080 --> 00:00:27.470
that changes your database.

00:00:27.720 --> 00:00:29.560
It adds a table, or it renames a column.

00:00:29.580 --> 00:00:30.740
And the migration tool

00:00:30.760 --> 00:00:32.410
applies those files in order.

00:00:33.140 --> 00:00:35.170
Sometimes the tool can't apply a change.

00:00:35.380 --> 00:00:36.740
When that happened in these stories,

00:00:36.760 --> 00:00:37.660
the tool offered a command

00:00:37.680 --> 00:00:39.680
that deletes every table and rebuilds

00:00:39.700 --> 00:00:41.450
the database from the migration files.

00:00:41.700 --> 00:00:42.800
Any data that didn't come

00:00:42.820 --> 00:00:44.530
from a migration file is deleted.

00:00:45.560 --> 00:00:46.800
And an agent that's stuck

00:00:46.820 --> 00:00:48.990
will try the command that gets it unstuck.

00:00:49.240 --> 00:00:51.170
Here's one agent explaining what it did:

00:00:51.340 --> 00:00:52.820
"I chose the nuclear option

00:00:52.840 --> 00:00:54.530
of resetting the database."

00:00:55.460 --> 00:00:56.420
We found a command

00:00:56.440 --> 00:00:58.640
like that in five different migration tools,

00:00:58.660 --> 00:01:00.730
and in each case an agent ran it.

00:01:01.700 --> 00:01:02.990
There's a second problem.

00:01:03.340 --> 00:01:05.020
A migration tool is a separate program

00:01:05.040 --> 00:01:06.670
that runs outside your build,

00:01:06.720 --> 00:01:08.180
so it changes your database

00:01:08.200 --> 00:01:09.540
without type checking your code

00:01:09.560 --> 00:01:10.730
or running your tests.

00:01:11.720 --> 00:01:13.290
So here's what I did in Elements.

00:01:13.580 --> 00:01:14.790
This is a new app.

00:01:14.860 --> 00:01:16.090
The build is on the left,

00:01:16.180 --> 00:01:17.180
my code is on the right,

00:01:17.200 --> 00:01:18.470
and the page is under it.

00:01:19.620 --> 00:01:20.820
In a second terminal,

00:01:20.840 --> 00:01:22.240
I'll create a migration that adds

00:01:22.260 --> 00:01:24.170
two tables, users and posts.

00:01:24.800 --> 00:01:25.660
Claude Code would normally run

00:01:25.680 --> 00:01:26.580
this command for you,

00:01:26.600 --> 00:01:27.920
but I'm doing it by hand

00:01:27.940 --> 00:01:29.270
so you can see each step.

00:01:30.540 --> 00:01:31.820
And there's the new migration file,

00:01:31.840 --> 00:01:33.270
in the migrations folder.

00:01:33.640 --> 00:01:35.690
A migration in Elements is a SQL file.

00:01:35.880 --> 00:01:37.080
There's no ORM to learn,

00:01:37.100 --> 00:01:39.630
and Claude Code already knows SQL very well.

00:01:40.460 --> 00:01:42.520
This is the SQL the create command wrote

00:01:42.540 --> 00:01:43.510
into the file.

00:01:43.820 --> 00:01:44.760
It starts with a comment

00:01:44.780 --> 00:01:46.290
that describes the migration.

00:01:46.380 --> 00:01:48.280
Then it has two create table statements,

00:01:48.300 --> 00:01:50.130
one for users and one for posts.

00:01:50.740 --> 00:01:52.820
The command gave each table three columns

00:01:52.840 --> 00:01:54.940
to start with: an id, a created at time,

00:01:54.960 --> 00:01:56.120
and an updated at time.

00:01:56.140 --> 00:01:58.000
And under each table there's a trigger,

00:01:58.020 --> 00:01:59.500
which sets updated at whenever

00:01:59.520 --> 00:02:00.470
a row changes.

00:02:01.340 --> 00:02:02.870
Now I'll add my own columns.

00:02:03.220 --> 00:02:04.120
In users: an email,

00:02:04.140 --> 00:02:06.130
which is text, not null, and unique.

00:02:06.560 --> 00:02:08.810
Then a username, and a password hash.

00:02:09.860 --> 00:02:10.910
And in posts:

00:02:11.140 --> 00:02:13.910
a user id, which references the users table.

00:02:14.120 --> 00:02:15.650
Then a title, and a body.

00:02:16.780 --> 00:02:18.550
So how do I apply this migration?

00:02:18.800 --> 00:02:19.740
I just save the file,

00:02:19.760 --> 00:02:21.890
and the build applies it automatically.

00:02:22.940 --> 00:02:24.650
Oops. The build failed.

00:02:24.990 --> 00:02:26.570
The error says the relation

00:02:26.590 --> 00:02:28.300
"usres" doesn't exist.

00:02:28.360 --> 00:02:29.900
I misspelled the users table.

00:02:31.380 --> 00:02:32.980
Elements runs all the SQL

00:02:33.000 --> 00:02:33.900
in a migration file

00:02:33.920 --> 00:02:36.170
inside one transaction by default.

00:02:36.580 --> 00:02:38.260
So when one statement fails,

00:02:38.280 --> 00:02:39.750
Postgres undoes the others.

00:02:40.120 --> 00:02:42.030
The users table wasn't created either.

00:02:42.220 --> 00:02:43.580
My database is exactly the way

00:02:43.600 --> 00:02:45.010
it was before I saved.

00:02:45.720 --> 00:02:47.090
So I fix the spelling.

00:02:47.920 --> 00:02:49.680
I save again, and this time

00:02:49.700 --> 00:02:51.210
the build applies the migration.

00:02:51.480 --> 00:02:52.380
I didn't run a command.

00:02:52.400 --> 00:02:53.388
In Elements the build

00:02:53.408 --> 00:02:54.890
applies your migrations,

00:02:55.020 --> 00:02:56.020
and it's the same build

00:02:56.040 --> 00:02:57.160
that type checks your code

00:02:57.180 --> 00:02:58.310
and runs your tests.

00:02:59.480 --> 00:03:01.410
That's how you work with a migration here.

00:03:01.460 --> 00:03:03.190
You edit the file, you save,

00:03:03.240 --> 00:03:04.730
and you check the build for errors.

00:03:05.340 --> 00:03:06.640
Claude Code works the same way.

00:03:06.660 --> 00:03:08.570
It never runs a migration tool.

00:03:09.740 --> 00:03:11.620
There's no rows in the database yet,

00:03:11.640 --> 00:03:13.970
and our HTML page isn't using the database.

00:03:14.120 --> 00:03:16.190
So let's update the page to show the posts.

00:03:16.740 --> 00:03:17.800
In the page's template,

00:03:17.820 --> 00:03:20.070
I export an interface called FeedPost.

00:03:20.300 --> 00:03:22.570
It's the type for one row on this page.

00:03:22.860 --> 00:03:24.040
It has a title and a body,

00:03:24.060 --> 00:03:25.510
which come from the posts table.

00:03:25.600 --> 00:03:26.180
And a username,

00:03:26.200 --> 00:03:27.890
which comes from the users table.

00:03:28.520 --> 00:03:29.600
On the html tag,

00:03:29.620 --> 00:03:31.870
I add a constructor attribute called posts.

00:03:32.040 --> 00:03:33.220
It's a list of feed posts,

00:03:33.240 --> 00:03:35.050
and it defaults to an empty list.

00:03:35.500 --> 00:03:37.650
Then a paragraph with an if directive:

00:03:37.880 --> 00:03:38.760
when the list is empty,

00:03:38.780 --> 00:03:40.210
it says no posts yet.

00:03:40.540 --> 00:03:42.060
Then an article with the for directive,

00:03:42.080 --> 00:03:44.310
which repeats once for each post.

00:03:45.080 --> 00:03:46.420
Inside it goes the post's title

00:03:46.440 --> 00:03:47.250
in a heading,

00:03:47.280 --> 00:03:49.210
the body in a paragraph, and the username.

00:03:50.360 --> 00:03:51.340
I save the file,

00:03:51.360 --> 00:03:53.190
and the page is updated immediately.

00:03:53.600 --> 00:03:54.800
It says no posts yet,

00:03:54.820 --> 00:03:55.840
because nothing is giving

00:03:55.860 --> 00:03:57.130
the template any posts.

00:03:58.380 --> 00:04:00.250
So where does the template get its posts?

00:04:00.920 --> 00:04:02.170
From its route function.

00:04:02.340 --> 00:04:03.760
In the app's index file,

00:04:03.780 --> 00:04:05.035
we call app dot route,

00:04:05.055 --> 00:04:07.150
and we pass it a URL and a function.

00:04:07.340 --> 00:04:09.210
When the user navigates to this URL,

00:04:09.340 --> 00:04:10.890
this home function is called.

00:04:11.860 --> 00:04:13.800
And here's where that function is defined,

00:04:13.820 --> 00:04:15.650
right next to the page it serves.

00:04:16.040 --> 00:04:17.000
Right now, all it does

00:04:17.020 --> 00:04:18.350
is return the template.

00:04:19.860 --> 00:04:21.700
So inside this function I'll get the posts

00:04:21.720 --> 00:04:22.460
from the database,

00:04:22.480 --> 00:04:24.060
and pass them to the template.

00:04:25.000 --> 00:04:26.420
I'll make a variable called posts,

00:04:26.440 --> 00:04:28.160
and call the sql function.

00:04:28.270 --> 00:04:30.171
I pass it a type argument, FeedPost,

00:04:30.191 --> 00:04:31.600
which tells the TypeScript compiler

00:04:31.620 --> 00:04:34.125
that every row coming back is a FeedPost.

00:04:34.145 --> 00:04:35.320
Then I write the query.

00:04:35.340 --> 00:04:37.020
Select posts dot title,

00:04:37.040 --> 00:04:39.420
posts dot body, and users dot username.

00:04:39.440 --> 00:04:40.780
From posts.

00:04:40.950 --> 00:04:42.460
And here I join the two tables:

00:04:42.480 --> 00:04:44.500
join users, on users dot id

00:04:44.520 --> 00:04:46.510
equals posts dot user id.

00:04:46.740 --> 00:04:48.590
That gives each post its author.

00:04:49.300 --> 00:04:50.720
Order by created at, descending,

00:04:50.740 --> 00:04:52.350
so the newest post is first.

00:04:52.700 --> 00:04:53.700
Now I'll call all,

00:04:53.720 --> 00:04:55.190
which gives me an array of the rows.

00:04:55.740 --> 00:04:57.710
Notice I wrote that join myself.

00:04:57.900 --> 00:04:58.940
That's one of the reasons Elements

00:04:58.960 --> 00:05:00.470
doesn't use an ORM.

00:05:00.660 --> 00:05:02.800
An ORM is a library that turns each table

00:05:02.820 --> 00:05:04.450
into a class in your code.

00:05:04.540 --> 00:05:06.450
So it gives a type for each table,

00:05:06.540 --> 00:05:07.860
but a page usually needs rows

00:05:07.880 --> 00:05:10.030
from two or three tables joined together.

00:05:10.320 --> 00:05:11.680
Here you write the join in SQL,

00:05:11.700 --> 00:05:12.900
and you write an interface

00:05:12.920 --> 00:05:15.210
for exactly the rows this page needs.

00:05:15.360 --> 00:05:16.980
No unnecessary abstractions.

00:05:17.000 --> 00:05:17.770
It's just SQL.

00:05:18.960 --> 00:05:21.090
Then I pass posts to the template.

00:05:22.000 --> 00:05:22.560
I save,

00:05:22.580 --> 00:05:24.460
and the page still says no posts yet,

00:05:24.480 --> 00:05:26.270
because there's no rows in the database.

00:05:27.340 --> 00:05:28.910
So how do I get some rows in?

00:05:29.180 --> 00:05:30.550
With a second migration.

00:05:31.340 --> 00:05:33.290
Now there are two migration files.

00:05:33.620 --> 00:05:34.880
Whenever you change the database,

00:05:34.900 --> 00:05:35.720
you add another file,

00:05:35.740 --> 00:05:37.550
and the build applies them in order,

00:05:37.800 --> 00:05:38.610
oldest first.

00:05:38.800 --> 00:05:40.060
An app we worked on last week

00:05:40.080 --> 00:05:42.560
has about 350 migration files,

00:05:42.580 --> 00:05:43.570
and that's fine.

00:05:44.300 --> 00:05:45.920
But I don't want these rows in production.

00:05:45.940 --> 00:05:48.170
So under the comment, I add a build tag.

00:05:48.660 --> 00:05:50.950
At migration, env equals development.

00:05:51.060 --> 00:05:52.740
That tells the build to apply this file

00:05:52.760 --> 00:05:54.950
in development, and skip it in production.

00:05:55.920 --> 00:05:56.710
Then the rows.

00:05:56.900 --> 00:05:58.820
I insert into users: an email,

00:05:58.840 --> 00:06:00.490
a username, and a password hash.

00:06:00.560 --> 00:06:01.580
The first user is Ada,

00:06:01.600 --> 00:06:02.950
and the second is Grace.

00:06:03.160 --> 00:06:04.520
The password hash is a placeholder,

00:06:04.540 --> 00:06:06.450
because nobody signs in as these two.

00:06:06.780 --> 00:06:07.870
And the posts.

00:06:08.080 --> 00:06:08.920
I insert into posts:

00:06:08.940 --> 00:06:10.970
a user id, a title, and a body.

00:06:11.180 --> 00:06:11.920
For the user id,

00:06:11.940 --> 00:06:13.850
I select the id of the user named Ada,

00:06:14.080 --> 00:06:14.820
and her post

00:06:14.840 --> 00:06:16.730
is Notes on the analytical engine.

00:06:16.920 --> 00:06:17.910
Then one for Grace:

00:06:17.960 --> 00:06:19.030
The first compiler.

00:06:20.140 --> 00:06:22.370
Now keep your eye on the page when I save.

00:06:22.440 --> 00:06:23.820
And our page updates immediately

00:06:23.840 --> 00:06:24.830
with the data.

00:06:26.020 --> 00:06:27.380
Now, how will I know if a change

00:06:27.400 --> 00:06:29.250
to the database breaks this page?

00:06:29.560 --> 00:06:30.630
We need a test.

00:06:30.820 --> 00:06:32.910
So I'll ask Claude Code to write one.

00:06:33.500 --> 00:06:34.900
Here's the test it wrote.

00:06:35.360 --> 00:06:37.010
It adds a user and a post.

00:06:37.300 --> 00:06:38.260
It calls the route.

00:06:38.280 --> 00:06:39.400
And it checks that the post

00:06:39.420 --> 00:06:41.450
and its author are on the page.

00:06:42.240 --> 00:06:43.950
In Elements, tests are part of the build.

00:06:44.040 --> 00:06:45.380
When I save a change to my code

00:06:45.400 --> 00:06:47.260
or to a migration, the build runs the tests

00:06:47.280 --> 00:06:48.890
that depend on what changed.

00:06:48.960 --> 00:06:50.910
And right now, this test passes.

00:06:52.300 --> 00:06:54.430
Now a mistake an agent really makes.

00:06:54.780 --> 00:06:55.960
Claude Code renames a column,

00:06:55.980 --> 00:06:57.410
in a third migration.

00:06:58.420 --> 00:06:59.180
Alter table posts,

00:06:59.200 --> 00:07:01.470
rename column title to headline.

00:07:01.730 --> 00:07:03.500
It's one line of SQL and it's valid,

00:07:03.520 --> 00:07:04.940
so Postgres will run it.

00:07:05.740 --> 00:07:07.370
But look at the query for the page.

00:07:07.620 --> 00:07:09.230
It still asks for title.

00:07:09.680 --> 00:07:11.430
So what happens when I save?

00:07:12.120 --> 00:07:13.870
The build is in the error state.

00:07:14.080 --> 00:07:16.140
The error comes from the test for the page:

00:07:16.160 --> 00:07:18.030
the column title does not exist.

00:07:18.940 --> 00:07:20.130
But look at the page.

00:07:20.300 --> 00:07:21.420
The posts are still there,

00:07:21.440 --> 00:07:22.360
so the app database

00:07:22.380 --> 00:07:24.290
still has its title column.

00:07:25.040 --> 00:07:25.850
Here's why.

00:07:26.360 --> 00:07:27.320
Elements made two databases

00:07:27.340 --> 00:07:28.940
for this app on my laptop,

00:07:28.960 --> 00:07:31.110
a test database and the app database.

00:07:31.200 --> 00:07:32.700
When I saved, the build ran

00:07:32.720 --> 00:07:34.920
the migration on the test database first,

00:07:34.990 --> 00:07:36.410
and then it ran the tests.

00:07:36.900 --> 00:07:38.460
The test for the page failed,

00:07:38.480 --> 00:07:39.920
because in the test database

00:07:39.940 --> 00:07:41.470
the title column was gone.

00:07:42.000 --> 00:07:43.520
So the build ended in the error state,

00:07:43.540 --> 00:07:44.360
and it never ran

00:07:44.380 --> 00:07:46.030
the migration on the app database.

00:07:46.580 --> 00:07:48.950
The build works in that order every time.

00:07:49.200 --> 00:07:50.740
It migrates the test database,

00:07:50.760 --> 00:07:51.620
it runs the tests,

00:07:51.640 --> 00:07:53.000
and only then does it migrate

00:07:53.020 --> 00:07:54.010
the app database.

00:07:55.140 --> 00:07:57.120
So far, all of this ran on my laptop,

00:07:57.140 --> 00:07:58.690
on my development database.

00:07:58.740 --> 00:07:59.680
But the people I mentioned

00:07:59.700 --> 00:08:00.680
at the start of this video

00:08:00.700 --> 00:08:02.130
lost a production database.

00:08:02.660 --> 00:08:04.570
So what happens if I deploy this migration?

00:08:04.880 --> 00:08:05.830
Let's try it.

00:08:06.080 --> 00:08:07.340
I deployed this app earlier,

00:08:07.360 --> 00:08:09.750
and its production database has 200 posts.

00:08:10.060 --> 00:08:11.710
And I haven't fixed anything.

00:08:12.040 --> 00:08:13.900
The migration that renames title to headline

00:08:13.920 --> 00:08:15.420
is still in my migrations folder,

00:08:15.440 --> 00:08:17.190
and the query still asks for title.

00:08:17.800 --> 00:08:19.260
A deploy is one command.

00:08:19.280 --> 00:08:20.830
And the deploy stopped.

00:08:21.680 --> 00:08:24.150
The build on my laptop has a failing test,

00:08:24.300 --> 00:08:26.490
so no file was sent to the app servers.

00:08:27.640 --> 00:08:30.230
But say the build on my laptop had passed.

00:08:30.720 --> 00:08:31.740
Then every app server

00:08:31.760 --> 00:08:33.150
does what my laptop did.

00:08:33.200 --> 00:08:34.120
It builds the new release

00:08:34.140 --> 00:08:35.570
in a pre-release folder.

00:08:35.800 --> 00:08:37.040
It migrates the test database

00:08:37.060 --> 00:08:38.190
and runs the tests.

00:08:38.620 --> 00:08:39.860
Only when the tests pass

00:08:39.880 --> 00:08:42.050
does it migrate the production database,

00:08:42.320 --> 00:08:43.460
and then the app servers switch

00:08:43.480 --> 00:08:44.530
to the new release.

00:08:44.780 --> 00:08:46.110
If any step fails,

00:08:46.240 --> 00:08:47.260
the app servers keep running

00:08:47.280 --> 00:08:48.390
the old release.

00:08:49.320 --> 00:08:51.270
So is the production database okay?

00:08:52.000 --> 00:08:53.510
Let me connect to it and check.

00:08:53.870 --> 00:08:55.240
I select the title from posts,

00:08:55.260 --> 00:08:57.270
and the title column is still there.

00:08:57.740 --> 00:08:59.710
I count the rows, and there are 200.

00:09:01.080 --> 00:09:03.490
Now I'll ask Claude Code to fix the build.

00:09:04.290 --> 00:09:05.630
It reads the failed test.

00:09:05.700 --> 00:09:06.660
It changes the query,

00:09:06.680 --> 00:09:08.530
the template and the test to use headline.

00:09:08.980 --> 00:09:10.180
When it saves those files,

00:09:10.200 --> 00:09:11.430
the build runs immediately.

00:09:11.820 --> 00:09:13.400
The tests pass, and the app database

00:09:13.420 --> 00:09:14.830
is migrated successfully.

00:09:16.160 --> 00:09:17.250
One more question.

00:09:17.580 --> 00:09:18.600
The agents I mentioned

00:09:18.620 --> 00:09:19.640
at the start of this video

00:09:19.660 --> 00:09:20.970
didn't rename a column.

00:09:21.020 --> 00:09:22.790
They reset the database.

00:09:22.950 --> 00:09:24.530
So can a deploy do that?

00:09:25.140 --> 00:09:26.340
On my laptop, I can edit

00:09:26.360 --> 00:09:28.390
a migration that's already been applied.

00:09:28.700 --> 00:09:30.560
I'll add a column to the first migration:

00:09:30.580 --> 00:09:31.690
is public, a boolean.

00:09:32.400 --> 00:09:33.230
When I save,

00:09:33.320 --> 00:09:34.800
Elements restores my development database

00:09:34.820 --> 00:09:36.600
to the way it was before that migration,

00:09:36.620 --> 00:09:38.230
and applies the new version.

00:09:38.840 --> 00:09:40.330
A deploy doesn't do that.

00:09:40.820 --> 00:09:41.600
Elements keeps a hash

00:09:41.620 --> 00:09:43.590
of every migration it applies.

00:09:43.960 --> 00:09:45.830
A hash is a fingerprint of the file.

00:09:46.780 --> 00:09:48.540
So I deploy the edited migration.

00:09:48.560 --> 00:09:50.060
The hash doesn't match,

00:09:50.080 --> 00:09:51.970
so the deploy stops with an error.

00:09:52.520 --> 00:09:53.870
The migration is stale,

00:09:53.940 --> 00:09:55.670
and it can't be run again.

00:09:56.740 --> 00:09:58.110
So how do I fix that?

00:09:58.260 --> 00:09:59.400
I put the migration file

00:09:59.420 --> 00:10:00.660
back the way it was,

00:10:00.680 --> 00:10:03.110
and I add my new column in a new migration.

00:10:03.340 --> 00:10:05.540
Or I can set ignore stale migrations

00:10:05.560 --> 00:10:06.540
for that environment,

00:10:06.560 --> 00:10:08.470
and Elements will ignore the edited file.

00:10:08.720 --> 00:10:09.400
Either way,

00:10:09.420 --> 00:10:12.030
the edited file never applies to production.

00:10:13.520 --> 00:10:16.130
A deploy never applies a migration twice,

00:10:16.220 --> 00:10:18.170
and it never resets your database.

00:10:19.020 --> 00:10:20.020
So will Claude Code drop

00:10:20.040 --> 00:10:21.250
your production database?

00:10:22.020 --> 00:10:23.370
On Elements, no.

00:10:23.780 --> 00:10:25.040
An agent never calls a tool

00:10:25.060 --> 00:10:26.310
to migrate your database.

00:10:26.440 --> 00:10:28.830
It only edits a migration SQL file.

00:10:28.880 --> 00:10:31.110
And Elements runs the migrations safely.

00:10:32.160 --> 00:10:34.850
You can install Elements at elements.dev.

00:10:36.100 --> 00:10:37.420
Subscribe, and tell us your craziest

00:10:37.440 --> 00:10:38.540
production database story

00:10:38.560 --> 00:10:39.830
in the comments below.
